User Tools

Site Tools


Table of Contents

C++ using

sample 1

#include <iostream>
 
class A {
    public:
        void print(char *str) {
            std::cout << "A:" << str << std::endl;
        }
};
 
class B : public A {
    public:
        void print(char *str) {
            std::cout << "B1:" << str << std::endl;
        }
 
        using A::print;
        void print(std::string& str) {
            std::cout << "B2:" << str << std::endl;
        }
};
 
int main(int argc, char **argv){
 
    A a;
    char str[] = "qwerty";
    a.print(str);
 
    std::string sstr("12345");
 
    B b;
    b.print(str);
    b.print(sstr);
 
    return 0;
}
$ ./virt2
A:qwerty
B1:qwerty
B2:12345

sample 2

#include <iostream>
 
class A {
    public:
        void print(char *str) {
            std::cout << "A:" << str << std::endl;
        }
};
 
class B : public A {
    public:
        //void print(char *str) {
            //std::cout << "B1:" << str << std::endl;
        //}
 
        using A::print;
        void print(std::string& str) {
            std::cout << "B:" << str << std::endl;
        }
};
 
int main(int argc, char **argv){
 
    A a;
    char str[] = "qwerty";
    a.print(str);
 
    std::string sstr("12345");
 
    B b;
    b.print(str);
    b.print(sstr);
 
    return 0;
}
$ ./virt2
A:qwerty
A:qwerty
B:12345