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");
}
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");
}
阅读全文
0 0
- c继承
- C++----------------继承
- 【c#】继承
- C++:继承
- C++::继承
- [C++]继承
- 【C++】继承
- 【C#】继承
- 【c++】继承
- 【C++】继承
- c#-继承
- 【C++】 继承
- 【C#】继承
- C++|继承
- 【C++】继承
- C/C++--私有继承
- [C/C++]继承
- c++:私有继承,公有继承,保护继承
- Spring IOC
- 面试题总结
- QT5.8+Opencv3.2+Opencv_contrib-3.2.0环境配置
- JLINK_V8固件烧写教程
- 20、二分查找的实现与应用
- C++继承
- 神经网络
- sqlite3编程
- 设计模式——策略模式及在jdk中的应用
- I
- Android爬坑日记
- javascript中三大弹出框alert(),confirm(), prompt() 的应用和区别
- 网站开发(十五)文章分页技术
- repo/git提交代码