首页 > 其他分享 >自留_CPP面向对象习题

自留_CPP面向对象习题

时间:2023-09-03 23:13:33浏览次数:32  
标签:cout int double class display CPP 习题 public 自留

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

相关文章

  • 陈恕行《现代偏微分方程导论》第一章习题参考答案
    可能有错误,如果发现请在评论区指出.第一节1.证明\(C_c^\infty({\mathbb{R}}^n)\)在\(L^p({\mathbb{R}}^n)\)和\(C^0(\mathbb{R}^n)\)中稠密.证明.先证明\(L^p\)的情形,设\(u\inL^p\).对任何\(\varepsilon>0\),取\(R\)充分大,使得\(\|u\|_{L^p(B_R(0)^c)}<\va......
  • 习题纠错05
    以下哪些算法是可以用来求最小生成树()//答案是ADAkruskal算法Bdijkstra算法Cfloyd算法Dprim算法//1.Prim算法(适合稠密图,贪心算法的运用,时间复杂度O(n+e),邻接表存储;O(n^2),图)//2.Kruskal算法(适合稀疏图,贪心算法的运用,时间复杂度O(eloge),e为边数)//求图最短路径......
  • JSONCPP向浏览器前端发送服务器本地文件列表
    服务器解析了浏览器请求之后,要进行响应响应体里需要存放请求的内容HTML标签:是页面的核心内容,定义了页面有什么内容。CSS:控制HTML元素的排版布局和展示方式,是美化页面文档的。JavaScript:让用户与页面进行交互,或在网页背后默默操控网页,以便让显示的内容与效果有所改变。对网页来......
  • 2.分支结构-习题
    1.偶数【题目描述】读入一个正整数a,如果a为偶数输出yes。【输入】一个正整数a。【输出】偶数输出yes,否则什么也不输出。【输入样例】12【输出样例】yesinta;scanf("%d",&a);if(a%2==0){ printf("yes");}2.范围判断【题目描述】读入一个整数,若这个数大于1......
  • 习题纠错03
    表达式"X=A+B(C-D)/E"的后缀表示形式可以是()//答案是CAXAB+CDE/-=BXA+BC-DE/=CXABCD-E/+=DXABCDE+/=//从左到右边遍历这个中缀表达式//X添加到后缀表达式,=入栈,A添加到后缀表达式中//+进入栈,B进入后缀表达式,和(入栈,C进入后缀表达式中//-进入栈,D进入后缀表达式,遇到),-和(出栈......
  • 练习题 - Java编程案例
    当用户在Java程序中输入字符时,我们可以通过代码对这些字符进行判断和处理。在这篇博客中,我们将介绍如何使用Java编写一个程序,根据用户输入的字符进行判断,并进行相应的输出。首先,让我们来看看如何判断一个字符是否为元音字母(a、e、i、o、u)。我们可以使用Scanner类来获取用户输入的字......
  • 2.分支结构习题
    1.偶数【题目描述】读入一个正整数a,如果a为偶数输出yes。【输入】一个正整数a。【输出】偶数输出yes,否则什么也不输出。【输入样例】12【输出样例】yesa=int(input())ifa%2==0:print('yes')2.范围判断【题目描述】读入一个整数,若这个数大于......
  • 报错test_features2d.cpp:51:10: fatal error: features2d/test/test_detectors_regre
    问题描述:ubuntu18.04安装opencv4.5.1+contrib报错test_features2d.cpp:51:10:fatalerror:features2d/test/test_detectors_regression.impl.hpp:没有那个文件或目录解决方法如题,报错如下:解决方法:按照报错提示,将opencv-4.5.1/modules中的features2d文件夹一整个复制到ope......
  • 【OpenCV】features2d_converters.cpp:2:10: fatal error: common.h: 没有那个文件或
    Linux环境下使用opencv的dnn模块调用yolov4遇到的坑(纯CPU)一、问题描述Ubuntu安装opencv4.4,第一次编译完成安装成功,发现编译时少加了几个选项,于是重新编译,结果报如下错误:opencv_contrib-4.4.0/modules/xfeatures2d/test/features2d/misc/java/src/cpp/\features2d_converters.cpp:......
  • 1.顺序结构习题
    1.交换值【题目描述】输入两个正整数a和b,试交换a、b的值(使a的值等于b,b的值等于a)。【输入】输入两个正整数a和b。【输出】输出a与b交换值后的结果。【输入样例】23【输出样例】32a,b=input().split()c=aa=bb=cprint(a,b)2.整数的和【题目描述】求3个整数......