c++面向对象实例题集锦_第1页
c++面向对象实例题集锦_第2页
c++面向对象实例题集锦_第3页
c++面向对象实例题集锦_第4页
c++面向对象实例题集锦_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C++面向对象实例:C++面向对象类的实例题目二题目描述:编写一个程序,设计一个产品类Product,其定义如下:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?class

Product

{

public:

Product(char

*n,int

p,int

q);

//构造函数

~Product();

//析构函数

void

buy(int

money);

//购买产品

void

get()

const;

//显示剩余产品数量

private:

char

*

name;

//产品名称

int

price;

//产品单价

int

quantity;

//剩余产品数量

};

并用数据进行测试。code:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#include<iostream>

#include<cstring>

using

namespace

std;

class

Product

{

char

*name;

int

price;

int

quantity;

public:

Product(char

*n,int

p,int

q);

~Product();

void

buy(int

money);

void

get()const;

};

Product::Product(char

*n,int

p,int

q)

{

name

=

n;

price

=

p;

quantity

=

q;

}

Product::~Product()

{

}

void

Product::buy(int

money)

{

int

r,n;

n

=

money/price;

r

=

money%price;

if(n

>

quantity)

{

cout<<"数量不够"<<endl;

}

else

{

quantity

-=

n;

cout<<"名称:"<<name<<",单价:"<<price<<"元"<<endl;

cout<<"顾客使用"<<money<<"元,购买"<<n<<"台,剩余"<<r<<"元"<<endl;

}

}

void

Product::get()const

{

cout<<"产品:"<<name<<",单价:"<<price<<",剩余:"<<quantity<<"台"<<endl;

}

int

main()

{

Product

p("Iphone6",100,20);

p.buy(10);

p.get();

cout<<"\n==========================\n"<<endl;

p.buy(1000);

p.get();

return

0;

}

输出:C++面向对象类的实例题目三编写一个程序,设计一个满足如下要求的CData类。(1)用下面的格式输出日期:日/月/年(2)输出在当前日期上加一天后的日期(3)设置日期code:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#include<iostream>

using

namespace

std;

class

CData

{

public:

CData(int

y,int

m,int

d);

void

setdate(int

y,

int

m,

int

d);

void

display();

void

add();

private:

int

day;

int

month;

int

year;

};

CData::CData(int

y,int

m,int

d)

{

day

=

d;

month

=

m;

year

=

y;

}

void

CData::setdate(int

y,int

m,int

d)

{

day

=

d;

month

=

m;

year

=

y;

}

void

CData::display()

{

cout<<day<<"/"<<month<<"/"<<year<<endl;

}

void

CData::add()

{

int

a[2][12]={

{31,28,31,30,31,30,31,31,30,31,30,31},

{31,29,31,30,31,30,31,31,30,31,30,31}

};

if((year%400

==

0)||(year%100

!=0

&&

year%4

==0))//闰年的情况

{

if(a[1][month-1]>day)day++;

else

{

month++;

if(month>12)

{

year++;

month

=

1;

}

day

=

1;

}

}

else

//平年的情况

{

if(a[0][month-1]>day)day++;

else

{

month++;

if(month>12)

{

year++;

month

=

1;

}

day

=

1;

}

}

}

int

main()

{

CData

date(2013,12,31);

date.display();

date.add();

date.display();

date.setdate(2014,11,11);

date.display();

date.add();

date.display();

return

0;

}

结果输出:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?31/12/2013

1/1/2014

11/11/2014

12/11/2014

C++面向对象类的实例题目四题目描述:以面向对象的概念设计一个类,此类包含3个私有数据:unlead、lead(无铅汽油和有铅汽油)以及total(当天总收入,无铅汽油的价格是17元/升,有铅汽油的加个是16元/升),请以构造函数方式建立此值。试输入某天所加的汽油量,本程序将列出加油当天的总收入。程序代码:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#include<iostream>

using

namespace

std;

class

Gas

{

public:

Gas(double

ulp,double

lp)

{

unprice

=

ulp;

price

=

lp;

}

void

show()

{

total

=

unlead*unprice

+

lead*price;

cout<<"无铅汽油的价格为17元/升,有铅汽油的价格为16元/升"<<endl;

cout<<"total:"<<total<<endl;

}

void

getdata()

{

cout<<"请输入当天无铅汽油的总量:";

cin>>unlead;

cout<<"请输入当天有铅汽油的总量:";

cin>>lead;

}

private:

double

unprice;

double

price;

double

lead;

double

unlead;

double

total;

};

int

main()

{

Gas

g1(17,16);

g1.getdata();

g1.show();

return

0;

}

程序输出:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?请输入当天无铅汽油的总量:10

请输入当天有铅汽油的总量:20

无铅汽油的价格为17元/升,有铅汽油的价格为16元/升

total:490

C++面向对象类的实例题目五题目描述:编写一个程序,采用一个类求n!,并输出5!的值。程序代码:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#include<iostream>

using

namespace

std;

class

CFactorial

{

public:

CFactorial(int

n)

{

num

=

n;

total

=

1;

}

void

calculate()

{

int

n

=

num;

while(n>0)

{

total

*=

n--;

}

}

void

display()

{

cout<<num<<"!

=

"<<total<<endl;

}

private:

int

num;

long

total;

};

int

main()

{

CFactorial

f(5);

f.calculate();

f.display();

return

0;

}

程序输出:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?5!

=

120

C++面向对象类的实例题目六问题描述:编写一个程序计算两个给定长方形的面积,其中在设计类成员函数addarea()(用于计算两个长方形的总面积)时使用对象作为参数。程序代码:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#include<iostream>

using

namespace

std;

class

Rectangular

{

public:

Rectangular(double

w,double

l)

{

width

=

w;

length

=

l;

}

double

getc()

{

circumference

=

width

+

length;

return

circumference;

}

double

adddata(Rectangular

&r)

{

return

(circumference

+

r.getc());

}

private:

double

width;

double

length;

double

circumference;

};

int

main()

{

Rectangular

r1(2,3);

cout<<"Circumference

of

r1

="<<r1.getc()<<endl;

Rectangular

r2(3,4);

cout<<"Circumference

of

r2

="<<r2.getc()<<endl;

cout<<"Circumference

of

r1+r2

="<<r1.adddata(r2)<<endl;

return

0;

}

结果输出:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?Circumference

of

r1

=6

Circumference

of

r2

=12

Circumference

of

r1+r2

=18

C++面向对象类的实例题目七题目描述:编写两个有意义的类,使一个类嵌套在另一个类中。分析:本题涉及两个类student和cdegree,前者为学生类,包含学生的学号(nubner),姓名(name)和成绩(degree),而成绩degree是类cdegree的对象。cdegree类有3个数据成员,分别为数学(math),英语(english)和物理(phy)分数。程序代码:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#include<iostream>

#include<string>

using

namespace

std;

class

Student

{

public:

void

getdata();

void

showdata();

private:

string

number;

string

name;

class

Cdegree

{

public:

double

math;

double

english;

double

phy;

}degree;

};

void

Student::getdata()

{

cout<<"Input

number:";

cin>>number;

cout<<"Input

name:";

cin>>name;

cout<<"Input

degree

of

math:";

cin>>degree.math;

cout<<"Input

degree

of

english:";

cin>>degree.english;

cout<<"Input

degree

of

physics:";

cin>>degree.phy;

}

void

Student::showdata()

{

cout<<"=========分割线======="<<endl;

cout<<"Number:"<<number<<endl;

cout<<"Name:"<<name<<endl;

cout<<"Math"<<degree.math<<endl;

cout<<"English:"<<degree.english<<endl;

cout<<"Physics:"<<degree.phy<<endl;

}

int

main()

{

Student

s1;

s1.getdata();

s1.showdata();

return

0;

}

结果输出:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?Input

number:007

Input

name:qianshou

Input

degree

of

math:89

Input

degree

of

english:99

Input

degree

of

physics:100

=========分割线=======

Number:007

Name:qianshou

Math89

English:99

Physics:100

C++面向对象类的实例题目八题目描述:编写一个程序输入3个学生的英语和计算机成绩,并按照总分从高到低排序。要求设计一个学生类Student,其定义如下:程序代码:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#include<iostream>

using

namespace

std;

class

Student

{

public:

void

getscore();

//获取一个学生成绩

void

display();

//显示一个学生成绩

void

sort(

Student

*);

//将若干个学生按总分从高到低排序

private:

int

english;

int

computer;

int

total;

};

void

Student::getscore()

{

cout<<"请输入该学生的英语成绩:";

cin>>english;

cout<<"请输入该学生的计算机成绩:";

cin>>computer;

total

=

english

+

computer;

}

void

Student::display()

{

cout<<"该学生的英语成绩为:"<<english<<",计算机成绩为:"<<computer<<",总分为:"<<total<<endl;

}

void

Student::sort(Student

*p)

{

if(p->total

>

total)

//p指向的对象比该对象大的时候,则交换对象的值

{

int

t1,t2,t3;

t1

=

p->english;

p->english

=

english;

english

=

t1;

t2

=

p->computer;

p->computer

=

computer;

computer

=

t2;

t3

=

p->total;

p->total

=

total;

total

=

t3;

}

}

int

main()

{

Student

st[3];

for(int

i

=

0;

i

<

3;

i++)

{

st[i].getscore();

st[i].display();

}

st[0].sort(&st[1]);

st[0].sort(&st[2]);

st[1].sort(&st[2]);

cout<<"======================"<<endl;

cout<<"排序结果如下:"<<endl;

for(int

i

=

0;

i

<

3;

i++)

{

st[i].display();

}

}

输出结果:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?请输入该学生的英语成绩:80

请输入该学生的计算机成绩:90

该学生的英语成绩为:80,计算机成绩为:90,总分为:170

请输入该学生的英语成绩:70

请输入该学生的计算机成绩:60

该学生的英语成绩为:70,计算机成绩为:60,总分为:130

请输入该学生的英语成绩:99

请输入该学生的计算机成绩:87

该学生的英语成绩为:99,计算机成绩为:87,总分为:186

======================

排序结果如下:

该学生的英语成绩为:99,计算机成绩为:87,总分为:186

该学生的英语成绩为:80,计算机成绩为:90,总分为:170

该学生的英语成绩为:70,计算机成绩为:60,总分为:130

C++面向对象类的实例题目九题目描述:编写一个学生和老师数据输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名、输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。程序代码:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#include<iostream>

#include<string>

using

namespace

std;

class

Person

{

public:

void

get()

{

cout<<"请输入编号:";

cin>>number;

cout<<"请输入姓名:";

cin>>name;

}

void

show()

{

cout<<"NO."<<number<<endl;

cout<<"name:"<<name<<endl;

}

private:

string

number;

string

name;

};

class

Student:public

Person

{

public:

void

get()

{

Person::get();

cout<<"请输入班级编号:";

cin>>class_number;

cout<<"请输入成绩:";

cin>>grade;

}

void

show()

{

Person::show();

cout<<"class_number:"<<class_number<<endl;

cout<<"grade:"<<grade<<endl;

}

private:

string

class_number;

float

grade;

};

class

Teacher:public

Person

{

public:

void

get()

{

Person::get();

cout<<"请输入职称:";

cin>>title;

cout<<"请输入部门:";

cin>>department;

}

void

show()

{

Person::show();

cout<<"title:"<<title<<endl;

cout<<"department:"<<department<<endl;

}

private:

string

title;

string

department;

};

int

main()

{

Student

s1;

Teacher

t1;

cout<<"输入一个学生数据:"<<endl;

s1.get();

cout<<"输出一个学生数据:"<<endl;

s1.show();

cout<<"==========================="<<endl;

cout<<"输入一个老师数据:"<<endl;

t1.get();

cout<<"输出一个老师数据:"<<endl;

t1.show();

return

0;

}

结果输出:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?输入一个学生数据:

请输入编号:001

请输入姓名:qianshou

请输入班级编号:003

请输入成绩:87.5

输出一个学生数据:

NO.001

name:qianshou

class_number:003

grade:87.5

===========================

输入一个老师数据:

请输入编号:007

请输入姓名:kkx

请输入职称:professor

请输入部门:seventh

输出一个老师数据:

NO.007

name:kkx

title:professor

department:seventh

C++面向对象类的实例题目十题目描述:编写一个程序,其中有一个汽车类vehicle,它具有一个需要传递参数的构造函数,类中的数据成员:车轮个数wheels和车重weight放在保护段中;小车类car是它的私有派生类,其中包含载人数passager_load;卡车类truck是vehicle的私有派生类,其中包含载人数passager_load和载重量payload。每个类都用相关数据的输出方法。程序代码:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#include<iostream>

using

namespace

std;

class

Vehicle

{

public:

Vehicle(int

wl,double

wh):wheels(wl),weight(wh){};

void

show()

{

cout<<"wheels:"<<wheels<<",weight:"<<weight<<endl;

}

protected:

int

wheels;

double

weight;

};

class

Car:private

Vehicle

{

public:

Car(int

wl,double

wh,int

pl):Vehicle(wl,wh),passager_load(pl){};

void

show()

{

Vehicle::show();

cout<<"passager_load:"<<passager_load<<endl;

}

private:

int

passager_load;

};

class

Truck:private

Vehicle

{

public:

Truck(int

wl,double

wh,int

pl,double

psl):Vehicle(wl,wh),passager_load(pl),payload(psl){};

void

show()

{

Vehicle::show();

cout<<"passager_load:"<<passager_load<<endl;

cout<<"payload:"<<payload<<endl;

}

private:

int

passager_load;

double

payload;

};

int

main()

{

Vehicle

v1(4,200);

v1.show();

cout<<"===================="<<endl;

Car

c1(4,290,10);

c1.show();

cout<<"===================="<<endl;

Truck

t1(4,500,50,200);

t1.show();

return

0;

}

结果输出:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?wheels:4,weight:200

====================

wheels:4,weight:290

passager_load:10

====================

wheels:4,weight:500

passager_load:50

payload:200

C++面向对象类的实例题目十一题目描述:写一个程序计算三角形,正方形和圆形3种图形的面积程序代码:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#include<iostream>

#include<cmath>

#define

PAI

3.1415

using

namespace

std;

class

Shape

{

public:

virtual

float

area()

//定义一个求面积的成员函数

{

return

0;

}

virtual

void

ShapeName()

=

0;//定义一个纯虚函数

};

class

Triangle:public

Shape

{

public:

Triangle(float

x,float

y,float

z):a(x),b(y),c(z){};

void

ShapeName()

{

cout<<"Triangle:"<<endl;

}

float

area()

{

float

p

=

(a+b+c)/2;

float

s

=

sqrt(p*(p-a)*(p-b)*(p-c));

return

s;

}

private:

float

a,b,c;

};

class

Square:public

Shape

{

public:

Square(float

l):length(l){};

void

ShapeName()

{

cout<<"Square:"<<endl;

}

float

area()

{

float

s

=

length

*

length;

return

s;

}

private:

float

length;

};

class

Circle:public

Shape

{

public:

Circle(float

r):radius(r){};

void

ShapeName()

{

cout<<"Square:"<<endl;

}

float

area()

{

float

s

=

PAI*radius*radius;

return

s;

}

private:

float

radius;

};

int

main()

{

Shape

*pt;

pt

=

new

Triangle(3,4,5);

pt->ShapeName();

cout<<"Area:"<<pt->area()<<endl;

cout<<"================================"<<endl;

pt

=

new

Square(2.5);

pt->ShapeName();

cout<<"Area:"<<pt->area()<<endl;

cout<<"================================"<<endl;

pt

=

new

Circle(2.5);

pt->ShapeName();

cout<<"Area:"<<pt->area()<<endl;

return

0;

}

结果输出:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?Triangle:

Area:6

================================

Square:

Area:6.25

================================

Square:

Area:19.6344

C++面向对象类的实例题目十二题目描述:写一个程序计算正方体、球体和圆柱体的表面积和体积程序代码:[cpp]\o"viewplain"viewplain\o"copy"copy\o"print"print\o"?"?#include<iostream>

#define

PAI

3.1415

using

namespace

std;

class

Shape

{

public:

virtual

void

ShapeName()=0;

virtual

void

area()

{

return

;

}

virtual

void

volume()

{

return

;

}

};

class

Cube:public

Shape

{

public:

Cube(float

len):length(len){};

void

ShapeName()

{

cout<<"Cube:"<<endl;

}

void

area()

{

double

s

=

6*length*length;

cout<<"Area:"<<s<<endl;

}

void

volume()

{

double

v

=

length*length*length;

cout<<"Volume:"<<v<<endl;

}

private:

float

length;

};

class

Sphere:public

Shape

{

public:

Sphere(float

r):radius(r){};

void

ShapeName()

{

cout<<"Sphere:"<<endl;

}

void

area()

{

double

s

=

4*radius*radius*PAI;

cout<<"Area:"<<s<<endl;

}

void

volume()

{

double

v

=

(4*radius*radius*radius*PAI)/3;

cout<<"Volume:"<<v<<endl;

}

private:

float

radius;

};

class

Cylinder:public

Shape

{

public:

Cylinder(float

r,float

h):radius(r),length(h){};

void

ShapeName()

{

cout<<"Cylinder:"<<endl;

}

void

area()

{

double

s

=

radius*radius*PAI

+

2*PAI*radius*length;

cout<<"Area:"<<s<<endl;

}

void

volume()

{

double

v

=

radius*radius*PAI*length;

cout<<"Volume:"<<v<<endl;

}

private:

float

radius;

float

length;

};

int

main()

{

Shape

*

pt;

pt

=

new

Cube(2);

pt->ShapeName();

pt->area();

pt->volume();

cout<<"==========================="<<endl;

pt

=

new

Sphere(2);

pt->ShapeName();

pt->area();

pt->volume();

cout<<"==========================="<<endl;

pt

=

new

Cylinder(2,2);

pt->ShapeName();

pt->area();

pt->volume();

cout<<"==========================="<<endl;

}

结果输出:[cpp]HYPERLINK"/zhez

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论