继承中的同名成员变量和函数
2016-08-23 15:41
585 查看
同名成员函数
#include <iostream>
using namespace std;
class A
{
public:
int a;
int b;
public:
void get()
{
cout<<"b "<<b<<endl;
}
void print()
{
cout<<"AAAAA "<<endl;
}
protected:
private:
};
class B : public A
{
public:
int b;
int c;
public:
void get_child()
{
cout<<"b "<<b<<endl;
}
void print()
{
cout<<"BBBB "<<endl;
}
protected:
private:
};
int main()
{
B b1;
b1.print();
b1.A::print();
b1.B::print(); //默认情况
return 0;
}
同名成员变量
#include <iostream>
using namespace std;
class A
{
public:
int a;
int b;
public:
void get()
{
cout<<"b "<<b<<endl;
}
void print()
{
cout<<"AAAAA "<<endl;
}
protected:
private:
};
class B : public A
{
public:
int b;
int c;
public:
void get_child()
{
cout<<"b "<<b<<endl;
}
void print()
{
cout<<"BBBB "<<endl;
}
protected:
private:
};
//同名成员变量
int main()
{
B b1;
b1.b = 1; //修改的是子类的b
b1.get_child();
b1.get();
b1.A::b = 100; //修改父类的b
b1.B::b = 200; //修改子类的b 默认情况是B
b1.get();
b1.get_child();
cout<<"hello..."<<endl;
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
int a;
int b;
public:
void get()
{
cout<<"b "<<b<<endl;
}
void print()
{
cout<<"AAAAA "<<endl;
}
protected:
private:
};
class B : public A
{
public:
int b;
int c;
public:
void get_child()
{
cout<<"b "<<b<<endl;
}
void print()
{
cout<<"BBBB "<<endl;
}
protected:
private:
};
int main()
{
B b1;
b1.print();
b1.A::print();
b1.B::print(); //默认情况
return 0;
}
同名成员变量
#include <iostream>
using namespace std;
class A
{
public:
int a;
int b;
public:
void get()
{
cout<<"b "<<b<<endl;
}
void print()
{
cout<<"AAAAA "<<endl;
}
protected:
private:
};
class B : public A
{
public:
int b;
int c;
public:
void get_child()
{
cout<<"b "<<b<<endl;
}
void print()
{
cout<<"BBBB "<<endl;
}
protected:
private:
};
//同名成员变量
int main()
{
B b1;
b1.b = 1; //修改的是子类的b
b1.get_child();
b1.get();
b1.A::b = 100; //修改父类的b
b1.B::b = 200; //修改子类的b 默认情况是B
b1.get();
b1.get_child();
cout<<"hello..."<<endl;
return 0;
}
相关文章推荐
- C++学习笔记:3.3.5 继承中的同名成员变量和函数
- C++继承时子类定义同名成员变量时的调用继承函数的问题
- C++继承时子类定义同名成员变量时的调用继承函数的问题
- C++继承时子类定义同名成员变量时的调用继承函数的问题
- public protected private 成员函数和成员变量在public protected private 继承后访问权限问题
- 继承、多态——成员变量、成员函数、构造函数(this、super)
- (25)继承中成员变量、函数中覆盖、构造函数的理解
- 静态变量和静态成员函数的继承
- C++继承中覆盖和隐藏基类成员变量或成员函数
- Java 继承之子父类中的成员变量和函数
- C++ 子类和父类同名成员变量或函数
- C++继承中覆盖和隐藏基类成员变量或成员函数
- 继承 单继承 多层继承 成员变量:this & super ; 成员函数:覆盖操作; 一个对象实例化过程!!
- [C++] 继承与同名成员变量
- public protected private 成员函数和成员变量在public protected private 继承后访问权限问题
- C++继承时子类父类同名成员变量的问题
- 黑马程序员——声明类Person,包含2个成员变量:name、age。定义函数sayHello(),调用时输出:我叫***,今年***岁了。声明类Chinese继承Person
- C++学习笔记--继承中的同名变量和同名函数
- C++如何在类的成员函数中表示同名的全局变量?
- 继承中的同名成员变量处理方法