第五章习题

1、


#include <iostream>
using namespace std;
class Student
{public:
  void get_value()
   {cin>>num>>name>>sex;}
  void display( )
    {cout<<"num: "<<num<<endl;
     cout<<"name: "<<name<<endl;
     cout<<"sex: "<<sex<<endl;}
 private :
   int num;
   char name[10];
   char sex;
};   




class Student1: public Student
 {public:
   void get_value_1()
    {get_value();
     cin>>age>>addr;}
   void display_1()
   {   cout<<"age: "<<age<<endl;//引用派生类的私有成员
       cout<<"address: "<<addr<<endl;}//引用派生类的私有成员
  private:
       int age;
       char addr[30];
 };




int  main()
{Student1 stud1;
  stud1.get_value_1();
  stud1.display();
  stud1.display_1();
  return 0;
}


2、


#include <iostream>
using namespace std;
class Student
{public:
  void get_value()
   {cin>>num>>name>>sex;}
  void display( )
    {cout<<"num: "<<num<<endl;
     cout<<"name: "<<name<<endl;
     cout<<"sex: "<<sex<<endl;}
 private :
   int num;
   char name[10];
   char sex;
};   




class Student1: private Student
 {public:
   void get_value_1()
    {get_value();
     cin>>age>>addr;}
   void display_1()
       {display();
       cout<<"age: "<<age<<endl;//引用派生类的私有成员,正确。
       cout<<"address: "<<addr<<endl;}//引用派生类的私有成员,正确。
  private:
       int age;
    char addr[30];
 };




int main()
 {Student1 stud1;
  stud1.get_value_1();
  stud1.display_1();
  return 0;
}










3、


#include <iostream>
using namespace std;
class Student//声明基类
{public://基类公用成员
  void get_value();
  void display( );
 protected ://基类保护成员
    int num;
    char name[10];
    char sex;
};




void Student::get_value()
 {cin>>num>>name>>sex;}




void Student::display( )
 {cout<<"num: "<<num<<endl;
  cout<<"name: "<<name<<endl;
  cout<<"sex: "<<sex<<endl;
 }




class Student1: protected Student//声明一个保护派生类
{public:
   void get_value_1();
   void display1( );
private:
   int age;                          
   char addr[30];
};




void Student1::get_value_1()
 {get_value();
  cin>>age>>addr;
 }
void Student1::display1( ) 
  {cout<<"num: "<<num<<endl;//引用基类的保护成员
   cout<<"name: "<<name<<endl;//引用基类的保护成员
   cout<<"sex: "<<sex<<endl;//引用基类的保护成员
   cout<<"age: "<<age<<endl;//引用派生类的私有成员
   cout<<"address: "<<addr<<endl;//引用派生类的私有成员
  }




int main( )
 {Student1 stud1;//stud1是派生类student1类的对象
  stud1.get_value_1();//调用派生类对象stud1的公用成员函数
  stud1.display1( );//调用派生类对象stud1的公用成员函数
  return 0;
 }










4、


#include <iostream>
using namespace std;
class Student//声明基类
{public://基类公用成员
  void get_value();
  void display( );
 protected ://基类保护成员
    int num;
    char name[10];
    char sex;
};




void Student::get_value()
 {cin>>num>>name>>sex;}




void Student::display( )
 {cout<<"num: "<<num<<endl;
  cout<<"name: "<<name<<endl;
  cout<<"sex: "<<sex<<endl;
 }




class Student1: public Student//声明一个公用派生类
{public:
   void get_value_1();
   void display1( );
 private:
   int age;                          
   char addr[30];
};




void Student1::get_value_1()
 {get_value();
  cin>>age>>addr;
 }
void Student1::display1( )
  {cout<<"num: "<<num<<endl;//引用基类的保护成员
   cout<<"name: "<<name<<endl;//引用基类的保护成员
   cout<<"sex: "<<sex<<endl;//引用基类的保护成员
   cout<<"age: "<<age<<endl;//引用派生类的私有成员
   cout<<"address: "<<addr<<endl;//引用派生类的私有成员
  }




int main( )
 {Student1 stud1;//stud1是派生类student1类的对象
  stud1.get_value_1();//调用派生类对象stud1的公用成员函数get_value_1
  stud1.display1( );//调用派生类对象stud1的公用成员函数display1
  return 0;
 }

9.include<iostream>
#include<string>
using namespace std;
class Teacher
{
private:
char name[20];
int age;
char title[20];
char sex[20];
char address[100];
char tel[20];
public:
void initTeacher(char nam[],int ag,char tit[],char se[],char add[],char t[])
{
age=ag;
strcpy(sex,se);
strcpy(name,nam);
strcpy(title,tit);
strcpy(address,add);
strcpy(tel,t);
}  
void display();
};
void Teacher::display()
{
cout<<name<<endl<<age<<endl<<sex<<endl<<address<<endl<<tel<<endl;
}
class Cadre
{
private:
char name[20];
int age;
char post[20];
char sex[20];
char address[100];
char tel[20];
public:  
void initCadre(char nam[],int ag,char po[],char se[],char add[],char t[])
{  
age=ag;
strcpy(sex,se);
strcpy(name,nam);
strcpy(post,po);
strcpy(address,add);
strcpy(tel,t);
}  
char * pos()  
{  
return post;
}
};
class Teacher_Cadre:public Teacher,public Cadre
{
private:
int wage;
public:
void initTC(char nam[],int ag,char po[],char tit[],char se[],char add[],char t[],int w)
{
initTeacher(nam,ag,tit,se,add,t);
initCadre(nam,ag,po,se,add,t);
wage=w;
}
void showk();
};
void Teacher_Cadre::showk()
{  
Teacher::display();
cout<<Cadre::pos()<<endl<<wage<<endl;
}
int main()
{
Teacher_Cadre A;
A.initTC("a",25,"书记","教师","男","武汉市","搜索1234567890",5000);
A.showk();
return 0;
}

10、

#include <iostream>
#include <cstring>
using namespace std;
class Teacher                                //教师类
 {public:
 Teacher(int,char [],char);              //声明构造函数
   void display();                         //声明输出函数
 private:
  int num;
  char name[20];
  char sex;
  };
Teacher::Teacher(int n,char nam[],chars)    //定义构造函数
 {num=n;
 strcpy(name,nam);
 sex=s;
}
void Teacher::display()                      //定义输出函数
 {cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
class BirthDate   //生日类


 {public:


   BirthDate(int,int,int);  //声明构造函数


   void display(); //声明输出函数


   void change(int,int,int); //声明修改函数


 private:


   int year;


   int month;


   int day;


};


 


BirthDate::BirthDate(int y,int m,intd) //定义构造函数


 {year=y;


 month=m;


 day=d;


 }


 


void BirthDate::display()//定义输出函数


 {cout<<"birthday:"<<month<<"/"<<day<<"/"<<year<<endl;}


 


void BirthDate::change(int y,int m,intd)//定义修改函数


 {year=y;


 month=m;


 day=d;


 }


 


class Professor:public Teacher //教授类


 {public:


   Professor(int,char [],char,int,int,int,float); //声明构造函数


   void display();//声明输出函数


   void change(int,int,int);  //声明修改函数


  private:


   float area;


   BirthDate birthday; //定义BirthDate类的对象作为数据成员


 };


 


Professor::Professor(int n,charnam[20],char s,int y,int m,int d,float a):


 Teacher(n,nam,s),birthday(y,m,d),area(a){}          //定义构造函数


 


void Professor::display() //定义输出函数


{Teacher::display();


 birthday.display();


 cout<<"area:"<<area<<endl;


}


 


void Professor::change(int y,int m,intd) //定义修改函数


 {birthday.change(y,m,d);


 }


 


int main()


{Professorprof1(3012,"Zhang",'f',1949,10,1,125.4);  //定义Professor对象prof1


 cout<<endl<<"originaldata:"<<endl;


 prof1.display();//调用prof1对象的display函数


 cout<<endl<<"newdata:"<<endl;


 prof1.change(1950,6,1); //调用prof1对象的change函数


 prof1.display(); //调用prof1对象的display函数


 return 0;


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值