c++实验答案

内容发布更新时间 : 2024/5/20 3:57:29星期一 下面是文章的全部内容请认真阅读。

The number of Prime between 250~300 is 10

251 257 263 269 271 277 281 283 289 293

实验十二 1.实验目的 通过本次实验

(1)理解继承与派生的概念;

(2)掌握派生类定义格式与使用方法;

(3)初步掌握派生类构造函数的定义与使用方法,理解构造函数的调用过程,及基类成员的初始化过程;

(4)理解冲突、支配规则与赋值兼容性原则的概念。 2.实验要求

(1)编写实验程序;

(2)在VC++运行环境中,输入源程序; (3)编译运行源程序;

(4)输入测试数据进行程序测试; (5)写出运行结果。 3.实验内容

(1)定义描述职工档案的类Archives,私有数据成员为职工号(No)、姓名(Name[8])、性别(Sex)、年龄(Age)。成员函数有:构造函数、显示职工信息的函数Show()。再由职工档案类派生出职工工资类Laborage,在职工工资类Laborage中新增数据成员:应发工资(SSalary)、社保金(Security)、实发工资(Fsalary),其成员函数有:构造函数,计算实发工资的函数Count(),计算公式为:实发工资=应发工资-社保金。显示职工档案及工资的函数Display()。在主函数中用Laborage类定义职工对象lab,并赋初始值(1001,”Cheng”,’M’,21,2000,100),然后显示职工档案与工资。

(2)定义描述矩形的类Rectangle,其数据成员为矩形的中心坐标(X,Y)、长(Length)与宽(Width)。成员函数为计算矩形面积的函数Area()与构造函数。再定义描述圆的类Circle,其数据成员为圆的中心坐标(X,Y)与半径R,其成员函数为构造函数。再由矩形类与圆类多重派生出长方体类Cuboid,其数据成员为长方体的高(High)与体积(Volume)。成员函数为:构造函数,计算体积的函数Vol(),显示矩形坐标(X,Y)、长方体的长、宽、高与体积的函数Show()。主函数中用长方体类定义长方体对象cub,并赋初始值(10,10,10,20,30,30,10,10),最后显示长方体的矩形坐标(X,Y)与长方体的长、宽、高与体积。

(3)定义个人信息类Person,其数据成员有姓名、性别、出生年月。并以Person为基类定义一个学生的派生类Student,增加描述学生的信息:班级、学号、专业、英语成绩和数学成绩。再由基类Person定义一个职工的派生类Employee,增加描述职工的信息:部门、职务、工资。编写程序实现学生与职工信息的输入与输出。 4.解答参考 (1)

# include # include class Archives

{ private: int No;

char Name[8]; char Sex; int Age; public:

Archives(int n,char name[],char s,int a) { No=n;

strcpy(Name,name); Sex=s; Age=a; }

void Show(void)

{ cout<<\ <<\ } };

class Laborage:public Archives { private:

float SSalary,Security,Fsalary; public:

Laborage(int n,char name[],char s,int a,float ss,float se): Archives(n,name,s,a) { SSalary=ss; Security=se; }

void Count() {

Fsalary=SSalary-Security; }

void Display(void) { Show();

cout<<\ <<'\\t'<<\ } };

void main(void)

{ Laborage lab(1001,\ lab.Count(); lab.Display(); }

程序运行结果:

No=1001 Name=Zhou Sex=M Age=52 SSalary=2000 Security=200 Fsalary=1800

(2)

# include # define PI 3.14159

class Rectangle //定义一个长方体类 { protected:

float Length,Width;

float Centerx,Centery; public:

Rectangle(float l,float w,float x,float y) { Length=l; Width=w; Centerx=x; Centery=y; }

float Area(void)

{ return Length*Width;} };

class Circle //定义一个圆形类 { protected:

float radius;

float Centerx,Centery; public:

Circle(float r,float x,float y) { radius=r;

Centerx=x; Centery=y; }

double Area(void)

{ return radius*radius*PI;} };

class Cuboid:public Rectangle,public Circle //由基类Rectangle、Circle派生出类Cuboid { private:

float High;

double RVolume,CVolume; public:

Cuboid(float l,float w,float x1,float y1,float r,float x2,float y2,float h):Rectangle(l,w,x1,y1),Circle(r,x2,y2) { High=h;}

void Vol(void) //分别计算长方体和圆柱体的体积 { RVolume=Rectangle::Area()*High; CVolume=Circle::Area()*High; }

void Show(void) //分别显示长方体和圆柱体的信息

{ cout<<\ <<\

cout<<\ <

cout<<\olume=\olume<<'\\n';

cout<<\

cout<<\ <

cout<<\olume=\ } };

void main (void)

{ Cuboid cub(10,10,10,20,30,30,10,10); cub.Show(); }

程序运行结果:

Length=10 Width=10 High=10 Rectangle Center coordinate = 10,20 Rectangle Volume=1000 Radius=30 High=10 Circle Center coordinate = 30,10 Circle Volume=28274.3 (3)

# include # include class Person { private:

char Name[8]; char Sex;

char Birth[10]; public: Person() {}

Person(char name[],char sex,char birth[]) { strcpy(Name,name); Sex=sex;

strcpy(Birth,birth); }

void Show()

{ cout<

};

class Student:public Person { private:

char Sclass[10]; int No;

char Major[10]; float Eng,Math; public:

Student():Person() {}

Student(char name[],char sex,char birth[],char sclass[],int no,char major[],float eng,float math):Person(name,sex,birth) { strcpy(Sclass,sclass); No=no;

strcpy(Major,major); Eng=eng; Math=math; }

void Print() { Person::Show();

cout<

cout<

class Employee :public Person { private:

char Department[10]; char Title[10]; float Salary; public:

Employee():Person() {}

Employee(char name[],char sex,char birth[],char department[10],char title[10],

float salary) : Person(name,sex,birth) { strcpy(Department,department); strcpy(Title,title); Salary=salary; }

void Print() { Person::Show();

cout<

cout<

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4 ceshi