}
#include
#include
#include"Cstring.h"
void main()
{
Cstring s1("first hello");
Cstring s2("second hello");
cout<<"赋值之前:"<
s1.display();
cout<<"s2=";
s2.display();
cout<<"赋值之后:"<
cout<<"s1=";
s1.display();
cout<<"s2=";
s2.display();
cout<<"相加之后:"<
}
第11章 继承和派生类
/*1、利用虚基类,消除“两性人”中的冗余数据:姓名、年龄,并编程实现之。*/
class Person
{
public:
Person(char *name,int age);
Person();
virtual ~Person();
protected:
int age;
char name[20];
};
#include "Person.h"
#include
Person::Person(char *name, int age)
{
strcpy(this->name,name);
this->age = age;
}
class Man: virtual public Person
{
public:
Man(char *name,int age,char *sex);
Man();
virtual ~Man();
protected:
char sex[8];
};
#include "Man.h"
#include "Person.h"
#include
Man::Man(char *name,int age,char *sex):Person(name,age)
{
strcpy(this->sex,sex);
}
#include"Person.h"
class Woman: virtual public Person
{
public:
Woman(char *name,int age,char *sex);
Woman();
virtual ~Woman();
protected:
char sex[8];
};
#include "Woman.h"
#include "Person.h"
#include
Woman::Woman(char *name,int age,char *sex):Person(name,age)
{
strcpy(this->sex,sex);
}
class Transexual:public Man,public Woman
{
public:
print();
Transexual(char *name, int age, char *sex1,char *sex2);
Transexual();
virtual ~Transexual();
};
#include "Transexual.h"
#include "Man.h"
#include "Woman.h"
#include "Person.h"
#include
Transexual::Transexual(char *name, int age, char *sex1,char *sex2):Person(name,age),Man(name,age,sex1),Woman(name,age,sex2)
{
}
Transexual::print()
{
cout<<"姓名:"<
/*2、通过Point类派生出Circle和Rectangle类,再通过Circle和Rectangle派生出“足球场图形”类Football,并实例化调用Football这个类,且用cout打印跟踪所有类的构造函数和析构函数的调用过程。(注:下面是这几个类的图形描述,且要求每个类都重载构造函数,且都不用默认构造函数)*/
#include
class Point{
public:
Point(double x){
this->x=x;
cout<<"Point Constructor called"<
~Point(){
cout<<"Point Destructor called"<
protected:
double x;
};
#include
#define PI 3.14159265
class Circle:virtual public Point{
public:
Circle(double x):Point(x){
cout<<"Circle Constructor called"<
~Circle(){
cout<<"Circle Destructor called"<
void setCarea(){
carea=PI*x/2*x/2;
}
double getCarea(){
return carea;
}
protected:
double carea;
};
#include
class Rectangle:virtual public Point{
public:
Rectangle(double x,double y):Point(x){
this->y=y;
cout<<"Rectangle Constructor called"<
~Rectangle(){
cout<<"Rectangle Destructor called"<
void setRarea(){
rarea=x*y;
}
double getRarea(){
return rarea;
}
protected:
double y,rarea;
};
#include
#define PI 3.14159265
class Football:public Circle,public Rectangle{
public:
Football(double x,double y):Point(x),Circle(x),Rectangle(x,y){
cout<<"Football Constructor called"<
~Football(){
cout<<"Football Destructor called"<
void setFarea(){
farea=x*y+PI*x/2*x/2;
}
double getFarea(){
return farea;
}
protected:
double farea;
};
#include
#include"Point.h"
#include"Circle.h"
#include"Rectangle.h"
#include"Football.h"
void main(){
Circle c(10);
Rectangle r(10,20);
Football f(10,20);
c.setCarea();
r.setRarea();
f.setFarea();
cout<<"Circle area:"<
第12章 模板
/*1、利用C++的模板机制定义单向队列类模板、链表类模板,并实例化应用之。*/
/*单向队列类模板*/
#define MAX_SIZE 50
template
class Temqueue
{
private:
T queue[MAX_SIZE];
T front;
T rear;
public:
Temqueue();
virtual ~Temqueue();
bool isEmpty();
bool isFull();
bool push(T);
T pop();
};
template
Temqueue
{
front = 0;
rear = 0;
}
template
Temqueue
{
}
template
bool Temqueue
{
if ((rear-MAX_SIZE) == front)
{
return true;
}
else
{
return false;
}
}
template
bool Temqueue
{
if (rear == front)
{
return true;
}
else
{
return false;
}
}
template
bool Temqueue
{
if (isFull())
{
return true;
}
queue[rear] = e;
rear = ++rear;
return true;
}
template
T Temqueue
{
if (isEmpty())
{
return true;
}
T e = queue[front];
front = ++front;
return e;
}
#include "Temqueue.h"
#include
using namespace std;
void main()
{
int k;
cout<<"请输入要给数列初始化的长度,队列长度为50。"<
Temqueue
for (int i=0 ; i
cQueue.push(i);
}
if(k>0)
{
for (i=0; i
if (i>=MAX_SIZE)
{
cout<<"队列已满!"<
}
else
cout<
cout<
else
cout<<"队列为空!"<
/*链表类模板*/
template
template
public:
Node();
friend class List
private:
T data;
Node
};
template
data=0;
next=NULL;
}
template
public:
List(); //空链表的构造(链表中只含表头结点)
~List(); //析构函数
void MakeEmpty(); //清空链表(删除链表中除表头结点以外的所有结点)
Node
void PrintList(); //输出链表中各结点的数据域
void CreateList(); //初始化链表
private:
Node
};
template
head=rear=new Node
}
template
Node