求继承与虚函数代码
一、分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Techer_Cadre(教师兼干部)。要求:
1.在两个基类中,都包含姓名,年龄,性别,地址,电话等数据成员。
2.在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Techer_Cadre中还包含数据成员wages(工资)。
3.对两个基类中的姓名,年龄,性别,地址,电话等数据成员用相同的名字,在引用这些数据成员的时候指定作用域。
4.在类中声明成员函数,在类外定义成员函数
5.在派生类Techer_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名,年龄,性别,职称,地址,电话,然后再用cout语句输出职务与工资。
二、声明抽象基类Shape,由它派生出3个派生类:Circle(圆形)、Rectangle(矩形类)、Triangle(三角形),
1.在基类中声明area()为纯虚函数。
2.为每个派生类定义其自己的构造函数、析构函数,并对基类中的area函数进行重写,分别求出3种图形的面积。
3.用一个全局函数printArea分别输出以上三者的面积。
---------------------------------------------------------------------------
刚刚学习C++ 读程序尚可 可要是自己动手 真的下不去手
恳请各位高手指点以上两题!
问题点数:50、回复次数:18Top
1 楼todototry(来csdn,学会扯淡了...)回复于 2006-12-01 13:51:47 得分 50
// 二、声明抽象基类Shape,由它派生出3个派生类:Circle(圆形)、Rectangle(矩形类)、Triangle(三角形),
// 1.在基类中声明area()为纯虚函数。
// 2.为每个派生类定义其自己的构造函数、析构函数,并对基类中的area函数进行重写,分别求出3种图形的面积。
// 3.用一个全局函数printArea分别输出以上三者的面积。
#include <iostream>
using namespace std;
class Shape
{
public:
virtual double area() = 0;
virtual ~Shape(){};
};
class Circle: public Shape
{
public:
Circle(double r = defaultR)
{
r_size = r;
}
double area()
{
return PI * r_size * r_size;
}
~Circle(){};
private:
double r_size;
static const double defaultR;
static const double PI;
};
const double Circle::defaultR = 1;
const double Circle::PI = 3.14;
class Rectangle: public Shape
{
public:
Rectangle(double l = defaultL, double w = defaultW)
{
length = l;
width = w;
}
double area()
{
return length * width;
}
~Rectangle(){};
private:
double length;
double width;
static const double defaultL;
static const double defaultW;
};
const double Rectangle::defaultL = 1;
const double Rectangle::defaultW = 1;
class Trinangle: public Shape
{
public:
Trinangle(double b = defaultB, double h = defaultH)
{
bottom = b;
height = h;
}
double area()
{
return bottom * height / 2;
}
~Trinangle(){};
private:
double bottom;
double height;
static const double defaultB;
static const double defaultH;
};
const double Trinangle::defaultB = 1;
const double Trinangle::defaultH = 1;
void PrintArea(Shape & rs)
{
cout << "the area is: " << rs.area() << endl;
}
double main()
{
Circle c;
Rectangle r;
Trinangle t;
PrintArea(c);
PrintArea(r);
PrintArea(t);
return 0;
}Top
2 楼weiyiabout()回复于 2006-12-01 14:22:18 得分 0
作业题吧?我好象都做过和这个很类似的Top
3 楼cunsh(村少)回复于 2006-12-01 14:50:44 得分 0
double main()
======================
哈哈哈Top
4 楼greenteanet(扎扎实实打基础,保持一颗平常心。)回复于 2006-12-01 14:51:26 得分 0
double main()
-------------
嘻嘻。搞笑。。。Top
5 楼todototry(来csdn,学会扯淡了...)回复于 2006-12-01 14:53:19 得分 0
不好意思,替换来着,呵呵
搞笑啊Top
6 楼todototry(来csdn,学会扯淡了...)回复于 2006-12-01 14:54:25 得分 0
int main()
声明了啊呵呵
没注意,大伙见笑了啊Top
7 楼todototry(来csdn,学会扯淡了...)回复于 2006-12-01 20:42:51 得分 0
// 一、分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Techer_Cadre(教师兼干部)。要求:
// 1.在两个基类中,都包含姓名,年龄,性别,地址,电话等数据成员。
// 2.在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Techer_Cadre中还包含数据成员wages(工资)。
// 3.对两个基类中的姓名,年龄,性别,地址,电话等数据成员用相同的名字,在引用这些数据成员的时候指定作用域。
// 4.在类中声明成员函数,在类外定义成员函数
// 5.在派生类Techer_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名,年龄,性别,职称,地址,电话,然后再用cout语句输出职务与工资。
#include <iostream>
#include <string>
using namespace std;
class Teacher
{
public:
virtual ~Teacher();
Teacher(string n = defaultname,
int a = defaultage,
string s = defaultsex,
string ad = defaultaddress,
int p = defaultphonenumber,
string t = defaulttitle);
void disply();
private:
string name;
int age;
string sex;
string address;
int phonenumber;
string title;
static const string defaultname;
static const int defaultage;
static const string defaultsex;
static const string defaultaddress;
static const int defaultphonenumber;
static const string defaulttitle;
};
Teacher::~Teacher()
{
}
Teacher::Teacher(string n, int a, string s,string ad, int p, string t)
{
name = n;
age = a;
sex = s;
address = ad;
phonenumber = p;
title = t;
}
void Teacher::disply()
{
cout << name << " " << age << " " << sex << " "
<< address << " " << phonenumber << " " << title << endl;
}
const string Teacher::defaultname = "ibm";
const int Teacher::defaultage = 1;
const string Teacher::defaultsex = "male";
const string Teacher::defaultaddress = "here";
const int Teacher::defaultphonenumber = 0;
const string Teacher::defaulttitle = "teacher";
//////////////////////////////////////////////////////////////////////
class Cadre
{
public:
virtual ~Cadre();
Cadre(string n = defaultname,
int a = defaultage,
string s = defaultsex,
string ad = defaultaddress,
int p = defaultphonenumber,
string po = defaultpost);
private:
string name;
int age;
string sex;
string address;
int phonenumber;
string post;
static const string defaultname;
static const int defaultage;
static const string defaultsex;
static const string defaultaddress;
static const int defaultphonenumber;
static const string defaultpost;
};
const string Cadre::defaultname = "microsoft";
const int Cadre::defaultage = 1;
const string Cadre::defaultsex = "male";
const string Cadre::defaultaddress = "here";
const int Cadre::defaultphonenumber = 0;
const string Cadre::defaultpost = "cadre";
Cadre::~Cadre()
{
}
Cadre::Cadre(string n, int a, string s,string ad, int p, string po)
{
name = n;
age = a;
sex = s;
address = ad;
phonenumber = p;
post = po;
}
///////////////////////////////////////////////////////////////////
class Techer_Cadre: public Teacher, Cadre
{
public:
Techer_Cadre(int w = defaultwages);
~Techer_Cadre();
void show();
private:
int wages;
static const int defaultwages;
};
const int Techer_Cadre::defaultwages = 10000;
Techer_Cadre::Techer_Cadre(int w)
{
wages = w;
}
Techer_Cadre::~Techer_Cadre()
{
}
void Techer_Cadre::show()
{
Teacher::disply();
cout << wages << endl;
}
/////////////////////////////////////////////////////////////////
int main()
{
Techer_Cadre tc;
tc.show();
return 0;
}Top
8 楼epautumn()回复于 2006-12-02 11:55:45 得分 0
谢谢各位! 我才刚刚开始学习,只是怎么都下不去手。
顺便问一下 我提问的点数怎么才能给这些回复的朋友?Top
9 楼todototry(来csdn,学会扯淡了...)回复于 2006-12-02 12:44:46 得分 0
点右下角的管理,然后给分,输入密码即可Top
10 楼OOPhaisky(异化$渴望成功~~)回复于 2006-12-02 12:44:59 得分 0
double main()
======================
哈哈哈
村少真是慧眼识英才阿,伯乐也^_^Top
11 楼jimmy212(dear jim)回复于 2006-12-02 17:49:52 得分 0
我可以问为什么要笑 double main()吗Top
12 楼believefym(feng)回复于 2006-12-02 17:56:41 得分 0
int main()
返回的是给系统的,比如一般0表示成功,哪有double当main返回值的Top
13 楼Robjuan()回复于 2006-12-02 17:56:49 得分 0
main 函数的返回值类型必须是intTop
14 楼todototry(来csdn,学会扯淡了...)回复于 2006-12-02 18:50:18 得分 0
大伙还笑兄弟的啊,呵呵
当时把个类改写的,替换int成double
所以呢没在意Top
15 楼aronzhou()回复于 2006-12-02 19:14:31 得分 0
to: todototry
你的类设计的不好,
你可以给teacher和干部设计一个person的虚基类里面有姓名、住址等
teacher和干部虚继承person类
那样设计比较合理Top
16 楼todototry(来csdn,学会扯淡了...)回复于 2006-12-02 20:13:48 得分 0
题目那样说的Top
17 楼todototry(来csdn,学会扯淡了...)回复于 2006-12-02 20:14:11 得分 0
1.在两个基类中,都包含姓名,年龄,性别,地址,电话等数据成员Top
18 楼todototry(来csdn,学会扯淡了...)回复于 2006-12-02 20:14:25 得分 0
3.对两个基类中的姓名,年龄,性别,地址,电话等数据成员用相同的名字,在引用这些数据成员的时候指定作用域。Top





