CUC程序设计继承与派生

贪吃的长颈鹿

长颈鹿是一种动物:动物需要吃食物,因此长颈鹿也需要吃食物;长颈鹿还可以伸长脖子,可以用嘴够树叶吃。请建立Animal类和Giraffe类,并编程描述上述关系。
在这里插入图片描述

#include<iostream>
using namespace std;
class Animal
{
public:
	void take()
	{cout<<"eat."<<endl;}
};
class Giraffe:public Animal
{
public:
	void StretchNeck()
	{
		cout<<"stretch neck."<<endl;
	}
	void Func(Giraffe & an);
private:
	Animal an;
};
void Func(Giraffe & an){
  an.take();//够树叶吃
}
int main(){
  Giraffe gir;
  gir.StretchNeck();
  Func(gir);    
}

继承和派生汽车类

在这里插入图片描述

#include <iostream>
using namespace std;
class vehicle //汽车类
{
protected:
    int wheels; // 车轮数
    double weight; // 重量
public:
    vehicle(int a,double b);
    int GetWheels() { return wheels; }
    double GetWeight() { return weight; }
    void show();
};
vehicle::vehicle(int a, double b)
{
    wheels = a;
    weight = b;
}
void vehicle::show() { cout<<wheels<<" "<<weight<<" ";}

class car :public vehicle //小汽车类
{
    int passenger;//载人数
public:
    car(int wheels1, double weight1, int passenger1);
    void show();
};
car::car(int wheels1, double weight1, int passenger1):
vehicle(wheels1, weight1){passenger = passenger1;}

void car::show()
{
    vehicle::show();
    cout<<passenger<<endl;
}

class truck :public vehicle //卡车类
{
    int passenger;//载人数
    double payload;//载重量
public:
truck(int wheels1, double weight1, int passenger1,
double payload1);
    void show();
};
truck::truck(int wheels1, double weight1, int passenger1, 
double payload1):vehicle(wheels1, weight1)
{
    passenger = passenger1;
    payload = payload1;
}
void truck::show()
{
    vehicle::show();
    cout<<passenger<<" "<<payload<<endl;
}
int main()
{
    std::ios::sync_with_stdio(false);cin.tie(0);
    int v1,c1,c3,t1,t3;
    double v2,c2,t2,t4;
    cin>>v1>>v2>>c1>>c2>>c3>>t1>>t2>>t3>>t4;
    vehicle v(v1,v2);
    car c(c1,c2,c3);
    truck t(t1, t2, t3,t4); 
    v.show();
    cout<<'\n';
    c.show();
    t.show();
    return 0;
}

继承与派生矩形类

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
class Rectangle
{
public:
    int length,width;
    Rectangle(int a,int b){length = a; width = b;}
    int s(){return length*width;}
    ~Rectangle(){}
};

class Cuboid:public Rectangle
{
public:
    int hight;
    Cuboid(int a,int b,int h):Rectangle(a,b){hight = h;}
    int v(){return s()*hight;}
    ~Cuboid(){cout<<"xxxxx"<<endl;}
};
int main()
{
    int m,n,p;
    cin>>m>>n>>p;
    Cuboid cu(m,n,p);
    cout<<cu.v()<<endl;
    return 0;
}

person类派生student类

基类Person派生出一个Student类,并在main()函数中测试这个Student类。
在这里插入图片描述

#include <iostream>
#include <cstring>
#define Maxsize 20
using namespace std;
class Person
{
public:
    Person(const char* s){
        name = new char[strlen(s)+1]; strcpy(name, s);
    }
    ~Person() { delete [] name; }
protected:
    char* name;
};

class Student:public Person
{
public:
  Student(char a[Maxsize],char b[Maxsize],float c):Person(b)
  {
    numb = new char[strlen(a)+1];
    strcpy(numb, a);
    scores = c;
  }
  int print();
   ~Student(){delete [] numb;}
protected:
    float scores;
    char *numb;
};

Student::print() {cout<<numb<<" "<<name<<" "<<scores;}

int main()
{
    char name[Maxsize],num[Maxsize];
    float score;
    cin >> num>>name >> score;
    Student a(num,name,score);
    a.print();
    return 0;
}

总结

本习题是本蒟蒻五月底写的,七月初发布~~太懒啦,主要懒得写总结,~~但是怕时间久了自己会忘记,学习记录者啦 ~~哈哈哈~~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幸愉聊信奥

谢谢亲的支持,我会继续努力啦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值