栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++面向对象编程-继承与派生练习题

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++面向对象编程-继承与派生练习题

1、建筑物类Building的派生类
#include

class Building
{
private:
    int N;//编号
    int floor;//层数
    int room;//房间数
    double area;//总面积
public:
    Building(int n=1,int f=5,int r=20,double a=2000)
    {
        N=n,floor=f,room=r,area=a;
    }
};
class TeachBuilding:public Building
{
    TeachBuilding(int n,int f,int r,double a):Building(n,f,r,a){}
};
class DormBuilding:public Building
{
    int stuNum;//容纳学生总人数
    //默认实参要在形参列表的结尾
    DormBuilding(int n,int f,int r,double a,int stuNum=1000):Building(n,f,r,a)
    {
        this->stuNum=stuNum;
    }
};
2、共同派生类
#include

class Table
{
private:
    double height;//高度
public:
    Table(double h):height(h){}
};
class Circle
{
private:
    double area;//面积
public:
    Circle(double a):area(a){}
};
//每一个父类前都要有继承方式
class RoundTable:public Table,public Circle
{
public:
    RoundTable(double h,double a):Table(h),Circle(a){}
};
3、人员类Person的派生(虚基类的使用)
#include
#include
using namespace std;
#include
class Date
{
private:
    int year,month,day;//年月日
public:
    Date(int y=1900,int m=1,int d=1)
    {
        year=y,month=m,day=d;
    }
    void show()
    {
        cout<
private:
    string name;//姓名
    string sex;//性别
    char id[25];//身份证号
    Date birthday;//出生年月
public:
    Person(){}
    Person(string n,string s,const char i[],int y,int m,int d):birthday(y,m,d)//birthday要拆开来赋值 
    {
        name=n,sex=s;
        strcpy(id,i);
    }
    void show()
    {
        cout<<"name:"<
private:
    char stuId[25];//学号
    double score;//成绩
public:
	//要传入const类型字符串,表示只读 
    Student(string name,string sex,const char id[],int y,int m,int d,const char stuid[],double sc):
    Person(name,sex,id,y,m,d)
    {
        strcpy(stuId,stuid);
        score=sc;
    }
    void show()
    {
    	Person::show();
        cout<<"stuId:"<
private:
    string job;//教师职称
public:
    Teacher(string name,string sex,const char id[],int y,int m,int d,string job):
    Person(name,sex,id,y,m,d)
    {
        this->job=job;
    }
    void show()
    {
        cout<<"job:"<
public:
	//这个构造函数,要使用全部基类进行赋值,包括间接基类Person 
    StuTech(string name,string sex,const char id[],int y,int m,int d,const char stuId[],double score,string job):
    Person(name,sex,id,y,m,d),Student(name,sex,id,y,m,d,stuId,score),Teacher(name,sex,id,y,m,d,job){}
    void show()
    {
        Student::show();
        Teacher::show();
    }
};

int main()
{
    StuTech st("zhangsan","man","123456789",2002,6,1,"001",95.5,"jiangshi");
    st.show();
    return 0;
}

4、带AM,PM的时钟类Clock
#include
#include//可以获取当前时间
#include
using namespace std;

class Clock
{
private:
	int hour,minute,second;
public:
	//Clock();
	Clock(int h=0,int m=0,int s=0)
	{
		hour=(h>=0&&h<24) ? h:0;
		minute=(m>=0&&m<60) ? m:0;
		second=(s>=0&&s<60) ? s:0;
	}
	int getHour()
	{
		return hour;	
	}	
	//24小时制
	void showTime24()
	{
		cout<
		if(hour==0)
		{
			cout<
			cout<
		cout<
		second=(second+1)%60;
		if(second==0)
		{
			minute=(minute+1)%60;
			if(minute==0)
			{
				hour=(hour+1)%24;
			}
		}
	}
}; 
int main()
{
	int hour,minute,second;
	double nowTime;
	cout << "请设置当前时间(以24小时制按顺序输入时 分 秒):" << endl;
	cin>>hour>>minute>>second;
	cout<<"t";
	Clock clock(hour,minute,second);
	int cnt=0;
	while(cnt<5)
	{
		nowTime=time(0);
		while(time(0)<(nowTime+1));
		clock.timeAdd();
		cout<<"r";
		cout<<"24小时制:";
		clock.showTime24();
		cout<
			cout<<"AM ";
			clock.showTimeAM();
		}else{
			cout<<"PM ";
			clock.showTimePM();
		}
		cnt++;
	}
	return 0;
}

5、Employee类实现职员月工资管理
#include
using namespace std;
#include
const double sales = 200000;//销售额
class Employee
{
private:
	int id;//编号
	string name;//姓名
	string job;//岗位
	double monthMoney;//月工资
public:
	Employee(int id, string name, string job)
	{
		this->id = id, this->name = name;
		this->job = job;
		monthMoney= getMonthMoney();
	}
	double getMonthMoney()
	{
		if (job.compare("manager") == 0)//使用string的compare函数比较字符串
		{
			monthMoney = 8000;
		}
		else if (job.compare("technician") == 0)
		{
			monthMoney = 100 * 8 * 30;
		}
		else if (job.compare("salesman") == 0)
		{
			monthMoney = sales * 0.04;
		}
		else if (job.compare("salesmanager") == 0)
		{
			monthMoney = 5000 + sales * 0.005;
		}
		return monthMoney;
	}
	void showInformation()
	{
		cout << "编号:" << id << endl;
		cout << "姓名:" << name << endl;
		cout << "岗位:" << job << endl;
		cout << "月薪:" << monthMoney << endl;
	}
};
int main()
{
	Employee p1(1, "zhangsan", "manager");
	Employee p2(2, "lisi", "technician");
	Employee p3(3, "wangwu", "salesman");
	Employee p4(4, "zhaoliu", "salesmanager");
	p1.showInformation();
	cout << endl;
	p2.showInformation();
	cout << endl;
	p3.showInformation();
	cout << endl;
	p4.showInformation();
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/977162.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号