1、有两个长方柱,其长、宽、高分别为: (1)15,26,27;(2)11,15,30。求解并输出它们的体积。编一个基于对象的程序,在类中用带参数的构造函数。
#include <iostream>
using namespace std;
/*
有两个长方柱,其长、宽、高分别为:
(1)15,26,27;(2)11,15,30。求解并输出它们的体积。
编一个基于对象的程序,在类中用带参数的构造函数。
*/
class Rectangle{
public:
Rectangle(int l,int w, int h){
length = l;
width = w;
height = h;}
~Rectangle(){}
float volume(){
float v;
v=length*width*height;
return v;}
private:
int length;
int width;
int height;
};
int main()
{
Rectangle r1(15,26,27),r2(11,15,30);
cout<<"the volume of revtangle1 is"<<r1.volume()<<endl;
cout<<"the volume of revtangle2 is"<<r2.volume()<<endl;
return 0;
}
2、编写一个程序,声明抽象基类Shape,由它派生出圆形Circle和矩形Rectangle,用一个函数area()分别输出上述两个图形的面积,两个图形的数据在定义对象时给出。
#include <iostream>
using namespace std;
/*
3.编写一个程序,声明抽象基类Shape,
由它派生出圆形Circle和矩形Rectangle,
用一个函数area()分别输出上述两个图形的面积,
两个图形的数据在定义对象时给出。
*/
class Shape{
public:
virtual void area()const=0;//纯虚函数
virtual void shapename()const=0;//纯虚函数
};
class Circle:public Shape{
public:
Circle(float r=0){
radius = r;}
virtual void area()const{
cout<<3.14159*radius*radius;}
virtual void shapename()const{cout<<"Circle";}
protected:
float radius;
};
class Rectangle:public Shape{
public:
Rectangle(int l=0,int w=0){
length=l;
width=w;
}
virtual void area()const{
cout<<length*width;}
virtual void shapename()const{
cout<<"Rectangle";}
protected:
int length;
int width;
};
int main(){
Rectangle r(10,9);
Circle c(2);
Shape *p;
p=&c;
p->shapename();
p->area();
cout<<endl;
p=&r;
p->shapename();
p->area();
return 0;
}
3、分别定义教师类Teacher和干部类Cadre,
采用多重继承的方式由这两个类派生出新类Teacher_Cadre(教师兼干部类)。
要求:
(1)在两个基类中都包含姓名、年龄、性别、地址、电话数据成员。
(2)在Teacher类中还包含数据成员职称title,在Cadre类中还包含数据成员职务post,
在Teacher_Cadre类中还包含数据成员工资wage
(3)对两个基类中的姓名、年龄、性别、地址、电话数据成员用相同的名字,
在访问这类数据成员时,指定作用域。
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,
输出姓名、年龄、性别、地址、电话,然后再用cout语句输出职务和工资。。
#include <iostream>
using namespace std;
/*
分别定义教师类Teacher和干部类Cadre,
采用多重继承的方式由这两个类派生出新类Teacher_Cadre(教师兼干部类)。
要求:
(1)在两个基类中都包含姓名、年龄、性别、地址、电话数据成员。
(2)在Teacher类中还包含数据成员职称title,在Cadre类中还包含数据成员职务post,
在Teacher_Cadre类中还包含数据成员工资wage
(3)对两个基类中的姓名、年龄、性别、地址、电话数据成员用相同的名字,
在访问这类数据成员时,指定作用域。
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,
输出姓名、年龄、性别、地址、电话,然后再用cout语句输出职务和工资。。
*/
class Teacher{
public:
Teacher(string n,int a,string s,string add,int p,string t);
void display();
protected:
string name;
int age;
string sex;
string address;
int phone;
string title;
};
Teacher::Teacher(string n,int a,string s,string add,int p,string t){
name=n;
age=a;
sex=s;
address=add;
phone=p;
title=t;
}
void Teacher::display(){
cout<<"name:"<<name<<endl;
cout<<"age"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"title:"<<title<<endl;
cout<<"address:"<<address<<endl;
cout<<"phone:"<<phone<<endl;
}
class Cadre{
public:
Cadre(string n,int a,string s,string add,int p,string po);
void display();
protected:
string name;
int age;
string sex;
string address;
int phone;
string post;
};
Cadre::Cadre(string n,int a,string s,string add,int p,string po){
name=n;
age=a;
sex=s;
address=add;
phone=p;
post=po;
}
void Cadre::display(){
cout<<"name:"<<name<<endl;
cout<<"age"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"post:"<<post<<endl;
cout<<"address:"<<address<<endl;
cout<<"phone:"<<phone<<endl;
}
class Teacher_Cadre:public Teacher,public Cadre{
public:
Teacher_Cadre(string n,int a,string s,string add,int p,string t,string po,float w);
void show();
protected:
float wage;
};
Teacher_Cadre::Teacher_Cadre(string n,int a,string s,string add,int p,string t,string po,float w):
Teacher(n,a,s,add,p,t),Cadre(n,a,s,add,p,po),wage(w){}
void Teacher_Cadre::show(){
Teacher::display();
cout<<"post:"<<Cadre::post<<endl;
cout<<"wages:"<<wage<<endl;
}
int main(){
Teacher_Cadre tc("wang",25,"f","sichuan",314257,"president","prof.",5000);
tc.show();
return 0;
}
4、
1.重载运算符“+”,使之能用于两个复数相加,并输出相加结果。
2.依据上题,重载运算符“<<”用来输出复数。
3.依据上题,重载运算符“>>”用来输入复数。
#include <iostream>
using namespace std;
/*
1.重载运算符“+”,使之能用于两个复数相加,并输出相加结果。
2.依据上题,重载运算符“<<”用来输出复数。
3.依据上题,重载运算符“>>”用来输入复数。
*/
class Complex{
public:
Complex(){real=0;image=0;}
Complex(double r,double i):real(r),image(i){}
Complex operator+(Complex &c2);
friend ostream&operator<<(ostream&,Complex&);
friend istream&operator>>(istream&,Complex&);
protected:
double real;
double image;
};
Complex Complex::operator+(Complex &c2){
Complex c;
c.real=real+c2.real;
c.image=image+c2.image;
return c;
}
ostream&operator<<(ostream &output,Complex &c){
output<<"("<<c.real<<"+"<<c.image<<"i"<<")"<<endl;
return output;
}
istream&operator>>(istream &input,Complex &c){
cout<<"please input real part and imaginary part of complex number:";
input>>c.real>>c.image;
return input;
}
int main(){
Complex c1,c2;
cin>>c1>>c2;
cout<<"c1="<<c1<<endl;
cout<<"c2="<<c2<<endl;
Complex c;
c=c1+c2;
cout<<"c1+c2="<<c<<endl;
return 0;
}
5、声明一字符串类String,用来存放不定长的字符串,重载运算符==,<,>,用于比较两个字符串的等于大于小于的比较运算。
#include <iostream>
#include <string.h>
using namespace std;
/*
声明一字符串类String,用来存放不定长的字符串,重载运算符==,<,>,
用于比较两个字符串的等于大于小于的比较运算。
*/
class String{
public:
String(){p=NULL;}
String(char *str){
p=str;}
friend bool operator>(String &s1,String &s2);
friend bool operator<(String &s1,String &s2);
friend bool operator==(String &s1,String &s2);
void display();
protected:
char *p;
};
void String::display(){
cout<<p;}
bool operator>(String &s1,String &s2){
if (strcmp(s1.p,s2.p)>0)
return true;
else
return false;
}
bool operator<(String &s1,String &s2){
if (strcmp(s1.p,s2.p)<0)
return true;
else
return false;
}
bool operator==(String &s1,String &s2){
if (strcmp(s1.p,s2.p)==0)
return true;
else
return false;
}
int main(){
String s1("hello"),s2("book"),s3("computer");
cout<<(s1>s2)<<endl;
cout<<(s1<s3)<<endl;
cout<<(s1==s2)<<endl;
return 0;
}