资源描述
1.有两个长方柱,其长.宽.高分别是(1)12,20,25;(2)10,14,20;求它们的体积,编写一个基于对象的程序,在类中用带参数的构造函数对数据成员初始化。
#include <iostream>
using namespace std;
class Box
{public:
Box(int,int,int);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len)
{height=h;
width=w;
length=len;
}
int Box::volume()
{return(height*width*length);
}
int main()
{
Box box1(12,25,30);
cout<<"The volume of box1 is "<<box1.volume()<<endl;
Box box2(15,30,21);
cout<<"The volume of box2 is "<<box2.volume()<<endl;
return 0;
}
2.声明一个基类学生,在声明基类的公用派生类研究生,用指向基类对象的指针输出数据。
#include <iostream>
#include <string>
using namespace std;
class Student
{public:
Student(int ,string ,float);
void display();
private:
int num;
string name;
float score;
};
Student::Student(int n,string nam ,float s)
{num=n;
name=nam;
score=s;
}
void Student::display()
{cout<<endl<<"num:"<<num<<endl;
cout<<endl<<"name:"<<name<<endl;
cout<<endl<<"score:"<<score<<endl;
}
class Graduate: public Student
{public:
Graduate(int ,string ,float,float );
void display();
private:
float wage;
};
Graduate::Graduate(int n,string nam,float s,float w):Student(n,nam,s),wage(w){}
Void Graduate::display()
{ Student::display()
Cout<<”wage=”<<wage<<endl;
}
int main()
{Student stud1(1001,"gg",88.9);
Graduate gra1(2001,"ff",66.9,19999);
Student* pt=&stud1;
pt->display();
pt=&gra1;
pt->display();
}
(此文档部分内容来源于网络,如有侵权请告知删除,文档可自行编辑修改内容,供参考,感谢您的配合和支持)
编辑版word
展开阅读全文