2.1面向对象程序设计基础(3)
来源:互联网 发布:淘宝小二网站 编辑:程序博客网 时间:2024/06/08 09:03
1.派生类的定义
格式:
class 派生类名:[继承方式]基类名{ 派生类新增的成员;}
继承方式:private public protected
注意:派生类继承基类的成员函数,但不继承构造函数
2.派生类构造函数的定义
派生类构造函数名(总参数列表):基类构造函数名(参数列表){派生类中新增成员变量初始化语句}
注意:若没有基类构造函数,则按默认构造函数初始化基类的变量
例子:
#include <iostream>using namespace std;class CRectangle {public: CRectangle(int width,int height); ~CRectangle(); double circum(); double area();protected: int width; int height;};CRectangle::CRectangle(int width,int height){ this->width=width; this->height=height; cout<<"create object"<<endl;}CRectangle::~CRectangle(){ cout<<"delete object"<<endl;}double CRectangle::circum(){ return 2*(width+height);}double CRectangle::area(){ return width*height;}class CCuboid:public CRectangle{public: CCuboid(int width,int height,int length); //包括基类的成员变量,新增变量length ~CCuboid(); double volume();private: int length;};CCuboid::CCuboid(int widht,int height,int length):CRectangle(int width,int height){ //派生类构造函数,调用基类构造函数 this->length=length; cout<<"create new object"<<endl;}CCuboid::~CCuboid(){ cout<<"delete the new object"<<endl;}double CCuboid::volume(){ return width*height*length;}void main (){ CCuboid *pCuboid=new CCuboid(30,20,100);//动态开辟空间 cout<<"the Cuboid's volume is :"<<pCuboid->volume()<<endl; delete pCuboid; pCuboid=NULL;//pCuboid 未被消除,消除的是它指向的空间}
阅读全文
0 0
- 2.1面向对象程序设计基础(3)
- 2.1面向对象程序设计基础(2)
- 2.1面向对象程序设计基础
- 面向对象程序设计基础
- java基础---面向对象程序设计
- C++基础---面向对象程序设计
- 八、面向对象程序设计基础
- 面向对象程序设计基础总结
- android基础篇------------java基础(4) (面向对象程序设计)
- 面向对象的程序设计(3)
- Java复习总结:Java面向对象程序设计 —面向对象(基础篇)
- 面向对象程序设计简单基础概述
- 【Linux基础】面向对象程序设计的特点
- Java基础复习---面向对象程序设计
- 面向对象(OO)程序设计
- 面向对象(OO)程序设计
- 程序设计基础——面向对象程序设计思想
- 面向对象(面向基础)
- classpath: 和classpath*:的区别
- 键盘布局背后的原理
- android studio 报错: Gradle DSL method not found: 'Android()'
- Java的三个体系—JAVASE,JAVAEE,JAVAME
- 乱搞(吧?)——Codeforces814C An impassioned circulation of affection
- 2.1面向对象程序设计基础(3)
- 关于JAVA项目中CLASSPATH路径详解
- ASP.NET的六大内置对象
- 刷清橙OJ--A1057.FJ的字符串
- eclipse中的.project 和 .classpath文件的具体作用
- C++标准库笔记:13.12.1 实作一个Output操作符
- 面试题18. 树的子结构
- 第二行代码第四章笔记
- 【如何校验邮件地址的有效性】第三篇 :原理二