int a,b;}
class B:private A
}
a+=x; b+=y; }
{
public:
B(int I,int j,int k,int l):A(I,j) { x=k; y=l; }
void Display() { cout<void Show() { A::Display(); }private:
int x,y;
};
void main()
{
A e(1,2);
e.Display();
B d(3,4,5,6);
d.Move();
d.Display();
d,Show();
}
运行结果:(1,2)
5,6
(6,9)
程序2:
#includeclass CRoot
{
public:
int small;
CRoot() { small=2; }
CRoot(int n;)
void showsmall() { cout<<”small=”<};
class CDer1:public CRoot
{
public:
CDer(int m):CRoot(m0 {}
};
class CDer2:public CRoot
{
public:
int small;
CRoot2(int n=0) { small=n; }
};
void main()
{
CRoot A;
CDer1 B(3);
CDer2 C;
A.showsmall(); }
运行结果:small=2
small=3 small=2C.showsmall();
B.showsmall();
26、定义一个人员类CPerson,包括市局成员:姓名、编号、性别和用于输入/输出的成员函数。在此基础上派生出学生类CStudent(增加成绩)和教师类CTeacher(增加教龄),并实现对学生和教师信息的输入/输出。
#include #include using namespace std;
class CPerson
{
public:
void set(string na,string num,string gen)
{
name=na;
number=num;
gender=gen;
}
void display()
{
cout << name << \" \" << number << \" \" << gender << \" \";
}
private:
string name;
string number;
string gender;
};
class CStudent: public CPerson
{
public:
void set_score(int a)
{
score = a;
}
void display()
{
CPerson::display();
cout << score;
}
private:
int score;
};
class CTeacher:public CPerson
{
public:
void set_school_age(int a)
{
of_school_age = a;
}
void display()
{
CPerson::display();
cout << of_school_age;
}
private:
int of_school_age;
};
int main()
{
CStudent a;
a.set(\"lilei\
a.set_score(89);
a.display();
cout << endl;
CTeacher b;
b.set(\"lisi\
b.set_school_age(12);
b.display();
return 0;
}
27、把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类CLine,在派生出一个矩阵类CRect。要求成员函数能求出两点间的距离、矩阵的周长和面积等。设计一个测试程序,并构造完整的程序。
#include#includeusing namespace std;
class cpoint
{
public:
int x,y;
public:
cpoint(int x=0,int y=0)
{
this->x=x;
this->y=y;
}
};
class cline:public cpoint
{
private:
cpoint p1,p2;
public:
cline(int a = 0,int b = 0,int c = 0,int d = 0) :p1(a,b),p2(c,d)
{ }
double length()//求两点间距
{
return sqrt((p2.y-p1.y)*(p2.y-p1.y)+(p2.x-p1.x)*(p2.x-p1.x));
}
};
class crect:public cline
{
private:
cline l1,l2;
public:
crect(int a,int b,int c,int d,int e,int f) : l1(a,b,c,d),l2(c,d,e,f)
{ }
void girth()
{
cout<<\"周长:\"<<2*(l1.length()+l2.length()) <}void area()
{
cout<<\"面积:\"<}};
int main()
{
crect c(0,0,1,0,1,1);
c.girth();
c.area();
return 0;
}
28、定义一个字符串类CStrOne,包含一个存放字符串的数据成员,能够通过构造函数初始化字符串,通过成员函数显示字符串的内容。在此基础上派生出CStrTwo类,增加一个存放字符串的数据成员,并能通过派生类的构造函数传递参数,初始化两个字符串,通过成员函数进行两个字符串的合并以及输出。(字符串和并可使标准库函数strcat,需要包含头文件string.h)
#include#includeusing namespace std;
class CStrOne
{
public:
CStrOne(char str[])
{
strcpy(strOne, str);
}
void print()
{
cout << strOne << endl;
}
protected:
char strOne[20];
};
class CStrTwo:public CStrOne
{
public:
CStrTwo(char str1[], char str2[]):CStrOne(str1)
{ strcpy(strTwo, str2); }
void Hebing()
{ strcat(strOne, strTwo); }
void printTwo()
{ cout << strTwo << endl; }
private:
char strTwo[20];
};
int main()
{
CStrTwo zfc(\"1zifuc\
cout << \"第一个字符串:\";
zfc.print();
cout << \"第二个字符串:\";
zfc.printTwo();
zfc.Hebing();
cout << \"合并后的字符串:\";
zfc.print();
return 0;
}