注:每个章节的代码均是独立的,测试时将每一节的代码复制粘贴即可
2-2、虚函数代码示例
//Shape2_2.h
#ifndef SHAPE_H
#define SHAPE_H
#include<iostream>
using namespace std;
class Shape
{
public:
Shape();
~Shape();
virtual double calcArea();
};
#endif //SHAPE_H
//Rect2_2.h
#ifndef RECT_H
#define RECT_H
#include"Shape2_2.h"
class Rect:public Shape
{
public:
Rect(double width,double height);
~Rect();
double calcArea();
protected:
double m_dWidth;
double m_dHeight;
};
#endif //RECT_H
//Circle2_2.h
#ifndef CIRCLE_H
#define CIRCLE_H
#include"Shape2_2.h"
class Circle:public Shape
{
public:
Circle(double r);
~Circle();
double calcArea();
protected:
double m_dR;
};
#endif //CIRCLE_H
//Shape2_2.cpp
#include"Shape2_2.h"
#include<iostream>
using namespace std;
Shape::Shape()
{
cout<<"Shape()"<<endl;
}
Shape::~Shape()
{
cout<<"~Shape()"<<endl;
}
double Shape::calcArea()
{
cout<<"Shape--calcArea"<<endl;
return 0;
}
//Rect2_2.cpp
#include"Rect2_2.h"
#include<iostream>
#include<string>
using namespace std;
Rect::Rect(double width,double height)
{
cout<<"Rect()"<<endl;
m_dHeight = height;
m_dWidth = width;
}
Rect::~Rect()
{
cout<<"~Rect()"<<endl;
}
double Rect::calcArea()
{
cout<<"Rect--calcArea()"<<endl;
return m_dWidth*m_dHeight;
}
//Circle2_2.cpp
#include<iostream>
#include"Circle2_2.h"
using namespace std;
Circle::Circle(double r)
{
cout<<"Circle()"<<endl;
m_dR = r;
}
Circle::~Circle()
{
cout<<"~Circle()"<<endl;
}
double Circle::calcArea()
{
cout<<"Circle--calcArea()"<<endl;
return 3.14*m_dR*m_dR;
}
//main文件
//2-2虚函数代码示例#include<iostream>
#include<stdlib.h>
#include"Circle2_2.h"
#include"Rect2_2.h"
using namespace std;
/*********************************/
/*
动态多态、虚函数
要求:
1.定义Shape类,成员函数:calcArea(),构造函数,析构函数
2.定义Rect类,成员函数:calcArea(),构造函数,析构函数
数据成员:m_dWidth,m_dHeight
3.定义Circle类,成员函数:calcArea(),构造函数,析构函数
数据成员:m_dR
/*********************************/
int main(void)
{
Shape *shape1 = new Rect(3,6);
Shape *shape2 = new Circle(5);
shape1->calcArea();
shape2->calcArea();
delete shape1;
shape1 = NULL;
delete shape2;
shape2 = NULL;
system("pause");
return 0;
}
2-5、虚析构函数代码示例
//Shape2_5.h
#ifndef SHAPE_H
#define SHAPE_H
#include<iostream>
using namespace std;
class Shape
{
public:
// virtual Shapr(); //错误,构造函数不能是虚函数
Shape();
/*
virtual static void test() //错误,virtual关键字不能加在静态成员函数前
{
}
*/
/* virtual inline void test() //无语法错误,但此时inline关键字则失效
{
}
*/
virtual ~Shape();
virtual double calcArea();
};
#endif //SHAPE_H
//Rect2_5.h
#ifndef RECT_H
#define RECT_H
#include"Shape2_5.h"
class Rect:public Shape
{
public:
Rect(double width,double height);
virtual ~Rect();
double calcArea();
protected:
double m_dWidth;
double m_dHeight;
};
#endif //RECT_H
//Circle2_5.h
ifndef CIRCLE_H
#define CIRCLE_H
#include"Shape2_5.h"
#include"Coordinate2_5.h"
class Circle:public Shape
{
public:
Circle(double r);
virtual ~Circle();
double calcArea();
protected:
double m_dR;
Coordinate *m_pCenter;
};
#endif //CIRCLE_H
//Coordinate2_5.h
#ifndef COORDINATE_H
#define COORDINATE_H
class Coordinate
{
public:
Coordinate(int x,int y);
~Coordinate();
protected:
int m_iX;
int m_iY;
};
#endif //COORDINATE_H
//Shape2_5.cpp
#include"Shape2_5.h"
#include<iostream>
using namespace std;
Shape::Shape()
{
cout<<"Shape()"<<endl;
}
Shape::~Shape()
{
cout<<"~Shape()"<<endl;
}
double Shape::calcArea()
{
cout<<"Shape--calcArea"<<endl;
return 0;
}
//Rect2_5.cpp
#include"Rect2_5.h"
#include<iostream>
#include<string>
using namespace std;
Rect::Rect(double width,double height)
{
cout<<"Rect()"<<endl;
m_dHeight = height;
m_dWidth = width;
}
Rect::~Rect()
{
cout<<"~Rect()"<<endl;
}
double Rect::calcArea()
{
cout<<"Rect--calcArea()"<<endl;
return m_dWidth*m_dHeight;
}
//Circle2_5.cpp
#include<iostream>
#include"Circle2_5.h"
using namespace std;
Circle::Circle(double r)
{
cout<<"Circle()"<<endl;
m_dR = r;
m_pCenter = new Coordinate(3,5);
}
Circle::~Circle()
{
cout<<"~Circle()"<<endl;
//释放内存
delete m_pCenter;
m_pCenter = NULL;
}
double Circle::calcArea()
{
cout<<"Circle--calcArea()"<<endl;
return 3.14*m_dR*m_dR;
}
//Coordinate2_5.cpp
#include"Coordinate2_5.h"
#include<iostream>
using namespace std;
Coordinate::Coordinate(int x,int y)
{
cout<<"Coordinate()"<<endl;
m_iX = x;
m_iY = y;
}
Coordinate::~Coordinate()
{
cout<<"~Coordinate()"<<endl;
}
//main文件
#include<iostream>
#include<stdlib.h>
#include"Circle2_5.h"
//2-5虚析构函数代码示例
/*
虚析构函数是为了防止内存泄露
*/
#include"Rect2_5.h"
using namespace std;
/*********************************/
/*
动态多态、虚函数
要求:
1.定义Shape类,成员函数:calcArea(),构造函数,析构函数
2.定义Rect类,成员函数:calcArea(),构造函数,析构函数
数据成员:m_dWidth,m_dHeight
3.定义Circle类,成员函数:calcArea(),构造函数,析构函数
数据成员:m_dR,m_pCenter
4.定义Coordinate类,成员函数:构造函数,析构函数
数据成员:m_iX,m_iY
/*********************************/
/*
示例函数
//virtual void test() //错误,virtual关键字不能加在普通函数前
void test()
{
}
*/
int main(void)
{
Shape *shape1 = new Rect(3,6);
Shape *shape2 = new Circle(5);
shape1->calcArea();
shape2->calcArea();
delete shape1;
shape1 = NULL;
delete shape2;
shape2 = NULL;
system("pause");
return 0;
}
2-8~2-9、虚函数表示例
//Shape2_2.h
#ifndef SHAPE_H
#define SHAPE_H
#include<iostream>
using namespace std;
class Shape
{
public:
Shape();
//~Shape();
double calcArea();
virtual ~Shape();
//virtual double calcArea();
};
#endif //SHAPE_H
//Circle2_8.h
#ifndef CIRCLE_H
#define CIRCLE_H
#include"Shape2_8.h"
class Circle:public Shape
{
public:
Circle(int r);
~Circle();
// double calcArea();
protected:
int m_iR;
};
#endif //CIRCLE_H
//Shape2_8.cpp
#include"Shape2_8.h"
#include<iostream>
using namespace std;
Shape::Shape()
{
// cout<<"Shape()"<<endl;
}
Shape::~Shape()
{
// cout<<"~Shape()"<<endl;
}
double Shape::calcArea()
{
cout<<"Shape--calcArea"<<endl;
return 0;
}
//Circle2_8.cpp
#include<iostream>
#include"Circle2_8.h"
using namespace std;
Circle::Circle(int r)
{
// cout<<"Circle()"<<endl;
m_iR = r;
}
Circle::~Circle()
{
// cout<<"~Circle()"<<endl;
}
//main文件
//2-8~2-9虚函数表示例
#include<iostream>
#include<stdlib.h>
#include"Circle2_8.h"
using namespace std;
/*********************************/
/*
虚函数表
要求:
1.定义Shape类,成员函数:calcArea(),构造函数,析构函数
3.定义Circle类,成员函数:构造函数,析构函数
数据成员:m_iR
概念说明:
1.对象的大小
2.对象的地址
3.对象成员的地址
4.虚函数表指针
/*********************************/
int main(void)
{
Shape shape;
//cout<<sizeof(shape)<<endl; //没有数据成员的对象默认占用1个存储单元
int *p =(int *)&shape;
//cout<<p<<endl;
cout<<(unsigned int)(*p)<<endl;
Circle circle(100);
// cout<<sizeof(circle)<<endl;
int *q = (int *)&circle;
// cout<<q<<endl;
cout<<(unsigned int)(*q)<<endl;
q++;
cout<<(unsigned int)(*q)<<endl;
system("pause");
return 0;
}
3-2、抽象类代码示例
//Person3_2.h
#ifndef PERSON_H
#define PERSON_H
#include<string>
using namespace std;
class Person
{
public:
Person(string name);
virtual ~Person();
virtual void work() = 0; //纯虚函数,有纯虚函数的类不能实例化,必须实现该函数才能实例化
private:
string m_strName;
};
#endif //PERSON_H
//Worker3_2.h
#ifndef WORKER_H
#define WORKER_H
#include"Person3_2.h"
#include<string>
class Worker:public Person
{
public:
Worker(string name,int age);
// virtual void work();
private:
int m_iAge;
};
#endif //WORK_H
//Dustman3_2.h
#ifndef DUSTMAN_H
#define DUSTMAN_H
#include"Work3_2.h"
#include<iostream>
using namespace std;
class Dustman:public Worker
{
public:
Dustman(string name,int age);
virtual void work();
};
#endif //DUSTMAN_H
//Person3_2.cpp
#include"Person3_2.h"
#include<iostream>
#include<string>
using namespace std;
Person::Person(string name)
{
m_strName = name;
// cout<<"Person()"<<endl;
}
Person::~Person()
{
// cout<<"~Rect()"<<endl;
}
//Worker3_2.cpp
#include"Work3_2.h"
#include<iostream>
#include<string>
using namespace std;
Worker::Worker(string name,int age):Person(name)
{
m_iAge = age;
cout<<"Worker()"<<endl;
}
/*void Worker::work()
{
cout<<"Worker--work()"<<endl;
}*/
//Dustman3_2.cpp
#include"Dustman3_2.h"
#include<iostream>
using namespace std;
Dustman::Dustman(string name,int age):Worker(name,age)
{
cout<<"Dustman()"<<endl;
}
void Dustman::work()
{
cout<<"Dustman--work()"<<endl;
}
//main文件
//3-2抽象类代码示例
#include<iostream>
#include<stdlib.h>
#include"Dustman3_2.h"
#include"Person3_2.h"
#include"Work3_2.h"
using namespace std;
/*********************************/
/*
纯虚函数抽象类:
要求:
1.定义Person类,成员函数:构造函数,虚析构函数,纯虚函数work(),数据成员:名字m_strName
2.定义Worker类,成员函数:构造函数,work(),数据成员:年龄m_iAge
2.定义Dustman类,成员函数:构造函数,work(),
/*********************************/
int main(void)
{
// Person person("CJ"); //错误,Person是含有纯虚函数的抽象类,不能实例化
// Worker worker("Merry",20); //若Worker类继承了Person类并且Worker类中没有实现继承自Person中的纯虚函数时,//此时Worker类也是抽象类,不能实例化
Dustman dustman("Merry",20);
system("pause");
return 0;
}
3-6、接口类代码示例
//Flyable3_6.h
//3_6接口类代码示例
#ifndef FLYABLE_H
#define FLYABLE_H
//只有纯虚函数的类不需要.cpp文件
class Flyable
{
public:
virtual void takeoff() = 0;
virtual void land() = 0;
};
#endif //FLYABLE_H
//Plane3_6.h
#ifndef PLANE_H
#define PLANE_H
#include"Flyable3_6.h"
#include<string>
using namespace std;
//class Plane:public Flyable
class Plane
{
public:
Plane(string code);
// virtual void takeoff();
// virtual void land();
void printCode();
protected:
string m_strCode;
};
#endif //PLANE_H
//FighterPlane3_6.h
#ifndef FIGHTERPLANE_H
#define FIGHTERPLANE_H
#include"Plane3_6.h"
#include<string>
using namespace std;
//class FighterPlane:public Plane
class FighterPlane:public Plane,public Flyable
{
public:
FighterPlane(string code);
virtual void takeoff();
virtual void land();
};
#endif //FIGHTERPLANE_H
//Flyable3_6.cpp(此文件可不写)
#include<iostream>
#include"Flyable3_6.h"
using namespace std;
//Plane3_6.cpp
#include<iostream>
#include"Plane3_6.h"
using namespace std;
Plane::Plane(string code)
{
m_strCode = code;
// cout<<"Plane()"<<endl;
}
/*
void Plane::takeoff()
{
cout<<"Plane--takeoff"<<endl;
}
void Plane::land()
{
cout<<"Plane--land()"<<endl;
}
*/
void Plane::printCode()
{
cout<<m_strCode<<endl;
}
//FighterPlane3_6.cpp
#include<iostream>
#include"FighterPlane3_6.h"
using namespace std;
FighterPlane::FighterPlane(string code):Plane(code)
{
}
void FighterPlane::takeoff()
{
cout<<"FighterPlane()--takeoff()"<<endl;
}
void FighterPlane::land()
{
cout<<"FightPlane--land()"<<endl;
}
//main文件
//3-6接口类代码示例
#include<iostream>
#include<stdlib.h>
#include"FighterPlane3_6.h"
using namespace std;
/*********************************/
/*
接口类:
要求:
1.定义Flyable类,成员函数:takeoff(),land()
2.定义Plane类,成员函数:构造函数,takeoff(),land(),printCode(),数据成员:m_strCode
3.定义FighterPlane类,成员函数:构造函数,takeoff(),land(),
4.全局函数flyMatch(Flyable *f1,Flyable *f2)
/*********************************/
void flyMatch(Flyable *f1,Flyable *f2)
{
f1->takeoff();
f1->land();
f2->takeoff();
f2->land();
}
int main(void)
{
FighterPlane p1("001");
FighterPlane p2("002");
p1.printCode();
p2.printCode();
flyMatch(&p1,&p2);
system("pause");
return 0;
}
4-2、RTTI代码示例(未能成功运行)
//Flyable4_2.h
//4-2RTTI
#ifndef FLYABLE_H
#define FLYABLE_H
//只有纯虚函数的类不需要.cpp文件
class Flyable
{
public:
virtual void takeoff() = 0;
virtual void land() = 0;
};
#endif //FLYABLE_H
//Plane4_2.h
#ifndef PLANE_H
#define PLANE_H
#include"Flyable4_2.h"
#include<string>
using namespace std;
class Plane:public Flyable
{
public:
virtual void takeoff();
virtual void land();
void carry();
};
#endif //PLANE_H
//Bird4_2.h
#ifndef BIRD_H
#define BIRD_H
#include<iostream>
#include<string>
#include"Flyable4_2.h"
using namespace std;
class Bird:public Flyable
{
public:
virtual void takeoff();
virtual void land();
void foraging();
};
#endif //BIRD_H
//Flyable4_2.cpp(此文件可不写)
#include<iostream>
#include"Flyable4_2.h"
using namespace std;
//Plane4_2.cpp
#include<iostream>
#include"Plane4_2.h"
using namespace std;
void Plane::takeoff()
{
cout<<"Plane--takeoff"<<endl;
}
void Plane::land()
{
cout<<"Plane--land()"<<endl;
}
void Plane::carry()
{
cout<<"Plane--carry()"<<endl;
}
//Bird4_2.cpp
#include"Bird4_2.h"
#include<iostream>
using namespace std;
void Bird::takeoff()
{
cout<<"Bird--takeoff()"<<endl;
}
void Bird::land()
{
cout<<"Bird~land()"<<endl;
}
void Bird::foraging()
{
cout<<"Bird--foraging"<<endl;
}
//main文件
//4-2RTTI代码示例(未能成功运行)
#include<iostream>
#include<stdlib.h>
#include"Bird4_2.h"
#include"Plane4_2.h"
#include<typeinfo>
using namespace std;
/*********************************/
/*
RTTI:
要求:
1.定义Flyable类,成员函数:takeoff(),land()
2.定义Plane类,成员函数:takeoff(),land(),carry()
3.定义Bird类,成员函数:takeoff(),land(),foraging()
4.全局函数doSomething(Flyable *obj)
/*********************************/
void doSomething(Flyable *obj)
{
cout<<typeid(*obj).name()<<endl; //打印obj的数据 类型
obj->takeoff();
if(typeid(*obj)==typeid(Bird)) //如果当前指针指向的obj的类型为Bird
{
Bird *bird = dynamic_cast<Bird *>(obj); //若obj为Bird类型的指针,
//则将obj转化为Bird的指针并赋值给Bird类型的bird
bird->foraging();
}
if(typeid(*obj) == typeid(Plane))
{
Plane *plane = dynamic_cast<Plane *>(obj);
plane->carry();
}
obj->land();
}
int main(void)
{
int i = 0;
cout<<typeid(i).name()<<endl;
system("pause");
return 0;
}
5-2、异常处理(未成功运行)
//Exception5_2.h
#ifndef EXCEPTION_H
#define EXCEPTION_H
class Exception
{
public:
virtual ~Exception();
virtual void printException();
};
#endif //EXCEPTION_H
//IndexException5_2.h
#ifndef INDEXECEPTION_H
#define INDEXECEPTION_H
#include"Exception5_2.h"
class IndexException:public Exception
{
public:
virtual void printException();
};
#endif //INDEXECEPTION_H
//Exception5_2.cpp
#include"Exception5_2.h"
#include<iostream>
#include<string>
using namespace std;
Exception::~Exception()
{
}
void Exception::printException()
{
cout<<"Exception--printException"<<endl;
}
//IndexException5_2.cpp
#include"IndexException5_2.h"
#include<iostream>
#include<string>
using namespace std;
void IndexException::printException()
{
cout<<"提示:下标越界。"<<endl;
}
//main文件
#include<iostream>
#include<string>
#include<stdlib.h>
#include"IndexException5_2.h"
using namespace std;
/*************************************/
/*异常处理:
1.定义一个Exception类,成员函数:printException,析构函数
2.定义一个IndexException类,成员函数:printException()
/*************************************/
void test()
{
throw 10;
}
int main(void)
{
try
{
test();
}
catch(int)
{
cout<<"Exception"<<endl;
}
/* catch(CMemoryException* e)
{
}
catch(CFileException* e)
{
}
catch(CException* e)
{
} */
system("pause");
return 0;
}