实验一 类和对象
#include <iostream>
#include<iomanip>
using namespace std;
class Date
{private:
int year;
int month;
int day;
public:
void set_date(void);
void show_dates(void);
};
Date d;
int main()
{ d.set_date();
d.show_dates();
return 0;
}
void Date::set_date(void)
{ cin>>d.year;
cin>>d.month;
cin>>d.day;
}
void Date::show_dates(void)
{ cout<<setw(4)<<right<<setfill('0')<<d.year<<"-"<<setw(2)<<right<<setfill('0')<<d.month<<"-"<<setw(2)<<right<<setfill('0')<<d.day<<endl; }
——————————————————————————————————————————————————————————————————————
第2关:类内函数
平台会对你编写的代码进行测试:
测试输入:04 12 23
预期输出:0004-12-23
测试输入:6 -1,151
预期输出:输入有错,请重新输入!
#include <iostream>
#include <iomanip>
using namespace std;
class Date
{ private:
int year;
int month;
int day;
public:
void set_date()
{ cin>>year;
cin>>month;
cin>>day;}
void show_dates()
{if ((year<1)||((month<1)||(month>12))||((day<1)||(day>31)))
cout<<"输入有错,请重新输入!"<<endl;
else if(((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))&&(day>31))
cout<<"输入有错,请重新输入!"<<endl;
else if(((month==4)||(month==6)||(month==9)||(month==11))&&(day>30))
cout<<"输入有错,请重新输入!"<<endl;
else if((month==2)&&(day>28))
cout<<"输入有错,请重新输入!"<<endl;
else
cout<<setw(4)<<right<<setfill('0')<<year<<"-"<<setw(2)<<right<<setfill('0')<<month<<"-"<<setw(2)<<right<<setfill('0')<<day<<endl;}
};
Date d;
int main()
{ d.set_date();
d.show_dates();
return 0;
}
——————————————————————————————————————————————————————————————
第3关:接口分离
本关任务:编写一个能将多个程序文件衔接运行的程序,具体程序文件包括:类定义的头文件teacher.h、成员函数定义的源文件teacher.cpp、
包括主函数的源文件main.cpp。在类中增加一个对数据成员赋初值的成员函数set_info。上机调试并运行通过。
//teacher.h
#include<string>
using namespace std;
class Teacher
{public:
void show( );
void set_info();
private:
int id;
string name;
string course;
char location;
};
++++++++++++++++++同一题
//teacher.cpp
#include <iostream>
#include<iomanip>
#include "teacher.h"
void Teacher::set_info()
{
cin>>id;
cin>>name;
cin>>course;
cin>>location;
}
void Teacher::show()
{
if(
(id>5000)||(id<0)||(location<'a')||(location>'z')
)
{
cout<<"输入有错,请重新输入!"<<endl;
}
else
{
cout<<"id:"<<setw(5)<<right<<setfill('0')<<id<<", name:"<<name<<", course:"<<course<<", location:"<<location<<endl;
}
}
——————————————————————————————————————————————————————————————
第4关:成员函数
需要求3个椭球(重心位于迪卡尔坐标系原点)的体积
(V=4/3*pi*x*y*z,pi取3.14,结果精确到两位小数)。
浮点数精度:cout<<fixed<<setprecision(2)。
请编一个基于对象的程序。数据成员包括x、y、z三轴上的半径。要求用成员函数实现以下功能:
① 由键盘分别输入3个椭球的x、y、z轴半径;
② 计算椭球的体积;
③ 输出3个椭球的体积。
输入不可为负,否则输出输入有错,请重新输入!
请编程序,上机调试并运行。
//ellipsoid.h
//请在这里补充代码,定义Ellipsoid类的头文件,包含Ellipsoid类所需的数据成员和成员函数声明。
//其中成员函数的定义,应放在另一个源文件ellipsoid.cpp中实现。
/********** Begin *********/
#include<string>
using namespace std;
class Ellipsoid{
public:
void volume();
void get_value();
void display();
private:
int x;
int y;
int z;
float v;
};
/********** End **********/
++++++++++++++++++++同一题
//ellipsoid.cpp
//请在这里补充代码,定义Ellipsoid类体外的成员函数
/********** Begin *********/
#include<iostream>
#include"ellipsoid.h"
#include<iomanip>
void Ellipsoid::get_value()
{
cin>>x;
cin>>y;
cin>>z;
}
void Ellipsoid::volume()
{
v=(4/3)*3.14*x*y*z;
}
void Ellipsoid::display(){
if((x<=0)||(y<=0)||(z<=0))
cout<<"输入有错,请重新输入!"<<endl;
else
cout<<fixed<<setprecision(2)<<v<<endl;
}
/********** End **********/
————————————————————————————————————————————————————————
————————————————————————————————————————————————————————
第二章 重载
第1关:"钱包"加减乘除
本关任务:编写一个能计算两个钱包类加减乘除的程序。
//定义一个Wallet类,包含Wallet类的数据成员人名币rmb, 日元jpy,美元usd,输入成员函数input,输出成员函数display,以及四个成员函数分别求两个钱包之和、差、积和商。
//每种货币之间独立进行四则运算。
#include<iostream>
using namespace std;
class Wallet
{
public:
Wallet(){rmb=0;jpy=0;usd=0;}
Wallet(double r,double j,double u){rmb=r;jpy=j;usd=u;}
Wallet operator+(Wallet &c2);
Wallet operator-(Wallet &c2);
Wallet operator*(Wallet &c2);
Wallet operator/(Wallet &c2);
void display();
void input();
private:
double rmb;
double jpy;
double usd;
};
Wallet Wallet::operator+(Wallet &c2)
{
Wallet c;
c.rmb=rmb+c2.rmb;
c.jpy=jpy+c2.jpy;
c.usd=usd+c2.usd;
return c;
}
Wallet Wallet::operator-(Wallet &c2)
{
Wallet c;
c.rmb=rmb-c2.rmb;
c.jpy=jpy-c2.jpy;
c.usd=usd-c2.usd;
return c;
}
Wallet Wallet::operator*(Wallet &c2)
{
Wallet c;
c.rmb=rmb*c2.rmb;
c.jpy=jpy*c2.jpy;
c.usd=usd*c2.usd;
return c;
}
Wallet Wallet::operator/(Wallet &c2)
{
Wallet c;
c.rmb=rmb/c2.rmb;
c.jpy=jpy/c2.jpy;
c.usd=usd/c2.usd;
return c;
}
void Wallet::input()
{
cin>>rmb>>jpy>>usd;
}
void Wallet::display()
{
cout<<"("<<rmb<<","<<jpy<<","<<usd<<")"<<endl;
}
——————————————————————————————————————————————
第2关:"钱包"与浮点数(人民币)之差(交换律)
本关任务:编写一个能计算钱包与浮点数(指钱包中的人民币余额)之差(交换律)的程序。
//定义一个钱包类Wallet,有三个浮点数成员变量,rmb,jpy,usd分别代表人民币,日元和美元。重载运算符“-”,使之能用于浮点数(人民币)的减法运算。
//参加运算的两个运算量可以都是类对象,也可以其中有一个是浮点数,顺序任意。定义一个钱包类,重载运算符“-”,使之能用于浮点数做减法。参加运算的两个运算量可以都是类对象,也可以其中有一个是代表人民币的浮点数,顺序任意。例如:w1-w2, 2.2-w1, w1-5均合法。
//例如:w1 - w2,2 - w1 , w1 - 1.2均合法。
#include<iostream>
using namespace std;
class Wallet
{
public:
Wallet(){rmb=0;jpy=0;usd=0;}
Wallet(float r,float j,float u){rmb=r;jpy=j;usd=u;}
friend Wallet operator-(Wallet &w1,Wallet &w2);
friend Wallet operator-(float &rmb,Wallet &w1);
friend Wallet operator-(Wallet &w1,float &rmb);
void display();
void input();
private:
float rmb;
float jpy;
float usd;
};
Wallet operator-(Wallet &w1,Wallet &w2)
{
return Wallet(w1.rmb-w2.rmb,w1.jpy-w2.jpy,w1.usd-w2.usd);
}
Wallet operator-(float &rmb,Wallet &w1)
{
return Wallet(rmb-w1.rmb,w1.jpy,w1.usd);
}
Wallet operator-(Wallet &w1,float &rmb)
{
return Wallet(w1.rmb-rmb,w1.jpy,w1.usd);
}
void Wallet::input()
{
cin>>rmb>>jpy>>usd;
}
void Wallet::display()
{
cout<<"("<<rmb<<","<<jpy<<","<<usd<<")"<<endl;
}
——————————————————————————————————————————
第3关:重载矩阵乘法
本关任务:编写一个能重载矩阵乘法运算符的程序。
但若要把矩阵与矩阵相乘,我们要计算行与列的"点积","点积" 是把 对称的元素相乘,然后把结果加起来:
(1, 2, 3) • (7, 9, 11) = 1×7 + 2×9 + 3×11 = 58
#include<iostream>
using namespace std;
class Matrix
{public:
void input();
void display();
friend Matrix operator*(Matrix &a,Matrix &b);
private:
float mat[3][3];
};
void Matrix::input()
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cin >> mat[i][j];
}
void Matrix::display()
{
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
}
Matrix operator*(Matrix &a,Matrix &b)
{Matrix c;
int i,j;
for ( i = 0; i < 3; i++)
for (j = 0; j < 3; j++){
float sum=0;
for(int k=0;k<3;k++)
sum=sum+a.mat[i][k]*b.mat[k][j];
c.mat[i][j]=sum;}
return c;}
——————————————————————————————————————————————————
——————————————————————————————————————————————————
实验三 继承与派生
第1关:按要求补充程序1
本关任务:定义一个学生类基类Teacher,在此基础上通过单继承方式定义一个公有派生类Teacher1。在main函数中定义Teacher1的对象,通过调用成员函数给派生类对象赋值,然后输出派生类各成员的值。
Teacher类和Teacher1类已经给出,按要求补充其余代码。
#include<iostream>
using namespace std;
class Matrix
{public:
void input();
void display();
friend Matrix operator*(Matrix &a,Matrix &b);
private:
float mat[3][3];
};
void Matrix::input()
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cin >> mat[i][j];
}
void Matrix::display()
{
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
}
Matrix operator*(Matrix &a,Matrix &b)
{Matrix c;
int i,j;
for ( i = 0; i < 3; i++)
for (j = 0; j < 3; j++){
float sum=0;
for(int k=0;k<3;k++)
sum=sum+a.mat[i][k]*b.mat[k][j];
c.mat[i][j]=sum;}
return c;}
————————————————————————————————————————————
第2关:按要求补充程序2
本关任务:在上一关的基础上,将派生类的继承方式改为protected,并且增加派生类的构造函数。main函数中的派生类对象定义时给定实参初始化。
Teacher类和Teacher1类已经给出,按要求补充其余代码。
/*Teacher.cpp,本文件存放Teacher类中成员函数的类外定义*/
#include<string>
#include<iostream>
#include"Teacher.h"
using namespace std;
Teacher::Teacher(int i,string n,char d, char s)
{
id=i;
name=n;
department=d;
sex=s;
}
void Teacher::show()
{
cout<<" id: "<<id<<endl;
cout<<" name: "<<name<<endl;
cout<<" department: "<<department<<endl;
cout<<" sex: "<<sex<<endl;
}
++++++++++++
/*Teacher.h*/
#pragma once
#include <string>
#include <iostream>
using namespace std;
// 声明基类
class Teacher
{public:
Teacher(int i,string n,char d, char s);
//void get_value( );
void show();
protected:
int id;
string name;
char department; //a-z字符
char sex;
};
++++++++++++++
/*Teacher1.cpp,本文件存放Teacher1类中的成员函数的类外定义,别忘了加头文件*/
#include<string>
#include<iostream>
#include"Teacher1.h"
using namespace std;
Teacher1::Teacher1(int i,string n,char d, char s,int a,string c,string o):Teacher(i,n,d,s)
{
age=a;
course=c;
office=o;
}
void Teacher1::show_1()
{
Teacher::show();
cout<<" age: "<<age<<endl;
cout<<" course: "<<course<<endl;
cout<<" office: "<<office<<endl;
}
++++++++++++++
/*Teacher1.h*/
#pragma once
#include "Teacher.h"
class Teacher1: protected Teacher
{public:
Teacher1(int i,string n,char d, char s,int a,string c,string o);
/*构造函数中提供了五个默认参数,分别给id、name、department、sex、age、
course、office成员赋值*/
//void get_value();
void show_1();
private:
int age;
string course;
string office;
};
——————————————————————————————————————————————
第3关:按要求写程序
本关任务:声明Student(学生)类和AssociationMember(社团成员)类,采用多重继承方式由这两个类派生出新类Student_AssociationMember(学生社团成员)。要求:
① 在两个基类中都包含id(身份编号)、name(姓名)、sex(性别)这些数据成员。
② 在Student类中还包含数据成员association_name(社团名称),在Association类中还包含数据成员post(职务)。在Student_AssociationMember类中还包含数据成员activities_num(参加活动次数)。
③ 对两个基类中的id、name、sex等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
④ 在类体中声明成员函数,在类外定义成员函数。
⑤ 在派生类Student_AssociationMember的成员函数display中调用Student类中的show函数,输出id、name、sex、association_name然后再用cout语句输出post与activities_num。
/*Association.cpp*/
#include <iostream>
#include<string>
#include "Association.h"
using namespace std;
Association ::Association(int i, string n, char s, string p)
{id=i;name=n;sex=s;post=p;}
void Association :: show()
{
cout<<"num:"<<id<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"post:"<<post<<endl;
}
+++++++++++++++++++++++++
/*Association.h*/
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Association
{
public:
Association(int i, string n, char s, string p);
void show();
protected:
int id;
string name;
char sex;
string post;
};
++++++++++++++++++++++++++++
/*Student.cpp*/
#include <iostream>
#include "Student.h"
using namespace std;
Student ::Student(int i, string n, char s, string a)
{id=i;name=n;sex=s;association_name=a;}
void Student ::show()
{
cout<<"id:"<<id<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"association_name:"<<association_name<<endl;
}
+++++++++++++++++++++++
/*Student.h*/
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(int i, string n, char s, string a);
void show();
private:
int id;
string name;
char sex;
string association_name;
};
++++++++++++++++++++++++++
/*sa.cpp,存放student_association类中成员函数的类外定义*/
#include<iostream>
#include<string>
#include "sa.h"
Student_Association::Student_Association(int i, string n, char s, string a,string p,int w):Student(i,n,s,a),Association(i,n,s,p)
{activities_num = w;}
void Student_Association :: display()
{
Student::show();
cout<<"post:"<<post<<endl;
cout<<"activities_num:"<<activities_num<<endl;
}
++++++++++++++++++++++++
/*sa.h,存放student_association类的定义*/
#pragma once
#include<iostream>
#include<string>
#include "Student.h"
#include "Association.h"
class Student_Association : public Student , public Association
{
public:
Student_Association(int i, string n, char s, string a,string p,int w);
void display();
private:
int activities_num;
};
1.1
//Box.h
#include <iostream>
using namespace std;
class Box
{public:
void get_value();
void volume();
void display();
private:
float length;
float width;
float height;
float vol;
};
//-------------------------------------------------
//Box.cpp
#include <iostream>
#include "Box.h"
using namespace std;
void Box::get_value()
{ cin>>length;
cin>>width;
cin>>height;
}
void Box::volume()
{ vol=length*width*height;}
void Box::display()
{ if(length<=0 || width<=0 || height<=0)
{ cout<<"输入有误,请重新输入"<<endl; }
else
cout<<vol<<endl;
}
//-------------------------------------------------
//main.cpp
#include "Box.h"
using namespace std;
int main()
{Box box1,box2,box3;
box1.get_value();
box2.get_value();
box3.get_value();
box1.volume();
box2.volume();
box3.volume();
box1.display();
box2.display();
box3.display();
return 0;
}
2.1
/定义一个Complex类,包含Complex类的数据成员实部real和虚部imag,输入成员函数input,输出成员函数display,以及四个成员函数分别求两个复数之和、差、积和商。
//和=(a+bi)+(c+di)=(a+c)+(b+d)i
//差=(a+bi)-(c+di)=(a-c)+(b-d)i
//积=(a+bi)*(c+di)=(ac-bd)+(bc+ad)i
//商=(a+bi)/(c+di)=(ac+bd)/(cc+dd)+((bc-ad)/(cc+dd))i
#include<iostream>
using namespace std;
class Complex
{
public:
void input();
void display();
friend Complex operator+(Complex &c1,Complex &c2);
Complex operator-(const Complex &c2);
Complex operator*(const Complex &c2);
Complex operator/(const Complex &c2);
private:
double real;
double imag;
};
Complex operator+(Complex &c1,Complex &c2)
{
Complex c;
c.real=c1.real+c2.real;
c.imag=c1.imag+c2.imag;
return c;
}
Complex Complex::operator-(const Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
Complex Complex::operator*(const Complex &c2)
{
Complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
return c;
}
Complex Complex::operator/(const Complex &c2)
{
Complex c;
c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
return c;
}
void Complex::input()
{
cin>>real>>imag;
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
2.2
//定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。
//参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。
//例如:c1 + c2,i + c1 , c1 + i均合法(设i为整数,c1,c2 为复数)。
#include <iostream>
using namespace std;
class Complex {
public:
Complex() {
real = 0;
imag = 0;
}
Complex(double r,double i) {
real = r;
imag = i;
}
Complex operator+(Complex &c2);
Complex operator+(double c2);
friend Complex operator+(double c1,Complex&c2);
void display();
void input();
private:
double real;
double imag;
};
void Complex::input() {
cin >> real >> imag;
}
void Complex::display() {
cout << "("<< real << "," << imag << "i)" << endl;
}
Complex Complex::operator+(Complex &c2) {
return Complex(real+c2.real,imag+c2.imag);
}
Complex Complex::operator+(double c2) {
Complex c;
c.real = real + c2;
c.imag =imag ;
return c;
}
Complex operator+(double c1,Complex &c2) {
return Complex(c2.real + c1,c2.imag );
}
2.3
//定义一个Matrix类,包含一个数据成员即二维数组mat作为2行3列的矩阵,成员函数包括输入函数input、输出函数display、运算符重载函数operator+
//要求实现两个2行3列的矩阵之和,求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如:c = a + b。
#include<iostream>
using namespace std;
class Matrix
{
public:
Matrix();
friend Matrix operator+(Matrix&,Matrix&);
void input();
void display();
private:
double mat[2][3];
};
Matrix::Matrix()
{
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
mat[i][j]=0;
}
Matrix operator+(Matrix &a,Matrix &b)
{
Matrix c;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
return c;
}
void Matrix::input()
{
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
cin>>mat[i][j];
}
void Matrix::display()
{
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
cout<<mat[i][j]<<" ";
cout<<endl;
}
}
3.1
/*Student.cpp*/
/*请补充Student类中成员的类外定义。get_value()函数的作用是给基类对象赋值,display()函数的作用是输出基类对象的成员值,输出格式请参照实验要求。*/
#include<iostream>
#include"Student.h"
#include<string>
void Student::get_value_1() {
cin >> num >> name >> sex;
}
void Student::display() {
cout<<" num: " << num << endl;
cout<<" name: " << name << endl;
cout<<" sex: " << sex << endl;
}
//student1.cpp
#include<iostream>
#include "Student1.h"
using namespace std;
Student1 st;
void Student1::get_value() {
st.get_value_1();
cin >> age >> addr ;
}
void Student1::display_1() {
st.display();
cout << " age: " << age <<endl;
cout << " address: " << addr <<endl;
}
3.2
/*Student.h*/
#pragma once
#include <string>
#include <iostream>
using namespace std;
// 声明基类
class Student
{public:
Student(int n,string na,char s);
//void get_value( );
void display( );
private :
int num;
string name;
char sex;
};
/*Student.cpp,本文件存放Student类中成员函数的类外定义*/
#include <iostream>
#include<string>
#include<iomanip>
#include "Student.h"
using namespace std;
Student::Student(int n,string na ,char s)
{
num=n;
name=na;
sex=s;
}
void Student:: display( )
{
cout<<" num: "<<num<<endl;
cout<<" name: "<<name<<endl;
cout<<" sex: "<<sex<<endl;
}
/*Student1.h*/
#pragma once
#include "Student.h"
class Student1: protected Student
{public:
Student1(int n=1011,string na="gaoping",char s='m',int a=19,string ad="");
/*构造函数中提供了五个默认参数,分别给num、name、sex、age、
address成员赋值*/
//void get_value();
void display_1( );
private:
int age;
string addr;
};
*Student1.cpp,本文件存放Student1类中的成员函数的类外定义,别忘了加头文件*/
#include <iostream>
#include<string>
#include<iomanip>
#include "Student1.h"
using namespace std;
Student1 sa;
Student1::Student1(int n,string na,char s,int a,string ad):Student(n,na,s)
{
age=a;
addr=ad;
}
void Student1:: display_1( )
{
sa.display();
cout << " age: " << age <<endl;
cout << " address: " << addr <<endl;
}
3.3
/*cadre.h*/
#include <string>
#include <iostream>
using namespace std;
class Cadre{
public:
Cadre(int num1,string name1,char sex1,string post1){
num=num1;
name=name1;
sex=sex1;
post=post1;
}
void display2();
private:
int num;
string name;
char sex;
string post;
};
/*cadre.cpp*/
#include <iostream>
using namespace std;
#include "cadre.h"
void Cadre::display2(){
cout<<"post:"<<post<<endl;
};
/*tc.cpp,存放teacher_cadre类中成员函数的类外定义*/
#include <string>
#include <iostream>
using namespace std;
#include "tc.h"
void teacher_cadre::display(){
display1();
display2();
cout<<"wages:"<<wages<<endl;
}
/*tc.h,存放teacher_cadre类的定义*/
#include <string>
#include <iostream>
using namespace std;
#include "teacher.h"
#include "cadre.h"
class teacher_cadre: public Teacher,public Cadre{
public:
void display();
teacher_cadre(int num1,string name1,char sex1,string title1,string post1,int wages1):Teacher(num1,name1,sex1,title1),Cadre(num1,name1,sex1,post1),num(num1),name(name1),sex(sex1),wages(wages1){};
private:
int num;
string name;
char sex;
int wages;
};
/*teacher.h*/
#pragma once
#include <string>
#include <iostream>
using namespace std;
class Teacher{
public:
Teacher(int num1,string name1,char sex1,string title1){
num=num1;
name=name1;
sex=sex1;
title=title1;
}
void display1();
private:
int num;
string name;
char sex;
string title;
};
/*teacher.cpp*/
#include <iostream>
#include <string>
#include <iostream>
using namespace std;
#include "teacher.h"
void Teacher::display1() {
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"title:"<<title<<endl;
}
题一:
#include <iostream>
using namespace std;
class A{
public:
A(){a=0 ;b=0;}
A(int i){a=i;b=0;}
A(int i,int j){a=i;b=j;}
void display(){cout<<"a="<<a<<" b="<<b;}
void display2(){cout<<"a="<<9-a<<" b="<<b*2+1;}
private:
int a;
int b;
};
class B:public A
{
public:
B():A(){c=0;}
B(int i):A(i){c=10;}
B(int i,int j):A(i,j){c=20;}
B(int i,int j,int k):A(i,j){c=k/5;}
void display1(){
display();
cout<<" c="<<c<<endl;
}
void display3(){
display2();
cout<<" c="<<c<<endl;
}
private:
int c;
};
int main(){
int x,y,z;
cin>>x>>y>>z;
B b1;
B b2(x);
B b3(x,y);
B b4(x,y,z);
b1.display1();
b2.display1();
b3.display3();
b4.display1();
return 0;
}
题二:
#include <iostream>
using namespace std;
class Time
{
public:
void set_time();
void show_time();
friend void fun(Time &t);
Time()
{ hour=0;
minute=0;
sec=0;
}
private:
int hour;
int minute;
int sec;
};
int main()
{
int set_time();
int show_time();
Time t;
t.set_time();
fun(t);
t.show_time();
return 0;
}
void fun(Time &t)
{ t.hour=18; }
int set_time()
{ cin>>hour>>minute>>sec; }
int show_time()
{ printf("%02d:%02d:%02d",t.hour,t.minute,t.sec);
}
题三:
#include <iostream>
using namespace std;
class Shape
{public:
virtual string shapeName(){}
virtual double area(){}
};
class Rectangle:public Shape
{
public:
Rectangle(){width = 0;height = 0;}
Rectangle(double w,double h)
{
width = w;
height = h;
}
string shapeName()
{
string r="Rectangle";
return r;
}
double area()
{
return width*height;
}
private:
double width;
double height;
};
void printArea(Shape& s) //以基类引用作为函数形参
{cout<<"area of "<<s.shapeName()<<" = "<<s.area()<<endl;}
int main()
{ double w=0.0,h=0.0;
cin >> w >> h;
Rectangle rect(w,h);
printArea(rect);
return 0;
}