C++ 继承与派生编程题

C++ 继承与派生—笔记

1. 编写一个程序,其中有一个汽车类Vehicle,它具有一个需要传递参数的构造函数,类中的数据成员:车轮个数wheels和车重weight为保护属性;小车类Car是它的私有派生类,其中包含载人数passager_load;卡车类Truck是Vehicle的私有派生类,其中包含载人数passager_load和载重量payload。每个类都有相关数据的输出方法。

#include
#include
using namespace std;

class Vehicle//声明汽车类
{
 protected:
  int wheel;//车轮
  double weight;//车重
 public:
  Vehicle(int w,double i){//构造函数
   wheel = w;
   weight = i;
  }
  void show(){//输出函数
   cout<<"车轮个数:"<  }
};

class Car:private Vehicle//声明小车类,私有继承汽车类
{
 private:
  int passager_load;//载人数
 public:
  Car(int w,double i,int p):Vehicle(w,i){//构造函数
   passager_load = p;
  }
  void display(){//输出函数
   cout<<"\n";
   Vehicle::show();
   cout<<"\t\t载人数:"<  }
};

class Truck:private Vehicle//声明卡车类,私有继承汽车类
{
 private:
  int passager_load;//载人数
  int payload;//载重量
 public:
  Truck(int w,double i,int p,int l):Vehicle(w,i){//构造函数
   passager_load = p;
   payload = l;
  }
  void display1(){//输出函数
   Vehicle::show();
   cout<<"\t\t载人数:"<  }
};

int main()
{
 Vehicle V(4,20);
 V.show();

 Car C(4,40.0,4);
 C.display();

 Truck T(6,80.0,2,100);
 T.display1();

 system("pause");
 return 0;
}

2. 假定居民的基本数据包括身份证号、姓名、性别和出生日期,而居民中的成年人又多两项数据:最高学历和职业,成年人中的党员又多一项数据:党员类别。现要求建立三个类,让成年人类继承居民类,而党员类继承成年人类,并要求在每个类中都提供有数据输入和输出的功能。

#include
#include
using namespace std;

class Residents
{
 private:
  int number;//身份证号
  string name;//姓名
  string sex;//性别  
  int birthday;//出生日期  
 public:
  void input(){
   cout<<"请按顺序(身份证号、姓名、性别、出生日期)输入居民信息:"<   cin>>number>>name>>sex>>birthday;   
  }
  void output() {  
   cout<<"\n\r输出居民信息:"<   cout<<" "<<number<<"   "<<name<<"   "<<sex<<"   "<<birthday<<endl;  
  }
};

class Adult:public Residents
{
 private:
  string Degree;//最高学历  
  string Occupation;//职业 
 public:
  void input1(){
   Residents::input();  
            cout<<"请输入最高学历和职业:"<<endl;  
            cin>>Degree>>Occupation;
  }
  void output1() { 
   cout<<"\n\r输出成年人信息:"<            Residents::output();  
            cout<<Degree<<"   "<<Occupation<<endl;  
        }  
};
class Party: public Adult
{  
 private:  
  string parties; //党员类别  
 public:  
        void input2() {  
            Adult::input1();  
            cout<<"请输入党员类别:"<<endl;  
            cin>>parties;  
        }  
        void output2(){  
            cout<<"\n\r输出党员信息:"<<endl;  
            Adult::output1();  
            cout<<parties<<endl;  
        }  
}; 

int main()
{
 //测试居民类
 Residents R;
 R.input();
 R.output();
 cout<<"\n"<

 //测试成人类
 Adult A;
 A.input1();
 A.output1();
 cout<<"\n"<

 // 测试党员类
 Party P;  
    P.input2();  
    P.output2();

 system("pause");
 return 0;
}

3. 数据成员 length,width;
成员函数 Rectangle( double l, double w ); //构造函数
double Area(); //返回矩形面积
double GetLength(); //返回数据成员length的值
double GetEidth(); //返回数据成员width的值
再定义Rectangle的派生类Rectangular,它包含:
数据成员 height
成员函数 Rectangular( double l, double w, double h ); //构造函数
double Volume(); //返回长方体体积
double GetHeight(); //返回数据成员height的值
在main函数中测试类体系,建立两个类的对象,显示它们的数据和面积、体积。


#include
#include
using namespace std;

class Rectangle//声明类
{
 public:
  double length;
  double width;
  Rectangle();//无参构造
  Rectangle(double l, double w); //构造函数
  double Area(); //返回矩形面积
  double GetLength(); //返回数据成员length的值
  double GetWidth(); //返回数据成员width的值
};

Rectangle::Rectangle(double l,double w)
{
 length = l;
 width = w;
}

double Rectangle::Area()
{
 return (length * width);
}

double Rectangle::GetLength()
{
 return length = 0.0;
}

double Rectangle::GetWidth()
{
 return width = 0.0;
}

class Rectangular:public Rectangle
{
 private:
  double height;
 public:
  Rectangular();//无参构造
  Rectangular( double l, double w, double h ); //构造函数
  double Volume(); //返回长方体体积
  double GetHeight(); //返回数据成员height的值
};

Rectangular::Rectangular(double l,double w,double h):Rectangle(l,w)
{
 height = h;
}

double Rectangular::Volume()
{
 return (length * width * height);
}

double Rectangular::GetHeight()
{
 return height = 0.0;
}

int main()
{
 Rectangle r(30,40);
 cout<<"面积为:"<

 Rectangular t(30,40,50);
 cout<<"体积为:"<

 system("pause");
 return 0;
}

**4. 编写一个师生数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名、输入和显示设计成一个类Person,并作为学生数据操作类Student和教师数据操作类Teacher的基类。
**


#include
#include
using namespace std;

class Person//声明类person
{
 public:  
  string name;//姓名
  int number;//编号

  Person();//无参构造
  Person(string n,int num){//构造函数
   name = n;
   number = num;   
  }
  void show(){//输出
   cout<<"姓名:"<  }
};

class Student:public Person//声明学生类
{
 private:
  int Class;//班号
  int score;//成绩
 public:
  Student();//无参构造
  Student(string n,int num,int C,int s):Person(n,num){//构造函数
   Class = C;
   score = s;
  }
  void display(){//输出函数
   show();
   cout<<"\t"<<"班号:"<  }
};

class Teacher:public Person//声明教师类
{
 private:
  string title;//职称
  string section;//部门
 public:
  Teacher();//无参构造
  Teacher(string n,int num,string t,string s):Person(n,num){//构造函数
   title = t;
   section = s;
  }
  void display(){//输出函数
   show();
   cout<<"\t"<<"职称:"<  }
};

int main()
{
 Student S("小李",123456,01,500);
 S.display();

 Teacher T("老张",987654,"班主任","数学");
 T.display();

 system("pause");
 return 0;
}

  • 4
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
面试C++三大特性相关的问题通常指的是封装、继承和多态。封装是将数据和操作封装在一个类,通过访问权限控制来保护数据,实现了数据的隐藏和封装。继承是一种面向对象编程的关键特性,允许一个类继承另一个类的属性和方法。多态是指同一操作作用于不同的对象,可以有不同的行为。下面我将分别讨论这三个特性。 封装是C++的一个重要特性,它通过将数据和操作封装在一个类,提供了数据的隐藏和封装。通过访问权限控制,我们可以将一些私有数据隐藏起来,只允许通过类的公有接口进行访问。这样可以保护数据的完整性和安全性,同时也提供了更好的代码组织和维护。 继承C++的另一个重要特性,它允许一个类继承另一个类的属性和方法。通过继承派生类可以获得基类的成员变量和成员函数,并且可以通过重写和扩展基类的方法来实现自己的功能。继承提供了代码复用和层次化设计的能力,同时也允许多态的实现。 多态是C++面向对象编程的一个关键特性,它可以让同一个操作作用于不同的对象,产不同的行为。多态通过虚函数实现,通过将函数声明为虚函数,派生类可以重写基类的方法,并通过基类的指针或引用调用派生类的方法。这样就可以根据对象的实际类型来决定调用哪个方法,实现了运行时的动态绑定。 综上所述,封装、继承和多态是C++面向对象编程的三大特性。封装提供了数据的隐藏和封装,继承提供了代码复用和层次化设计的能力,多态实现了同一操作作用于不同对象的不同行为。这些特性在C++广泛应用,是面试常见的考点。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值