C++面向对象编程--继承带参数构造函数的类
来源:互联网 发布:第三方isp网络方案 编辑:程序博客网 时间:2024/05/20 14:25
继承带构造函数的类,计算机系统内部需要先实例化基类,就需要先运行基类构造函数,再构造派生类构造函数。
#include <iostream>
using namespace std;
//定义基类
class A{
public:
//声明公共属性
int a;
int x;
public:
A(){
//初始化属性值
this->a=123;
this->x=456;
}
};
//派生类声明
//===============
class B:public A
{
public:
B();//声明派生类构造函数
public:
void mm();
};
//===============
//定义派生类构造函数
B::B():A()
{
this->x=789;
//只初始化一个值
};
//=============
int main()
{ A *p=new A();
B * xx=new B();
cout<<"基类属性"<<endl;
cout<<"a="<<p->a<<endl;
cout<<"x="<<p->x<<endl;
cout<<"派生类属性"<<endl;
cout<<"a="<<xx->a<<endl;
cout<<"x="<<xx->x<<endl;
cout<<"派生类虽然只初始化一个属性值,但是基类把另外一个属性值一起初始化了,说明了继承了基类构造方法"<<endl;
cout << "Hello world!" << endl;
return 0;
#include <iostream>
using namespace std;
//定义基类
class A{
public:
//声明公共属性
int a;
int x;
public:
A(){
//初始化属性值
this->a=123;
this->x=456;
}
};
//派生类声明
//===============
class B:public A
{
public:
B();//声明派生类构造函数
public:
void mm();
};
//===============
//定义派生类构造函数
B::B():A()
{
this->x=789;
//只初始化一个值
};
//=============
int main()
{ A *p=new A();
B * xx=new B();
cout<<"基类属性"<<endl;
cout<<"a="<<p->a<<endl;
cout<<"x="<<p->x<<endl;
cout<<"派生类属性"<<endl;
cout<<"a="<<xx->a<<endl;
cout<<"x="<<xx->x<<endl;
cout<<"派生类虽然只初始化一个属性值,但是基类把另外一个属性值一起初始化了,说明了继承了基类构造方法"<<endl;
cout << "Hello world!" << endl;
return 0;
}
0 0
- C++面向对象编程--继承带参数构造函数的类
- javascript面向对象编程构造函数继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- javascript 面向对象编程非构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- Javascript面向对象编程(二):构造函数的继承
- PHP多进程编程实例
- C++--Qt使用Http协议
- 字符串KMP算法-java
- PDO数据库抽象层实例
- "it could not find or load the Qt platform plugin "windows""解决方法
- C++面向对象编程--继承带参数构造函数的类
- Java语言基础——第四回 Java语言编程规范
- LeetCode[Sort]: Largest Number
- C++--Qt软件开发--自定义信号槽
- 进程提权
- C++ Qt常用类
- 基于php在各种web服务器的运行模式详解
- 蒙皮骨骼动画的理解
- poj 1112 二部图判定+背包(team them up)