C++题目练习03

C++题目练习03

3.1 定义哺乳动物类Mammal和Dog类

定义哺乳动物类 Mammal,有 age(int)和 weight(double)两个数据成员,再由此派生出狗类 Dog,有 color(string)。创建一个 Dog 类的对象

#include <iostream>
using namespace std;

class Mammal {
	private:
		int age;
		double weight;
	public:
		Mammal() {
			cout << "Constructor in Mammal." << endl;
		}
		void setAge(int age) {
			this->age = age;
		}
		void setWeight(int weight) {
			this->weight = weight;
		}
		int getAge() {
			return this->age;
		}
		int getWeight() {
			return this->weight;
		}
		~Mammal() {
			cout << "Destructor in Mammal." << endl;
		}
};

class Dog: public Mammal {
	private:
		string color;
	public:
		Dog() {
			cout << "Constructor in Dog." << endl;
		}
		~Dog() {
			cout << "Destructor in Dog." << endl;
		}
		void setColor(string color) {
			this->color = color;
		}
		string getColor() {
			return this->color;
		}
		void speak() {
			cout << "Dog sound wang,wang,wang!" << endl;
		}
};

int main() {
	Dog d;

	d.setAge( 4 );
	d.setWeight( 12 );
	d.setColor( "Black" );

	cout << d.getAge() << endl;
	cout << d.getWeight() << endl;
	cout << d.getColor() << endl;

	d.speak();

	return 0;
}


3.2 设计管理出版物的类

某出版社发行图书和光盘,利用继承设计管理出版物的类。
要求如下:给出一个基类Publication存储出版物的标题title、出版物名称name、单价price及出版日期date;继承创建Book类和CD类分别管理图书和光盘,它们都从Publication类派生;
Book类具有保存图书页数的数据成员page,CD类具有保存播放时间的数据成员playtime;
每个类都有构造函数、析构函数,且都有用于从标准输入获取数据的成员函数inputData()和用于显示数据的成员函数display()。
 

#include <iostream>
#include <string>
using namespace std;

// 年月日
struct Date {
   int year;
   int month;
   int day;

   Date( int y = 0, int m = 0, int d = 0 ) {
       year  = y;
       month = m;
       day   = d;
   }

   ~Date(){ }
};

// 时分秒
struct Time {
   int hour;
   int minute;
   int second;

   Time( int h = 0, int m = 0, int s = 0 ) {
       hour   = h;
       minute = m;
       second = s;
   }

   ~Time(){ }
};

// 基类:出版物
class Publication {
private:
   string title; // 出版物的标题
   string name; // 名称
   float price; // 单价
   Date date; // 出版日期

public:
   Publication( string t = "", string n = "", float p = 0, int h = 0, int m = 0, int s = 0 )
       :date( h, m, s ) {
       title = t;
       name  = n;
       price = p;
   }

   ~Publication(){ }

   void inputdata( ) {        
       cin >> title;        
       cin >> name;        
       cin >> price;        
       cin >> date.year >> date.month >> date.day;
   }

   void display() {
       cout << "*****display*****" << endl;
       cout << "title:" << title << endl;
       cout << "name:" << name << endl;
       cout << "price:" << price << endl;
       cout << "year-month-day:" << date.year << "-" << date.month << "-" << date.day << endl;
   }
};

class Book:public Publication
{
	private:
		int page;
	public:
		Book(string t="",string n="",float p=0,int h=0,int m=0,int s=0,int g=0)
		:Publication(t,n,p,h,m,s)
		{ 
			page = g;
		}
		~Book()
		{	
		}
		void inputdata()
		{
			Publication::inputdata();
			cin>>page;
		}
		void display()
		{
			Publication::display();
			cout<<"pages:"<<page<<endl;
		}
};

class CD:public Publication
{
	private:
	   Time time;
	public:
	   CD(int h,int m,int s,string t="",string n="",int p=0,int y=0,int mt=0,int d=0)
	      :time(h,m,s),Publication(t,n,p,y,mt,d)
	      {
		  }
	   ~CD()
	   {
	   }
	   
	   void display()
	   {
	   	Publication::display();
	   	cout<<"playtime(h:m:s)"<<" "<<time.hour<<":"<<time.minute<<":"<<time.second<<endl; 
	   }
};

int main() {
   Book b;
   CD c( 1, 2, 3, "Noname", "肖邦钢琴协奏曲", 61, 2018, 8, 1 );
   b.inputdata();
   b.display();
   c.display();    
   return 0;
}


3.3 学生类和教师类的设计

一个教学系统至少有 学生 和 教师 两种类型的人员,假设 教师 的数据有 教师编号、姓名、年龄、性别、职称和系别,学生的数据有 学号、姓名、年龄、性别、班级和语文、数学、英语三门课程的成绩。现编程完成学生和教师档案数据的输入和显示。要求如下:
设计三个类 Person、Teacher、Student,Person 是 Teacher 和 Student 的基类,具有此二类共有的数据成员姓名、年龄、性别,并具有输入和显示这些数据的成员函数;
Teacher 类继承了 Person 类的功能,并增加对教师编号、职称和系别等数据成员进行输入和显示的成员函数。Student 类按同样的方法设计。

#include <iostream>
#include <string>
using namespace std;

class Person{
private:
   string name;
   int age;
   string sex;

public:
   Person( string = "", int = 0, string = "" );

   void inputname( ) {
       cin >> name;
   }

   void printname( ) {
       cout << name << endl;
   }

   void inputage( ) {
       cin >> age;
   }

   void printage( ) {
       cout << age << endl;
   }

   void inputsex( ) {
       cin >> sex;
   }

   void printsex( ) {
       cout << sex << endl;
   }

};

Person::Person( string Name, int Age, string Sex ) {
   name = Name;
   age  = Age;
   sex  = Sex;
}

class Teacher:public Person
{
	private:
		string number;
		string zc;
		string xi;
	public:
		Teacher()
		{
		}
		Teacher(string Name, int Age, string Sex,string n,string z,string x)
		:Person(Name,Age,Sex)
		{
			number = n;
			zc = z;
			xi = x;
		}
		~Teacher()
		{	
		}
	void inputname()
	{
	   Person::inputname();
	}
	void inputage()
	{
		Person::inputage();
	}
	void printfname()
	{
		Person::printname();
	}
	void printfage()
	{
		Person::printage(); 
	}
 }; 
 
 class Student:public Person
 {
 	private:
		string number;
		string banji;
		int chinese;
		int math;
		int english;
		
	public:
		Student()
		{
		}
		Student(string Name, int Age, string Sex,string n,string b,int c,int m,int e)
		:Person(Name,Age,Sex)
		{
			number = n;
			banji = b;
			chinese = c;
			math =m;
			english =e;
			
		}
		~Student()
		{	
		}
	void inputname()
	{
	   Person::inputname();
	}
	void inputage()
	{
		Person::inputage();
	}
	void printfname()
	{
		Person::printname();
	}
	void printfage()
	{
		Person::printage(); 
	}
	void inputChinese()
	{
		cin>>chinese;
	}
 		
 }; 

int main( ) {
   Teacher t1, t2( "张明", 33, "男", "T001", "讲师",   "计算机科学与软件学院" );
   Student s1, s2( "陈晓梅", 19, "女", "S001", "0309201", 90, 92, 98 );

   t1.inputname();
   t1.inputage();

   s1.inputChinese();
   s1.inputname();

   t1.printname();
   t1.printage();

   s1.printname();
   s1.printage();

   t2.printname();
   t2.printage();

   s2.printname();
   s2.printage();

   return 0;
}


  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值