快考试了,一道C++的题,跪求高手!4:30就考试了,急啊!

定义并实现一个抽象基类CPerson(人类),在此基础上,定义一级派生类,CStudent(学生类)和CTeacher(教师类),然后定义二级派生类CStudentTeacher(学生教师类),要求二级派生是一个多继承类,在类CPerson中增加静态累加器m_sCount,在类CPerson的构造函数中对m_sCount进行累加,在类CPerson中增加静态成员函数ShowNum()用于显示总人数(也就是对象个数),在派生类中增加拷贝构造函数和重载赋值运算符,并注意动态内存的管理问题,编写测试程序进行测试。
190491713 我一脚踹死你!
最新回答
霁无瑕

2024-11-03 06:11:17

#include<iostream.h>

class CPerson
{
public:
CPerson(int k)
{
m_sCount++;
cout<<"CPernson"<<k<<endl;
}
static int m_sCount;
static void ShowNum()
{
cout<<"当前对象个数:"<<m_sCount<<endl;
}
};
int CPerson::m_sCount=0;

class CStudent: public CPerson
{
public:
CStudent():CPerson(0)
{a = 0;}
CStudent(int k):CPerson(k)
{
a= k;
cout<<"CStudent"<<k<<endl;
}
CStudent(CStudent &stu):CPerson(stu.a)
{
a = stu.a;
cout<<"CStudent拷贝构造函数:"<<a<<endl;
}

private:
int a;
};
class CTeacher:public CPerson
{
public:
CTeacher():CPerson(0)
{b = 0;}
CTeacher(int k):CPerson(k)
{
b=k;
cout<<"CTeacher"<<k<<endl;
}
CTeacher(const CTeacher &Tea):CPerson(Tea.b)
{
b = Tea.b;
cout<<"CTeacher拷贝构造函数:"<<b<<endl;
}
CTeacher operator =(const CTeacher &tea);
private:
int b;
};
CTeacher CTeacher::operator=(const CTeacher &tea)
{
cout<<"重载=运算符"<<tea.b<<endl;
return CTeacher(tea.b);
}
class CStudentTeacher:public CStudent,public CTeacher
{
public:
CStudentTeacher(int a,int b,int c,int d):CStudent(a),CTeacher(b)
{
x=a;
y=b;
cout<<"CStudentTeacher"<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
}
CStudentTeacher(const CStudentTeacher &stut):CStudent(stut.x),CTeacher(stut.y)
{
x = stut.x;
y = stut.y;
cout<<"CStudentTeacher拷贝构造函数:"<<x<<endl;
}

private:
int x,y;
};
void main()
{
CStudentTeacher stut(1,2,3,4);
CPerson son(5);
son.ShowNum();

CStudent stu(6);
CStudent stu1(stu);

CTeacher tea(7);
CTeacher tea1;
tea1 = tea;
}
//基本都实现了
给你一口甜甜

2024-11-03 09:31:40

class CPerson
{
public:
CString name;
CString age;
static int m_sCount;
CPerson()
{
m_sCount = 0;
}
static int ShowNum()
{
return m_sCount;
}
};
class CStudent : public CPerson
{
public:
CStudent()
{
CPerson::m_sCount++;
}
CStudent(CStudent& student)
{
this.name = student.name;
this.age = student.age;
}
oprator = (CStudent& student)
{
this.name = student.name;
this.age = student.age;
}
};
class CTeacher :public CPerson
{
public:
CTeacher()
{
CPerson::m_sCount++;
}
CTeacher(CTeacher& teacher)
{
this.name = teacher.name;
this.age = teacher.age;
}
};
class CStudentTeacher : public CStudent,public CTeacher
{
//自己根据需要再写写吧
}
顽皮捣蛋小精灵

2024-11-03 01:41:06

参见
http://zhidao.baidu.com/question/104518981.html
叆叇若紫

2024-11-03 09:15:37

#define PI 3.14
#include<iostream>
//抽象类
class CShape
{
public:
virtual float GetArea() =0 ;//纯虚函数
static int m_sCount ;
CShape()
{
++m_sCount ;
}
static int ShowNum()
{
return m_sCount ;
}
};
class CSquare:public CShape
{
private:
float m_Length;
float m_Height ;

public:

float GetArea()
{
return m_Length * m_Height ;
}

}
class CCircle:public CShape
{
public float GetArea()
{
return PI *r *r ;
}

}
写不动了
山河不入梦

2024-11-03 02:44:47

下面这个程序我运行了一下,还可以吧。如果数据成员没有指针类型的话,也不需要拷贝构造函数;如果有,只要申请个空间,让指针指向它即可。
#include<iostream>
#include<string>
using namespace std;

class CPerson
{
public:
CPerson(){m_sCount++;}
static int ShowNum();
void get();
void put();
protected:
string name;
string sex;
int age;
static int m_sCount;
};
int CPerson::m_sCount=0;
int CPerson::ShowNum(){return m_sCount;}
void CPerson::get(){cin>>name>>sex>>age;return;}
void CPerson::put(){cout<<name<<" "<<sex<<" "<<age;return;}
class CStudent:virtual public CPerson
{
public:
void gets(){cin>>number;return;}
void puts(){cout<<" "<<number;return;}
protected:
string number; //学号
};
class CTeacher: virtual public CPerson
{
public:
void gett(){cin>>subject;return;}
void putt(){cout<<" "<<subject;return;}
protected:
string subject; //教师所教科目
};

class CStudentTeacher:public CStudent,public CTeacher
{
public:
CStudentTeacher():CPerson(){}
friend istream& operator >>(istream&,CStudentTeacher&);
friend ostream& operator <<(ostream&,CStudentTeacher&);
};
istream& operator >>(istream& input,CStudentTeacher& obj)
{ obj.get();obj.gets();obj.gett(); return input;}
ostream& operator <<(ostream& output,CStudentTeacher& obj)
{ obj.put();obj.puts();obj.putt(); return output;}

int main()
{
CStudentTeacher obj;
cin>>obj;
cout<<obj.ShowNum()<<endl;
cout<<obj;
return 0;
}
っ麦↘兜兜

2024-11-03 02:14:16

完全按原题要求编写的程序如下,dev-c++和vs2005下编译通过。
#include <iostream>
using namespace std;

const double PI=3.1415926;

class CPerson{//定义抽象类
public:
static int m_sCount;//定义静态累加器
virtual double Getdata()=0;//声明纯虚函数
CPerson()
{
m_sCount++;
}
~CPerson()
{
m_sCount--;
}
void static ShowNum(){//定义静态成员函数
cout<<endl<<"There are "<<m_sCount<<" person(s)"<<endl;
}
};

int CPerson::m_sCount = 0;//静态数据成员初始化

class CStudent:public CPerson{//公用继承
public:
CStudent(double Cost=0.0):cost(Cost){};
CStudent(CStudent &s)//定义拷贝构造函数
{
cost=s.cost;
}
void operator=(CStudent &s)//重载赋值运算符
{
cost=s.cost;
}
double Getdata()//重新定义基类中的纯虚函数
{
return cost;
}
protected:
double cost;
};

class CTeacher:public CPerson{
public:
CTeacher(double Page=0.0):page(Page){};
CTeacher(CTeacher &c)//定义拷贝构造函数
{
page=c.page;
}
void operator=(CTeacher &c)//重载赋值运算符
{
page=c.page;
}
double Getdata()//重新定义基类中的纯虚函数
{
return page;
}
protected:
double page;
};

class CStudentTeacher: public CStudent, public CTeacher{//多重继承
public:
CStudentTeacher(double Cost, double Page,int Age):CStudent(Cost),CTeacher(Page),age(Age){};
CStudentTeacher(CStudentTeacher &c)//定义拷贝构造函数
{
age=c.age;
cost=c.cost;
page=c.page;
}
void operator=(CStudentTeacher &c)//重载赋值运算符
{
age=c.age;
cost=c.cost;
page=c.page;
}
void Showdata()
{
cout<<"Age and Cost and Page per month of new person is "<<age<<" "<<cost<<" "<<page<<endl;
}
private:
int age;
};

int main()
{
CStudent s1(600.0);
s1.ShowNum();
cout<<"Cost per month of new person is "<<s1.Getdata()<<endl;
CStudent s2(s1);
s2.ShowNum();
cout<<"Cost per month of new person is "<<s2.Getdata()<<endl;
CStudent s3=s1;
s3.ShowNum();
cout<<"Cost per month of new person is "<<s3.Getdata()<<endl;
CTeacher c1(1500.0);
c1.ShowNum();
cout<<"Page per month of new person is "<<c1.Getdata()<<endl;
CTeacher c2(c1);
c2.ShowNum();
cout<<"Page per month of new person is "<<c2.Getdata()<<endl;
CTeacher c3=c1;
c3.ShowNum();
cout<<"Page per month of new person is "<<c3.Getdata()<<endl;
CStudentTeacher s4(500.0,1000.0,25);
CPerson::m_sCount--;
s4.ShowNum();
s4.Showdata();
CStudentTeacher s5(s4);
CPerson::m_sCount--;
s5.ShowNum();
s5.Showdata();
CStudentTeacher s6=s4;
CPerson::m_sCount--;
s6.ShowNum();
s6.Showdata();

system("pause");
return 0;
}