| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1166 人关注过本帖
标题:新手求教,c++简单程序
只看楼主 加入收藏
diwang0001
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2004-10-4
收藏
 问题点数:0 回复次数:4 
新手求教,c++简单程序

1、声明一个虚基类person,数据成员有姓名、性别、年龄;纯虚函数show()

2、一个派生自person类的employee类,有单位、应发工资,实发工资等数据成员,编写show()函数;

3、另一个派生自person类的student类,有专业、年级、学号数据成员,编写show()函数;

4、派生自employee类和student类的表示在职攻读学位的in-service类,他同时具有employee类和student类的性质。但是他的实发工资就会因为读书而只发放应发工资的80%,编写show()函数。

Main函数实现以上各功能。

注意:1、使用基类和派生类的对象指针来体会虚函数,如果不声明show()为虚函数结果会如何?

2、在in-service类对象中访问基类person数据成员,试想如果不将person声明为虚基类结果会怎样?

二、函数重载

给出类:

class three_d

{

int x,y,z;

public:

three_d(int i,int j,int k)

{

x=i;

y=j;

z=k;

}

three_d()

{

x=0;

y=0;

z=0;

}

void get(int &i,int &j,int &k)

{

i=x;

j=k;

k=z;

}

};

针对给出的类,重载”+””-””++”运算符。

2004-11-29 10:25
corrupt
Rank: 2
等 级:新手上路
威 望:3
帖 子:535
专家分:0
注 册:2004-9-29
收藏
得分:0 

你的第二 题,可以到 置顶的 常用算法 中看看, 我写了一个 复数的 重载,

和你的 一样~


2004-11-29 13:11
corrupt
Rank: 2
等 级:新手上路
威 望:3
帖 子:535
专家分:0
注 册:2004-9-29
收藏
得分:0 

第一题, 至于 用对象 指针 体会, 那 要自己写了, 我要上课了~~

#include<iostream.h> #include<string.h>

class Cperson { protected: char Cname[8]; char Csex[5]; int Iage; public: Cperson(){}; Cperson(char *n,char *s,int a) { strcpy(Cname,n); strcpy(Csex,s); Iage=a; } virtual void show() { cout<<"姓名:"<<Cname<<endl; cout<<"性别:"<<Csex<<endl; cout<<"年龄:"<<Iage<<endl; } };

class Cemployee : public Cperson { protected: char Caddress[7]; double Fyingfawage; double Fshifawage; public: Cemployee(){}; Cemployee(char *n,char *s,int a,char *d,double ying,double shi):Cperson(n,s,a) { strcpy(Cname,n); strcpy(Csex,s); Iage=a; strcpy(Caddress,d); Fyingfawage=ying; Fshifawage=shi; } void show() { Cperson::show(); cout<<"单位:"<<Caddress<<endl; cout<<"应发工资:"<<Fyingfawage<<endl; cout<<"实发工资:"<<Fshifawage<<endl; } };

class Cstudent : public Cperson { protected: char Cspecialty[10]; char Cgrade[10]; int Cid; public: Cstudent(){}; Cstudent(char *p,char *g,int i) { strcpy(Cspecialty,p); strcpy(Cgrade,g); Cid=i; } Cstudent(char *n,char *s,int a,char *p,char *g,int i):Cperson(n,s,a) { strcpy(Cspecialty,p); strcpy(Cgrade,g); Cid=i; } void show1() { cout<<"专业:"<<Cspecialty<<endl; cout<<"年级:"<<Cgrade<<endl; cout<<"学号:"<<Cid<<endl; } void show() { Cperson::show(); cout<<"专业:"<<Cspecialty<<endl; cout<<"年级:"<<Cgrade<<endl; cout<<"学号:"<<Cid<<endl; } };

class Cin_service:public Cemployee,public Cstudent { public: Cin_service(char *n,char *s,int a,char *d,double ying,double shi,char *p,char *g,int i):Cemployee(n,s,a,d,ying,shi*0.8),Cstudent(p,g,i) {

} void show() { Cemployee::show(); Cstudent::show1(); } };

int main() { Cperson person("张三","男",20); person.show(); cout<<"*-------------------------------------------*"<<endl; Cemployee employee("李四","女",20,"会计部",1500,1500); employee.show(); cout<<"*-------------------------------------------*"<<endl; Cstudent student("王五","男",20,"计算机","大三",456); student.show(); cout<<"*-------------------------------------------*"<<endl; Cin_service in_service("小样","男",20,"教研室",2000,2000,"计算机","大四",456); in_service.show(); cout<<"*-------------------------------------------*"<<endl; return 0;

}


2004-11-29 14:22
diwang0001
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2004-10-4
收藏
得分:0 

谢谢


游离在代码和爱情之间的我, 忘记了代码的枯燥; 品味了爱情的甜蜜; 盛夏来的, 来陪伴我最最可爱的代码......
2004-11-29 21:27
corrupt
Rank: 2
等 级:新手上路
威 望:3
帖 子:535
专家分:0
注 册:2004-9-29
收藏
得分:0 

不用谢,~~~~~


2004-11-30 10:47
快速回复:新手求教,c++简单程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014687 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved