Question
第一部分 C++面向对象练习题
-
1
定义盒子类Box,包括三个private类型数据成员x,y,z,分别代表其长、宽、高。类中包括有参构造函数,计算体积的private类型成员函数volume和public类型显示函数display。在主函数中,定义对象box1(10,20,30),调用相关函数显示该盒子对象的长、宽、高以及其体积。
-
2
定义盒子类Box,包括三个private类型数据成员x,y,z,分别代表其长、宽、高。类中包括有参构造函数和无参构造函数(此时,x、y、z初值分别设置为5,10,20),计算体积的private类型成员函数volume和public类型显示函数display。在主函数中,定义对象box1()和box2(10,20,30),分别调用相关函数显示该盒子对象的长、宽、高以及其体积。
-
3
定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。编写完整程序验证。
-
4
定义一个字符串类String,用来存放不定长的字符串,重载运算符“==”,用于两个字符串的等于比较运算。编写完整程序验证。
-
5
定义一个描述学生情况的类Student,包括private类型的数据成员num、name、sex,成员函数包括有参构造函数和显示数据成员的public类型display函数。在主函数中,定义对象stud1,并显示对象的值。
-
6
先建立一个Point(点)类,包括private类型数据成员x、y(坐标点)。以他为基类,派生出Circle(圆)类,增加数据成员r(半径)。再以Circle为直接基类,派生出Cylinder(圆柱体)类,再增加数据成员h(高度)。重载运算符“>>”和“<<”,使之能够输出以上类对象。
-
7
定义抽象类Shape,由他派生三个类:Circle(圆),Rectangle(长方形),Trapzoid(梯形),用一个函数printArea分别输出三者的面积,三个图形的数据在定义对象时给定。
-
8
定义一个人员类Cperson,包括private类型数据成员编号、姓名、性别和用于输入输出的成员函数,在此基础上,派生出学生类Cstudent(增加分数)和教师类Cteacher(增加教龄),并实现对学生和教师信息的输入输出。
-
9
定义抽象类Shape,由它派生出五个类:Circle(圆),Square(正方形)、Rectangle(长方形),Trapezoid(梯形),Triangle(三角形)。用虚函数分别计算五个图形的面积,并求它们的面积之和。要求用基类指针数组,使它的每一个元素指向一个派生类的对象。
-
10
定义一个描述学生情况的类Student,包括protected类型的数据成员num、name、score,有参构造函数和显示三个数据成员的display函数。定义Student类的公有派生类Graduate,它包括有参构造函数和显示四个数据成员的display函数,并增加private数据成员pay。采用虚函数和动态多态性概念,输出派生类对象的内容。
-
11
类rect有两个保护数据成员float len、float wid,有一个有参构造函数,以及显示两个数据成员的公有成员函数display()。类crect为类rect的public派生类,增加私有数据成员float hei,有一个有参构造函数和可显示三个数据成员的公有成员函数display()。 写出完整的程序,实现下列功能。在主函数中,定义基类指针和派生类对象crect1(其三个数据值为1.1、2.2、3.3)。定义虚函数display(),使用动态多态性概念,输出crect1对象的三个成员值。
-
12
类rect有两个保护数据成员float len、float wid,有一个有参构造函数,以及显示两个数据成员的保护成员函数display()。类crect为类rect的public派生类,增加私有数据成员float hei,有一个有参构造函数和可显示三个数据成员的公有成员函数display1()。 写出完整的程序,实现下列功能。在主函数中,定义派生类对象crect1(其三个数据值为1.1、2.1、3.1)并输出crect1对象的三个成员值。
-
13
#include <string> #include <iostream> using namespace std; class Cperson { protected: void setvalue(int n,string nam,char s ) { num=n; name=nam; sex=s; } void display( ) { cout<<"num: "<<num<<endl;cout<<"name: "<<name<<endl; cout<<"sex: "<<sex<<endl;} private : int num; string name; char sex; };
(1)、设计新类Cperson1为类Cperson的public派生类,增加private数据成员int age。
(2)、 添加类Cperson1的public类型成员函数setvalue1和display1。setvalue1为其四个数据成员赋值,display1可以输出四个数据成员。
(3)、在主函数中,定义类Cperson1的对象person1,使用setvalue1函数为其四个数据成员赋值(10010,"Wang",'m',36),并使其正确输出person1的四个数据成员。 -
14
定义Date类,它包含protected访问类型的三个数据成员int year;int month;int day, 有参构造函数以及public类型输出显示函数displaydate()。定义Time类,它包含protected访问类型的三个数据成员int hour;int minute;int second, 有参构造函数以及public类型输出显示函数displaytime()。定义派生类DateTime类,类Date和类Time为其直接基类,均为public继承方式,一个有参构造函数及public类型输出显示函数display()。在主函数中定义DateTime对象dt1(2019,2,19,0,30,50),使其形式:2019-2-19;10:30:50 。
-
15
定义点类class point,它包括private 访问类型的double x(横坐标)和double y(纵坐标),有参构造函数以及求面积的纯虚函数double area()。定义长方形的类class rectangle,class point为其直接基类,public继承方式,增加private访问类型的double w(长)和double h(宽),一个有参构造函数,重新定义计算面积的函数double area()。在主函数中,定义类rectangle的对象r1(50,60,10,20),其中(50,60)为坐标,(10,20)为长和宽,利用动态多态性输出r1的面积。
Answer
题目一
#include<iostream>
using namespace std;
class box{
public:
box(double X,double Y,double Z){
x=X;
y=Y;
z=Z;
}
void display(){
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
cout<<"V="<<volume(x,y,z)<<endl;
}
private:
double x,y,z;
double volume(double a,double b,double c){
return a*b*c;
}
};
int main(){
box box1(10,20,30);
box1.display();
}
题目二
#include<iostream>
using namespace std;
class box{
public:
box(){
x=5;
y=10;
z=20;
}
box(double X,double Y,double Z){
x=X;
y=Y;
z=Z;
}
void display(){
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
cout<<"V="<<volume(x,y,z)<<endl;
}
private:
double x,y,z;
double volume(double a,double b,double c){
return a*b*c;
}
};
int main(){
box box1;
box1.display();
box box2(10,20,30);
box2.display();
}
第三题
#include<iostream>
using namespace std;
class Complex{
public:
Complex(double R=0,double I=0){
real=R;
imag=I;
}
void display(){
if(real==0){
if(imag==0){
cout<<0;
}else{
cout<<imag<<"i";
}
}else {
if(imag==0){
cout<<real;
}else if(imag<0){
cout<<real<<imag<<"i";
}else{
cout<<real<<"+"<<imag<<"i";
}
}
}
Complex operator + (Complex cpx);
private:
double real;
double imag;
};
Complex Complex::operator + (Complex cpx){
return Complex(real+cpx.real,imag+cpx.imag);
}
int main(){
Complex num1(3.5,2);
Complex num2(2.2,-3);
Complex rst=num1+num2;
rst.display();
}
第四题
当时做的不好
#include<iostream>
#include<cstring>
using namespace std;
class String{
public:
String(char* mos){
str=new char[strlen(mos)+1];
strcpy(str,mos);
}
bool operator == (String sr){
if(strcmp(str,sr.str)==0){
return true;
}
return false;
}
~String(){
delete[] str;
}
private:
char* str;
};
int main(){
String st("DreamChaser");
String sy("RoboMaster");
cout<<(st==sy);
}
第五题
#include<iostream>
#include<string> //记得加!!!
using namespace std;
class Student{
public:
Student(int NU,string NA,char S){
num=NU;
name=NA;
sex=S;
}
void display(){
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}
private:
int num;
string name;
char sex;
};
int main(){
Student stu1(1,"Santerc",'M');
stu1.display();
}
第六题
#include<iostream>
#include<string>
using namespace std;
class point {
public:
point() {
x = 0;
y = 0;
}
point(double X, double Y) {
x = X;
y = Y;
}
void display() {
cout << "x=" << x << endl;
cout << "y=" << y << endl;
}
friend ostream& operator << (ostream& os, point& po);
friend istream& operator >> (istream& is, point& po);
private:
double x;
double y;
};
class circle : public point {
public:
circle() :point() {
r = 0;
}
circle(double X, double Y, double R) :point(X, Y) {
r = R;
}
void display() {
point::display();
cout << "r=" << r << endl;
}
friend ostream& operator << (ostream& os, circle& c);
friend istream& operator >> (istream& is, circle& c);
private:
double r;
};
class Cylinder : public circle {
public:
Cylinder() :circle() {
h = 0;
}
Cylinder(double X, double Y, double R, double H) :circle(X, Y, R) {
h = H;
}
void display() {
circle::display();
cout << "h=" << h << endl;
}
friend ostream& operator << (ostream& os, Cylinder& cy);
friend istream& operator >> (istream& is, Cylinder& cy);
private:
double h;
};
ostream& operator << (ostream& os, point& po) {
po.display();
return os;
}
istream& operator >> (istream& is, point& po) {
int a, b;
cout << "x=";
is >> a;
cout << "y=";
is >> b;
po=point(a, b);
return is;
}
ostream& operator << (ostream& os, circle& c) {
c.display();
return os;
}
istream& operator >> (istream& is, circle& c) {
int a, b, cr;
cout << "x=";
is >> a;
cout << "y=";
is >> b;
cout << "r=";
is >> cr;
c=circle(a, b, cr);
return is;
}
ostream& operator << (ostream& os, Cylinder& cy) {
cy.display();
return os;
}
istream& operator >> (istream& is, Cylinder& cy) {
int a, b, c, d;
cout << "x=";
is >> a;
cout << "y=";
is >> b;
cout << "r=";
is >> c;
cout << "h=";
is >> d;
cy=Cylinder(a, b, c, d);
return is;
}
int main() {
point p;
circle c;
Cylinder cy;
cin >> p >> c >> cy;
cout << p << c << cy;
}
第七题
#include<iostream>
using namespace std;
class shape{
public:
virtual void Area()=0;
};
class Rectangle:public shape{
public:
Rectangle(double L,double H){
len=L;
hei=H;
}
void Area(){
cout<<len*hei<<endl;
}
private:
double len;
double hei;
};
class Circle:public shape{
public:
Circle(double R){
r=R;
}
void Area(){
cout<<3.14*r*r<<endl;
}
private:
double r;
};
class Trapzoid:public shape{
public:
Trapzoid(double UD,double LD,double H){
upper_l=UD;
lower_l=LD;
h=H;
}
void Area(){
cout<<0.5*(upper_l+lower_l)*h<<endl;
}
private:
double upper_l;
double lower_l;
double h;
};
int main(){
Rectangle RC(3,5);
Circle CR(10);
Trapzoid TZ(3,4,5);
RC.Area();
CR.Area();
TZ.Area();
}
第八题
#include<iostream>
#include<string>
using namespace std;
class Cperson{
private:
string id;
string name;
string sex;
public:
Cperson(){};
Cperson(string ID,string NAME,char SEX){
id=ID;
name=NAME;
sex=SEX;
}
virtual void input(){
cout<<"ID: ";
cin>>id;
cout<<"Name: ";
cin>>name;
cout<<"Sex: ";
cin>>sex;
}
virtual void display(){
cout<<"ID: "<<id<<endl<<"Name: "<<name<<endl<<"Sex: "<<sex<<endl;
}
};
class Cstudent: public Cperson{
public:
Cstudent(){};
Cstudent(string ID,string NAME,char SEX,int MARK){
Cperson(ID,NAME,SEX);
mark=MARK;
}
void input(){
Cperson::input();
cout<<"Mark: ";
cin>>mark;
}
void display(){
Cperson::display();
cout<<"Mark: "<<mark<<endl;
}
private:
int mark;
};
class Cteacher: public Cperson{
public:
Cteacher(){};
Cteacher(string ID,string NAME,char SEX,int AGE){
Cperson(ID,NAME,SEX);
age=AGE;
}
void input(){
Cperson::input();
cout<<"Teaching years: ";
cin>>age;
}
void display(){
Cperson::display();
cout<<"Teaching years: "<<age<<endl;
}
private:
int age;
};
int main(){
Cstudent st;
st.input();
Cteacher te;
te.input();
st.display();
te.display();
}
第九题
/*稍稍改了一点题*/
#include<iostream>
using namespace std;
class shape{
public:
virtual double Area()=0;
};
class Rectangle:public shape{
public:
Rectangle(double L,double H){
len=L;
hei=H;
}
double Area(){
cout<<len*hei<<endl;
return len*hei;
}
private:
double len;
double hei;
};
class Circle:public shape{
public:
Circle(double R){
r=R;
}
double Area(){
cout<<3.14*r*r<<endl;
return 3.14*r*r;
}
private:
double r;
};
class Trapzoid:public shape{
public:
Trapzoid(double UD,double LD,double H){
upper_l=UD;
lower_l=LD;
h=H;
}
double Area(){
cout<<0.5*(upper_l+lower_l)*h<<endl;
return 0.5*(upper_l+lower_l)*h;
}
private:
double upper_l;
double lower_l;
double h;
};
int main(){
Rectangle RC(3,5);
Circle CR(10);
Trapzoid TZ(3,4,5);
shape* pt[3];
pt[0]=&RC;
pt[1]=&CR;
pt[2]=&TZ;
double S=pt[0]->Area()+pt[1]->Area()+pt[2]->Area();
cout<<S<<endl;
}
第十题
#include<iostream>
#include<string>
using namespace std;
class student{
protected:
int num;
int score;
string name;
public:
student(int NUM,string NAME,int SCORE){
num=NUM;
name=NAME;
score=SCORE;
}
virtual void display(){
cout<<"Num: "<<num<<endl<<"Name: "<<name<<endl<<"Score: "<<score<<endl;
}
};
class Graduate:public student{
private:
int pay;
public:
Graduate(int NUM,string NAME,int SCORE,int PAY):student(NUM,NAME,SCORE){
pay=PAY;
}
void display(){
student::display();
cout<<"Pay: "<<pay<<endl;
}
};
int main(){
Graduate gu(1,"Santerc",100,20000);
gu.display();
}
第十一题
/*是谁忘加了标准头呢,不能再犯这样愚蠢的错误了*/
#include <iostream>
using namespace std;
class rect{
protected:
float len;
float wid;
public:
rect(float L,float W){
len=L;
wid=W;
}
virtual void display(){
cout<<"Lenth:"<<len<<endl;
cout<<"Width:"<<wid<<endl;
}
};
class crect: public rect{
private:
float hei;
public:
crect(float L,float W,float H):rect(L,W){
hei=H;
}
void display(){
rect::display();
cout<<"Height:"<<hei<<endl;
}
};
int main(){
rect* p_rec;
crect crect1(1.1,2.2,3.3);
p_rec=&crect1;
p_rec->display();
return 0;
}
第十二题
#include <iostream>
using namespace std;
class rect{
protected:
float len;
float wid;
public:
rect(float L,float W){
len=L;
wid=W;
}
void display(){
cout<<"Lenth:"<<len<<endl;
cout<<"Width:"<<wid<<endl;
}
};
class crect: public rect{
private:
float hei;
public:
crect(float L,float W,float H):rect(L,W){
hei=H;
}
void display1(){
display();
cout<<"Height:"<<hei<<endl;
}
};
int main(){
crect crect1(1.1,2.1,3.1);
crect1.display1();
return 0;
}
第十三题
#include <string>
#include <iostream>
using namespace std;
class Cperson
{
protected:
void setvalue(int n,string nam,char s )
{ num=n; name=nam; sex=s; }
void display( )
{ cout<<"num: "<<num<<endl;cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;}
private :
int num; string name; char sex;
};
class Cperson1: public Cperson{
private:
int age;
public:
void setvalue1(int n,string nam,char s,int a){
setvalue(n,nam,s);
age=a;
}
void display1( )
{
display();
cout<<"age: "<<age<<endl;}
};
int main(){
Cperson1 person1;
person1.setvalue1(10010,"Wang",'m',36);
person1.display1();
}
第十四题
#include<iostream>
using namespace std;
class Data{
protected:
int year;
int month;
int day;
public:
Data(int Y,int M,int D){
year=Y;
month=M;
day=D;
}
void displaydata(){
cout<<year<<"-"<<month<<"-"<<day<<";";
}
};
class Time{
protected:
int hour;
int minute;
int second;
public:
Time(int H,int MI,int S){
hour=H;
minute=MI;
second=S;
}
void displaytime(){
cout<<hour<<":"<<minute<<":"<<second<<"。";
}
};
class DataTime :public Data,public Time{
public:
DataTime(int Y,int M,int D,int H,int MI,int S):Data(Y,M,D),Time(H,MI,S){
}
void display(){
displaydata();
cout<<" ";
displaytime();
}
};
int main(){
DataTime dt1(2019,2,19,0,30,50);
dt1.display();
}
第十五题
#include<iostream>
using namespace std;
class point{
private:
double x;
double y;
public:
point(double X,double Y){
x=X;
y=Y;
}
virtual double area()=0;
};
class rectangle : public point{
private:
double w;
double h;
public:
rectangle(double X,double Y,double W;double H):point(X,Y){
w=W;
h=H;
}
double area(){
return w*h;
}
};
int main(){
rectangle r1(50,60,10,20);
cout<<r1.area()<<endl;
}
标签:cout,int,double,class,display,CPP,习题,public,自留
From: https://www.cnblogs.com/Santerc/p/17675806.html