文章目录
贪吃的长颈鹿继承和派生汽车类继承与派生矩形类person类派生student类总结
贪吃的长颈鹿
长颈鹿是一种动物:动物需要吃食物,因此长颈鹿也需要吃食物;长颈鹿还可以伸长脖子,可以用嘴够树叶吃。请建立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;
}
总结
本习题是本蒟蒻五月底写的,七月初发布~~太懒啦,主要懒得写总结,~~但是怕时间久了自己会忘记,学习记录者啦 ~~哈哈哈~~