2025年C 编程实践与技巧深度解析_第1页
2025年C 编程实践与技巧深度解析_第2页
2025年C 编程实践与技巧深度解析_第3页
2025年C 编程实践与技巧深度解析_第4页
2025年C 编程实践与技巧深度解析_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

面向对象程序设计

(C++)

试验一C++到C的扩充

一、试验目的

1.初步理解C++.

2.理解vc6++的试用

二、试验任务

#include<iostream>

usingnamespacestd;

intmain()

{intadd(inlx,in(y);

inta,b,c;

cin»a»b;

c=add(a,b);

cout«"a+b="«c«endl;

return0;

}

intadd(intx,inty)

{intz;

z=x+y;

retjrn(z);

}

三、试验成果

章]SettDocuaLents\Debuc\Cpp1.exe

14~

A*b-5

Pi*cja3Ant/keyt:oeont:i.nue

四、试验问题

试验原始程序(书第一章习题9)

#includc<iostrcam>

usingnamespacestd;

intmain()

(

inta.b;

c=add(a.b)

cout«Ma+b=r'«c«endl;

return0;

)

intadd(intx,inty);

(

z=x+y;

retrun(z);

1

发现7个错误:

(1)对add函数未申明就调用,应在main函数中对add函数进行申明。

(2)定义add函数时,函数首行末尾不应有分号。

(3)变量c未经定义。

(4)add函数中的变量z未经定义。

(5)第6行末尾少了一种分号。

(6)add函数中的retnm拼写错误,应为return。编译系统把retrun作为未申明的标识符

而报错,由于retrun(z)会被认为是函数调用的形式。

(7)变量a和b未被赋值。

改正后的程序如上:

试验汇报二类与对象

一、试验目的

I;掌握类的定义和使用;

2掌握类对象的申明;复习具有不一样访问属性的组员的访问方式;

3观测构造函数和析构函数的执行过程:学习类组合使用措施

二、试验任务

定义一种Box类,包括lcngth(长),width(宽)height(高)属性,规定函数实现如下功能:

(1)有键盘分别输入2个长方柱的长宽高;

(2)计算其体积;

(3)输出2个长方柱的体积。

定义•种Body类,拥有身高、体重等数据,可以根据身高、体重检测身体状况,规定

设计对应的函数组员和构造函数。

三、试验成果

源文献1:

#includc<iostrcam>

usingnamespacestd;

classBox

{public:

voidgct_valuc();

floatvolumn();

voiddisplay();

public:

floatlength;

floatwidth;

floatheight;

};

voidBox::get_value()

{cout«"plcaseinputlength,width,height:";

cin»Iength;

cin»width;

cin»height;

I

floatBox::volumn()

{return(length*width*height);)

voidBox::display()

{cout«volumn()«endl;}

intmain()

{Boxboxl,box2;

boxl.get_value();

cout«"volumnofboxIis";

boxI.displayO;

box2.get_value();

cout«"volumnofbox2is";

box2.display();

return0;

c''DocumentsandSettings\user\Debug\l.exe*X

pleaseinputlength,width,height:10.524.512.3

volunnofboxlis3164.18

pleaseinputlength,width,height:4.17.56.3

volunnofbox2is193.?25

Pressanykeytocontinue.

—id

源文献2:

#include<iostream>

usingnamespacestd;

classbody

public:

body()

cout«n构造函数被调用"vvendl;

)

~body()

(

cout«"析构函数被调用“<<cndl;

)

voidset(doubleh,doublew)

(

HIGH=h;

WEIGHT=w;

}

voidshow()

(

coul«nHIGH="«HIGH«endl;

cout«"WEIGHT="«WEIGHT«endl;

)

private:

doubleHIGH;

doubleWEIGHT;

};

intmain()

(

doublei,j;

bodya;

cout<<”请输入身高(cm)和体重(kg)"v<endl;

cin»i»j;

a.set(i,j);

a.show();

四、试验问题

C++提供了构造函数(constructor)来处理对象的初始化。

构造函数是一种特殊的组员函数,与其他组员函数不一样,不需要顾客来调

用它,而是在建立对象时自动执行。

构造函数的名字必须与类名同名,而不能由顾客任意命名,以便编译系统能

识别它并把它作为构造函数处理。

它不具有任何类型,不返回任何值。

试验汇报三继承与派生

一、试验目的

I.学习定义和使用类的继承关系,定义派生类;

2熟悉不一样继承方式卜一对基类组员的访问控制;

3学习运用虚基类处理二义性问题。

二、试验任务

1.定义一种基类Animal,有私有整型组员变量age,构造其派生类dog,在其组员函数

SetAge(inln)中直接给age赋值,看看会有什么问题,把age改为公有或保护组员变量,还会

有问题吗?编程试试看,阐明了什么原则?

2定义一种车(vehicle)基类,具有MaxSpeed、Weight等组员变量,Run、Stop等组员函数,

由此派生出自行车(bicycle)类,汽车(motorcar)类。自行车(bicycle)类有高度(Height)

等属性,汽车(motorcycle)类有座位数(ScatNum)等属性。从bicycle和Motorcar派生出

摩托车(motorcycle)类,在继承过程中,注意把vehicle设置为虚基类。假如不把vehicle设置

为虚基类,会有什么问?编程试式看。

三、试验成果

1源程序

#include<iostream>

usingnamespacestd;

classAnimal

public:

AnimalO

cout«"Animal构造函数被调用"«endl;

}

-AnimalO

(

cout«"Animal析构函数被调fflH«endl;

//private:

//protected:

iniage;

1;

classdog:publicAnimal

(

public:

dog()

cout«"dog构造函数被调用"<<endl;

〜dog()

cout«"dog析构函数被调用"vvendl;

voidSetAge(intn);

voidshow()

(

COUt<<"年龄是:,

cout«agc«cndl;

voiddog::SctAgc(intn)

n=3;

age=n;

intmain()

intt;

dogb;

b.SetAge(t);

b.showQ;)

\DocuaentsandSettings\uscr\Debuc\qq.exe*-nx

Animal构遥鬲

d。"攀函数被调角

&帚乐易球四甩,

Animal析祈的数检调用

P>*essan</keytocontinue

2源程序

#include<iostream>

usingnamespacestd;

classvehicle

public:

vehicle()

cout«"vchicle构造函数被调用"《endl;

~vehicle()

cout«"vehicle析构函数被调用"vvendl;

)

voidRun()

inti,j;

cout<<”请输入最大重量和最大速度:"<<endl;

cin»i»j;

MaxSpccd=i;

Weight=j;

voidStopO

cout«"MaxSpeed="«MaxSpeed«endl;

cout«"Weight=n«Weight«endl;

)

private:

doubleMaxSpeed.Weight;

};

classbicycle:virtualpublicvehicle

(

public:

bicycle()

(

cout«"bicycle构造函数被调用"vvendl;

)

〜bicycle。

(

cout«"bicycle析构函数被调用"《endl;

I

private:

doubleHeight;

1;

classmotorcar:virtualpublicvehicle

(

public:

motorcarO

(

cout«"motorcar构造函数被调用"<<cndl;

)

~motorcar()

(

cout«"motorcar析构M数被调用"vvcndl;

)

private:

intSeatNum;

1;

classmotorcycle:publicmotorcar,publicbicycle

public:

motorcycle()

(

cout«"motorcycle构造函数被调用"<<cndl;

)

~motorcycle()

f

cout«"motorcycle析构函数被调用"Wendi;

intmain()

motorcyclea;

a.bicycle::Run();

a.bicycle::Stop();

a.motorcar::Run();

a.motorcar::Stop();

试验汇报四多态性与运算符重载

一、试验目的

1掌握运算符重载的措施;学习使用虚函数实现动态多态性。

二、试验任务

1.定义Point类,有坐标_x,_y两个组员变量;对Point类重载“++”(自增)、“一”(自减)运

算符,实现对坐标的变化。(提醒:注意前++和后++)

2.定义一种车(vehicle)基类,有Run、Stop等虚组员函数,由此派生出自行车(bicycle)类、

汽车(motorcar)类,从bicycle到motorcar派生出摩托车(motorcycle)类,它们均有Run、St叩

等组员函数,运用基类指针或引用虚函数,观测虚函数的作用,说说多态的含义,假如不采

用虚函数会是什么样的成果?

三、试验成果(源程序+法释)

1源程序

#include<iostream>

usingnamespacestd;

classpoint

{

public:

point()

(

cout«"point构造函数被调用"wendl;

)

〜point。

(

cout«"point析构函数被调用"《endl;

!

voidset(inti=l,intj=3)

(

x=i;

y=j;

)

voidshow()

cout<〈"坐标是:"«endl;

cout«x«""«y«endl;

}

pointoperator++();

pointoperator-();

pointoperator++(inl);

pointoperator-(int);

private:

intx,y;

};

pointpoint::operator++()

(

x++;

y++;

return*this;

}

pointpoint::operator-()

y--;

return*this;

1

pointpoint::opcrator++(int)

(

++x;

++y;

return*this;

}

pointpoint::operator—(int)

(

-x;

一y;

return*this;

)

intniain()

(

intij;

pointa;

cout<〈"请输入坐标:"«en(Jl:

cin»i»j;

a.set(i,j);

a.show();

++a;

a.show();

-a;

a.sho\v();

a++;

a.show();

a-;

a.show();

c(*C:\Docu*entsandSettings\user\Debug\qq.exe_|口[X

point构造函数被调用

请输入里标:

24

坐标是:

point析构函数被调用

坐标是,

point;析构函数被调用

坐标是:

24

poigt析构函数被调用

坐标是:

point析构函数被调用

坐标是:

Soint:析构函数被调用

Pt*essanykeytocontinue

2源程序

#include<iostream>

usingnamespacestd;

classvehicle

(

public:

vchiclc()

(

cout«"vehicle构造函数被调用"vvendl;

1

-vehicle()

cout«"vehicle析构函数被调用"vvendl:

)

virtualvoidRun()

(

coutvv"设置vehicle最大速度和重量"<<endl;

)

virtualvoidStop()

(一

cout<<"显示vehicle最大速度和重量"<<endl;

)

private:

doubleMaxSpeed,Weight;

};

classb沁ycle:publicvehicle

(

public:

bicycle()

f

cout«"bicycle构造函数被调用"«endl;

}

~bicycle()

(

coinvv"bicycle析构函数被调用"vvendl;

)

voidRun()

(

cout<<"设置bicycle最大速度和重量"<<endl;

)

voidStop()

(

coutvv"显示bicycle最大速度和重量"<<cndl;

)

private:

doubleHeight;

classmotorcar:publicvehicle

public:

motorcar()

(

cout«"motorcar1构造函数被调用"<<endl;

)

~motorcar()

(

cout«”molorcar析构函数被调用"v<endl;

)

voidRun()

(

coui<<"设置motorcar最大速度和重量"<<endl;

)

voidStop()

(

cout<<"显示motorcar最大速度和重量"Wendi;

|

private:

intScatNum;

I;

classmotorcycle:publicmotorcar,publicbicycle

(

public:

motorcycleO

(

cout«"motorcycle构造函数被调用"<<endl;

)

-motorcycleO

(

cout«"motorcycle析构函数被调用"<<endl;

)

voidRun()

{

cout<<"设置motorcycle最大速度和重量"<<endl;

}

voidStop()

cout<<"显示motorcycle最大速度和重量"<<endl;

voidfun(vehicle*p)

p->Run();

p->Stop();

)

intmain()

(

vehicleaO,*p;

bicycleal;

motorcara2;

motorcyclea3;

cout«cndi;

p=&aO;

fun(p);

p=&al;

fun(p);

p=&a2;

fun(p);

//p=&a3;

//fun(p);

cout«endl;

|c',C:\DocuMervtsandSett±ngs\user\Debug\qq.exe*

温馨提示

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

评论

0/150

提交评论