C面向对象程序的设计复习提纲.ppt
文本预览下载声明
;第一章 C++的初步知识;1.4 C++对C的扩充 ;;;作业;第2章 类和对象 ;类定义格式:;;例2.3 类成员的调用;int main()
{
Time t1;
t1.set_time();
t1.show_time();
Time t2;
t2.set_time();
t2.show_time();
return 0;
}; void Time::set_time()
{
cinhour;
cinminute;
cinsec;
}
void Time::show_time()
{
couthour:minute:secendl;
};多文件系统 ;作业 ;第3章 关于类和对象的进一步讨论 ;3.1构造函数 ;3.1.2构造函数的作用 ; #include iostream
using namespace std;
class Box
{public:
Box(); //不带形参的构造函数为系统默认函数
Box(int h,int w ,int len): height(h), width(w), length(len) { }
int volume();
private:
int height;
int width;
int length;
};; #include iostream
using namespace std;
class Box
{public:
Box(int w=10,int h=10,int len=10);
int volume();
private:
int height;
int width;
int length;
};;3.2析构函数 ;3.3调用构造函数和析构函数的顺序 ;作业 ;第四章 运算符重载 ;4.2运算符重载的方法;4.3重载运算符的规则;例4.2;#include iostream.h
class Complex
{public:
Complex () {real=0;imag=0;}
Complex (double r) {real=r;imag=0;}
Complex (double r,double i) {real=r;imag=i;}
friend Complex operator+ (Complex c1, Complex c2); //类的友元函数重载运算符
void display();
private:
double real;
double imag;
};;作业 ;第五章 继承与派生 ;;继承与派生的关系;5.2 派生类的声明方式 ;5.3 派生类的构成 ;5.5.1 简单的派生类的构造函数;例5.5 简单派生类的构造函数; class Student1: public Student // 声明公用派生类
{ public:
Student1(int n,string nam,char s,int a,char ad[ ] ): Student ( n,nam,s) // 派生类构造函数
{ age=a; // 只对派生类新增的数据成员初始化
addr=ad;
}
void show( );
private: // 派生类的私有部分
int age;
string addr;
};;5.6 多重继承 ;5.6.3 多重继承引起的二义性问题;同名隐藏规则;1.虚基类的作用; 类的继承举例1:
class N
{public :
int a;
Void display()
{ cout“N::a=“a;}
};;作业 ;第六章 多态性与虚函数 ;虚函数的作用; (1)在基类用virtual声明成员函数为虚函数。在派生类中重新定义同名函数,让它具有新的功能。
(2)在派生类中重新定义此函数时,要求函数名、函数类型、参数个数和类型与基类的虚函数相同,根据需要重新定义函数体。C++规定,当一个成员函数被声明为虚函数后,其派生类中的同名函数自动成为虚函数。
(3)定义一个指向基类对象的指针变量,并让它获得同一类族中某个对象的地址。
(4)用该指针变量调用虚函数,调用的就是该对象所属类的虚函数。;例:虚函数用法;
显示全部