C++继承

来源:互联网 发布:淘宝虚拟网店要不要钱 编辑:程序博客网 时间:2024/06/08 20:03
#include"iostream"
using namespace std;


//封装 属性和操作放在一个结构体里面
//面向过程处理对象 函数
//面向对象编程 处理对象 类和对象


//继承 代码复用 可以复用以前的代码


class Parent
{
public:
int a;
protected:
int b;
private:
int c;
public:
Parent(int a = 0, int b = 0, int c = 0)
{
this->a = a;
this->b = b;
this->c = c;
}
public:
void Myprint()
{
cout << "我是爹" << endl;
}
};
//class Child :public Parent
//class Child :public Parent
class Child :public Parent
{
public:


//在类的内部不能使用父类的私有成员和函数
int getA()
{
return 0;
}


//公有继承老爹的保护成员
int getB()
{
return b;
}
protected:
private:
};


//C++中类成员的访问级别
//只有和private是否足够?


//用private 修饰的成员函数、属性只能在类的内部使用,不能在类的外部使用
//用procted 修饰的成员函数、属性能在类的内部使用,不能在类的外部使用
//用public  修饰的成员函数、属性既能在类的内部使用,也能在类的外部使用
void main()
{
Parent p1;
p1.Myprint();
Child c1;
c1.Myprint();
//子类从父类中 公有继承 protected修饰的成员函数,属性在类的外部不能使用
c1.a;
//c1.b;
system("pause");
}
原创粉丝点击