类与对象练习题.docx

上传人:b****6 文档编号:7759006 上传时间:2023-01-26 格式:DOCX 页数:11 大小:16.64KB
下载 相关 举报
类与对象练习题.docx_第1页
第1页 / 共11页
类与对象练习题.docx_第2页
第2页 / 共11页
类与对象练习题.docx_第3页
第3页 / 共11页
类与对象练习题.docx_第4页
第4页 / 共11页
类与对象练习题.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

类与对象练习题.docx

《类与对象练习题.docx》由会员分享,可在线阅读,更多相关《类与对象练习题.docx(11页珍藏版)》请在冰豆网上搜索。

类与对象练习题.docx

类与对象练习题

本次实训的目的是掌握类,对象,构造函数,析构函数,成员函数,成员变量等概念,及

基本程序的实现。

实训题目:

1、书:

C++程序设计实训(第2版),第51页,构建集合类实训

2、书:

c++程序设计教程第2版,第193页,完成6、7、8、9、10题。

#include

#include

usingnamespacestd;

voidmain(){

ofstreamfile;

file.open("input.txt");

file<<"aaaaaaaa\nbbbbbbbb\ncccccccc";

file.close();

ifstreamfilei("input.txt");

ofstreamfileo;

fileo.open("output.txt");

charc;

filei>>noskipws;

inti=1;

fileo<

cout<

while(filei>>c){

if(c=='\n'){

i++;

fileo<<"\n";

cout<<"\n";

fileo<

cout<

}else{

fileo<

cout<

}

}

filei.close();

fileo.close();

}

#include

#include

classdog

{

public:

intweight;

intage;

dog()

{

}

dog(intweight1,intage1)

{

weight=weight1;

age=age1;

}

~dog()

{

}

};

intmain()

{

dogdog1(5,10);

dogdog2;

ofstreamfout;

ifstreamfint;

fout.open("out.txt");

fout<

fint.open("out.txt");

fint>>dog2.weight>>dog2.age;

cout<

fout.close();

//二进制

ofstreamfdata("file.dat",ios:

:

binary);

fdata.write((char*)(&dog1),sizeof(dog1));

ifstreamfint1("file.dat",ios:

:

binary);

fint1.read((char*)(&dog2),sizeof(dog2));

cout<

return0;

}

//dsStack.h:

interfaceforthedsStackclass.

//

//////////////////////////////////////////////////////////////////////

#if!

defined(AFX_DSSTACK_H__784FB507_8F89_496B_9619_22C97C0AA0D8__INCLUDED_)

#defineAFX_DSSTACK_H__784FB507_8F89_496B_9619_22C97C0AA0D8__INCLUDED_

#if_MSC_VER>1000

#pragmaonce

#endif//_MSC_VER>1000

#include

template

structnode

{

Tdata;

node*next;

};

template

classdsStack

{

private:

intsize;

node*floor;

node*top;

public:

dsStack();

virtual~dsStack();

Tpop();

voidpush(Tob);

intgetSize();

};

template

dsStack:

:

dsStack()

{

size=0;

top=newnode;

top->data=0;

top->next=0;

floor=top;

}

template

dsStack:

:

~dsStack()

{

size=0;

node*p;

p=top;

while(p)

{

top=top->next;

deletep;

p=top;

}

floor=0;

top=0;

}

template

TdsStack:

:

pop()

{

Ttmp;

tmp=top->data;

node*p;

p=top;

top=top->next;

size--;

deletep;

returntmp;

}

template

voiddsStack:

:

push(Tob)

{

node*p=newnode;

p->data=ob;

p->next=top;

top=p;

size++;

}

template

intdsStack:

:

getSize()

{

returnsize;

}

#endif//!

defined(AFX_DSSTACK_H__784FB507_8F89_496B_9619_22C97C0AA0D8__INCLUDED_)

#include"dsStack.h"

typedefdoubleT;

voidmain()

{

dsStackst;

Ti;

Ta;

for(i=1;i<10;i++)

{

st.push(i);

}

cout<

for(i=1;i<5;i++)

{

a=st.pop();

cout<

}

for(i=1;i<5;i++)

{

st.push(i+0.1);

}

for(i=1;i<10;i++)

{

a=st.pop();

cout<

}

}

#include

classString

{

char*str;//首地址

intlen;//长度值

public:

String();

String(String&str);

~String(){}

voidset(char*s);

voidshow();

intgetlen();

voiddelchar(charch);

String&operator=(String&);

String&operator+=(String&);

friendStringoperator+(String&,String&);

intoperator==(String&);

};

String:

:

String()//构造函数

{

len=0;

str=newchar[len+1];

*str='\0';

}

String:

:

String(String&s)

{

len=s.len;

str=newchar[len+1];

strcpy(str,s.str);

}

voidString:

:

set(char*s)

{

len=strlen(s);

str=newchar[len+1];

strcpy(str,s);

}

voidString:

:

show()//输出

{

cout<<"str="<

cout<<"len="<

}

intString:

:

getlen()

{

returnlen;

}

voidString:

:

delchar(charch){}

String&String:

:

operator=(String&s)//赋值

{

if(s.str)

{

str=newchar[strlen(s.str)+1];

strcpy(str,s.str);

}

elsestr=0;

returns;

}

String&String:

:

operator+=(String&s)//连接

{

strcat(str,s.str);

returns;

}

Stringoperator+(String&s1,String&s2)//友元函数连接

{

Stringt;

t.len=s1.len+s2.len;

t.str=newchar[t.len+1];

strcpy(t.str,s1.str);

strcat(t.str,s2.str);

returnt;

}

intString:

:

operator==(String&s)//比较2

{

returnstrcmp(str,s.str);

}

/*定义一个描述教师的类Teacher,数据成员包括工号num、姓名name、性别sex、家庭住址faddr、联系电话tel、E-mail地址、

职务headship、职称post和工资salary。

对于数据成员,要求用字符数组实现工号、姓名、家庭住址、联系电话、E-mail地址、

职务和职称,用字符型量实现性别,用整型量实现工资。

成员函数包括:

(1)设置工号

(2)设置姓名(3)设置性别(4)设置家庭住址

(5)设置联系电话(6)设置E-mail地址(7)设置职务(8)设置职称(9)设置工资(10)输出一个教师的全部描述信息。

在主函数中定义一个教师类对象,然后对所有成员函数进行测试。

*/

#include

#include"teacher.h"

voidmain()

{

teacherBetty;//定义一个教师对象

}

voidteacher:

:

display()

{

cout<<"工号num:

"<

cout<<"姓名name:

"<

cout<<"性别sex:

"<

cout<<"家庭住址faddr:

"<

cout<<"联系电话tel:

"<

cout<"E-mail地址:

"<

cout<<"职务headship:

"<

cout<<"职称post:

"<

cout<<"工资salary:

"<

}

#include

#definek100

classteacher//定义教师类,设置其成员

{

charnum[k];

charname[k];

charfaddr[k];//家庭住址

chartel[k];

charmail[k];

charheadship[k];//职务

charpost[k];//职称

charsex;

intsalary;//工资

public:

charsetTnum(chartnum[k])

{strcpy(num=tnum);}

charsetTname(chartname[k])

{strcpy(name=tname);}

charsetTfaddr(chartfaddr[k])//家庭住址

{strcpy(faddr=tfaddr);}

charsetTtel(charttel[k])

{strcpy(tel=ttel);}

charsetTmail(chartmail[k])

{strcpy(mail=tmail);}

charsetTheadship(chartheadship[k])//职务

{strcpy(headship=theadship);}

charsetTpost(chartpost[k])//职称

{strcpy(post=tpost);}

charsetTsex(chartsex[k])

{sex=tsex;}

intsetTsalary(inttsalary[k])//工资

{salary=tsalary;}

voiddisplay();

};

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1