C++面向对象编程实验精选:题目+自调代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验1 Visual C++6.0开发环境使用入门及C++程序设计基础练习1.通过程序求以下表达式的值并输出到屏幕上
①2004/3
②2004%3
③20/7
④a=18, a*3
假设a1=1,a2=2,a3=3,求各逻辑表达式的值
①a1||a2+a3&&a3-a1
②a1+a2>a3&&a1==a2
③3<8&&8>18
④!(8>3)
实验一代码:
#include<iostream.h>
void main()
{
int a=18,m,n;float b;
m=2004/3;
n=2004%3;
b=20/7;
a=a*3;
cout<<"2004/3="<<m<<'\n';
cout<<"2004%3="<<n<<'\n';
cout<<"20/7="<<b<<'\n';
cout<<"a*3="<<a<<'\n';
}
练习2.结构体变量的应用
定义一个结构体student,成员包括id, name,
sex, age, department, score,在主程序中声明
两个student类型的变量并赋初值,即给出两
个学生的信息,之后在屏幕上输出以下内容:
❖这两个学生的信息
❖两个人的平均成绩
❖第一个学生信息数据所占的内存空间大小
#include <iostream.h>
void main()
{
struct student
{ int id;
char name;
char sex;
int age;
float score;
}stu1;
stu1.id=10000;
='jack';
stu1.sex='Mail';
stu1.age=20;
stu1.score=59.4;
cout<<"id="<<stu1.id<<'\n'
<<"name="<<<<'\n'
<<"sex="<<stu1.sex <<'\n'
<<"age="<<stu1.age<<'\n'
<<"score="<<stu1.score<<'\n';
}
练习3. 用循环语句编程打印如下矩阵或图案
#include<iostream.h>
int main(void)
{int i,j,k;
for(i=1;i<7;i++)
{cout<<i;
for(k=0,j=i-1;k<7;k++,j++)
{if(j==7){j=0;}
cout<<j;
}
cout<<endl;
}
return 0;
}
练习4. 函数练习
从键盘输入三个实数a、b、c分别作为一个一元二次方程ax2+bx+c=0的三个系数。
使用系统给出的平方根函数(sqrt),编写一段程序,使之求出这个方程的两个根。
其中,求△=b2-4*a*c的功能要以函数形式出现。
(提示:求根公式,△<0时方程无解)
#include<iostream.h>
#include<math.h>
void main()
{
double a,b,c,n,x1,x2;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<"c=";
cin>>c;
n=b*b-4*a*c;
if(n>=0)
{
x1=(-b-sqrt(n))/2;
x2=(-b+sqrt(n))/2;
cout<<"x1="<<x1;
cout<<"x2="<<x2;
}
else cout<<"无解"<<endl;
}
练习5.
编制实现输入一个整数,判断能否被3,5,7整除,并输出以下信息之一:(1)能同时被3,5,7整除
(2)能被其中两数(要指出哪两个)整除
(3)能被其中一个数(要指出哪一个)整除
(4)不能被3,5,7整除
#include<iostream.h>
void main()
{
int i;
cout<<"整数i=";
cin>>i;
if(i%3==0&&i%5==0&&i%7==0)
cout<<"可以被3、5、7整除";
else if(i%3==0&&i%5==0)
cout<<"可以被3、5整除";
else if(i%3==0&&i%7==0)
cout<<"可以被3、7整除";
else if(i%7==0&&i%5==0)
cout<<"可以被7、5整除";
else if(i%3==0)
cout<<"可以被3整除";
else if(i%5==0)
cout<<"可以被5整除";
else if(i%7==0)
cout<<"可以被7整除";
}
练习6. 用循环语句编程打印如下矩阵或图案
#include<iostream.h>
int main()
{
int i,j,k;
for(i=5;i>=0;i-=1)
cout<<" ";
{
for(j=1;j>10;j+=2)
cout<<"#";
{
for(k=5;k>=0;k-=1)
cout<<" "<<'\n';
}
}
return 0;
}
练习7. 函数嵌套、递归
采用递归调用求n 阶勒让德多项式的值,递归公式如下:
#include<iostream.h>
long fac(int,float);
int main()
{
int n;
float x;
long y;
cout<<"n=";
cin>>n;
cout<<"x=";
⎪⎩⎪⎨⎧>---===--1/))()1()()12((1
01)(21n n x P n x xP n n x n x P N n n
cin>>x;
y=fac(n,x);
cout<<"Pn(x)="<<y;
return 0;
}
long fac(int n,float x)
{
long f;
if(n<0)
{
cout<<"输入有误";
f=-1;
}
else if(n==0)f=1;
else if(n==1)f=x;
else f=((2*n-1)*x-fac(n-1)-(n-1)*fac(n-2))/n;
return f;
}
练习8. 函数嵌套、递归 编写程序实现
并输入数据进行验证
#include<iostream.h>
long int f(int n)
{
if (n==0||n==1) return (1); //A
else return n * f(n-1); //B
}
void main(void)
{
cout<< "10!= " << f(10) << '\n';
}
实验二 C++程序设计之函数篇
练习4.函数重载
使用函数重载的方法定义两个重名函数,分别求出整
型数的两点间距离和浮点型数的两点间距离。
然后自
行输入数据进行测试
#include<iostream.h>
#include<math.h>
long dis(int,int);
∑=101!
n n
float dis(float,float);
void main()
{
int x1,x2,y1,y2,s1,s2;
float n1,n2,m1,m2,p1,p2;
char s;
cout<<"请选择输入种类:整型i;浮点型任意"<<'\n';
cin>>s;
if(s=='i')
{
cout<<"x1="<<endl;
cin>>x1;
cout<<"y1="<<endl;
cin>>y1;
cout<<"x2="<<endl;
cin>>x2;
cout<<"y2="<<endl;
cin>>y2;
s1=x1-x2;
s2=y1-y2;
dis(s1,s2);
cout<<"两点的距离为"<<dis(s1,s2)<<endl;
}
else
cout<<"n1="<<endl;
cin>>n1;
cout<<"m1="<<endl;
cin>>m1;
cout<<"n2="<<endl;
cin>>n2;
cout<<"m2="<<endl;
cin>>m2;
p1=n1-n2;
p2=m1-m2;
dis(p1,p2);
cout<<"两点的距离为"<<dis(p1,p2)<<endl;
}
long dis(int s1,int s2)
{
int l;
l=sqrt(s1*s1+s2*s2);
return l;
}
float dis(float p1,float p2)
{
float l;
l=sqrt(p1*p1+p2*p2);
return l;
}
练习5. c++多文件结构
设计多文件工程用于求圆面积和矩形面积
要求:文件结构如下图,自定义函数声明全部
放到头文件中
1.myArea'h'
double circle( double radius ) ;
double rect( double width, double length ) ;
2. myCircle'cpp'
const double pi = 3.14 ;
double circle ( double radius )
{ return pi * radius * radius ; }
3. myMain'cpp'
#include<iostream.h>
#include "myArea.h"
void main()
{ double width, length ;
cout << "Please enter two numbers:\n" ;
cin >> width >> length ;
cout << "Area of recttangle is: " << rect( width, length ) << endl ;
double radius ;
cout << "Please enter a radius:\n" ;
cin >> radius ;
cout << "Area of circle is: " << circle( radius ) << endl ;
}
4. myRect'cpp'
double rect ( double with, double length )
{ return with * length ; }
实验三C++程序设计之类与对象篇
练习1.
1.创建一个Student类,该类中具有学生姓名、学号、性别、年龄、计算机成绩和英
语成绩等数据成员,并要求将数据成员定义为保护类型,通过定义带参构造函数初始化每个成员。
2.在该类中定义成员函数实现所有信息的输出,
3.在该类中定义成员函数实现学生平均成绩的计算,
4.创建该类的两个学生对象,并将他们的信息以及平均成绩进行输出
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
student(string pname,int pID,int page,int psex,float pComputerSciencescore,float pEnglishscore)
{
cout<<pname<<'\n';
name=pname;
cout<<pID<<'\n';
ID=pID;
cout<<page<<'\n';
age=page;
cout<<psex<<'\n';
sex=psex;
cout<<pComputerSciencescore<<'\n';
ComputerSciencescore=pComputerSciencescore;
cout<<pEnglishscore<<'\n';
Englishscore=pEnglishscore;
}
protected:
string name;
int ID;
int age;
char sex;
float ComputerSciencescore;
float Englishscore;
float average;
public:
void printinformation()
{
cout<<"name="<<name<<'\n';
cout<<"ID="<<ID<<'\n';
cout<<"age="<<age<<'\n';
cout<<"sex="<<sex<<'\n';
cout<<"ComputerScience score="<<ComputerSciencescore<<'\n';
cout<<"Englishscore="<<Englishscore<<endl<<'\n';
}
void printscore(float ComputerSciencescore,float Englishscore)
{
float average=(ComputerSciencescore+Englishscore)/2;
cout<<"平均分是:"<<average<<endl;
}
};
void main( )
{
student stud1("Jenny",001,18,'w',90.3,80.5),stud2("Mike",002,20,'w',60.4,77.6);
cout<<"学生一";
stud1.printscore(80.5,90.3);
stud1.printinformation( );
cout<<"学生二";
stud2.printscore(60.4,77.6);
stud2.printinformation( );
}
练习2.
1.设计一个Line类,用于表示二维坐标系中任意一条直线,并输出该直线的属性。
分析:
(1)设计一个Point类,该类中有两个私有数据成员x,y,有三个公有的成员函数,分别用于返回x,y的值,以及显示x,y的值
(2)设计一个类Line,具有三个Point类型的私有数据成员point1,point2,和斜率K,有一个公有成员函数,用于输出Line的端点坐标,斜率等属性。
#include<iostream>
#include<string>
using namespace std;
class point
{
public:
point(int px,int py)
{
cout<<px;
x=px;
cout<<py;
y=py;
}
private:
int x;
int y;
public:
reback1()
{
return x;
}
reback2()
{
return y;
}
printpoint()
{
cout<<"x="<<x;
cout<<"y="<<y<<endl;
}
};
class line
{
public:
line(point ppoint1(x1,y1),point ppoint2(x2,y2)) {
float k;
k=(y1-y2)/(x1-x2);
cout<<ppoint1;
point=ppoint1;
cout<<ppoint2;
point=ppoint2;
cout<<pk;
k=pk;
}
public:
void printfpointset()
{
cout<<"point1="<<point1<<'\n';
cout<<"x1="<<x1<<'\n';
cout<<"y1="<<y1<<'\n';
cout<<"x2="<<x2<<'\n';
cout<<"y2="<<y2<<'\n';
cout<<"k="<<k<<endl<<'\n';
}
};
void main( )
{
line lin1((0,0),(1,1)),lin2((0,0),(1,2));
cout<<"第一条直线相关数据是:";
lin1.printfpointset();
cout<<"第二条直线相关数据是:";
lin2.printfpointset();
}
练习3. 友元函数
定义一个应试人员类interviewee,具有私有属性:姓名、年龄、应聘职位。
相关属性由带参构造函数进行初始化;
(1)定义其友元函数showinfo将该类对象的相关信息进行输出。
(2)定义其友元类inerviewer,具有成员函数showinfo,用于将interviewee的对象信息进行输出。
(3)在主函数中创建interviewee对象并初始化,使用友元函数和友元类成员函数对interviewee对象信息进行输出
#include<iostream>
#include<string>
using namespace std;
class interviewee
{
friend class interviewer;
friend void showinfo();
public:
interviewee(){}
interviewee(string pname,int page,string pposition)
{
cout<<pname<<'\n';
name=pname;
cout<<page<<'\n';
age=page;
cout<<pposition<<'\n';
position=pposition;
}
string name;
int age;
string position;
};
class interviewer
{
public:
interviewer(){}
void showinfo()
{
interviewee person1;
cout<<"面试人员姓名是:"<<;
cout<<"面试人员年龄是:"<<person1.age;
cout<<"面试人员需求职位是:"<<person1.position;
}
};
void main( )
{
interviewee("Taylor Swift",24,"Singer");
interviewer();
}
练习4.
定义一个类实现银行帐户的概念,账户信息包括“帐号”、“用户名”和“存款余额”,能完成的操作有“登录”、“存款”、“取款”、“查询余额”和”显示帐号”。
定义主函数,创建若干帐户,并各完成登录、显示账号、存款、取款、查询余额操作各一次。
#include<iostream>
#include<string>
using namespace std;
static char m;
class banksystem
{
public:
banksystem(){}
banksystem(int ID1,string name1,float balance1,int key1):ID(ID1),name(name1),balance(balance1),key(key1){}
void enter()
{
int ID1,key1;
char p,q;
cout<<"enter your ID:";
cin>>ID1;
if(ID1==201)
{
cout<<"enter your key:";
cin>>key1;
if(key1==key)
{
char a;
cout<<"登陆成功!"<<'\n';
loop:cout<<"请选择操作:显示账号输入p,查询余额输入q,退出输入除了p、q以外任意键"<<'\n';
cin>>m;
while(m=='p')
{
printaaa();
cout<<"请选择操作:存款输入i,取款输入o"<<'\n';
cin>>a;
if(a=='i')
{
float n;
cout<<"请输入存款数目";
cin>>n;
getin(n);
}
else
{float n;
cout<<"请输入取款数目";
cin>>n;
getoff(n);
if(n>balance)
{
cout<<"超出余额!"<<'\n';
goto loop;
}
}
cout<<"请选择操作:查询余额输入q,再次存取款输入p,退出按除了p、q 以外任意键"<<'\n';
cin>>m;
}
}
else {cout<<"密码错误!";}
}
if(m=='q')
{
inquire();
}
goto loop;
}
float getin(int x)
{
balance=balance+x;
return balance;
}
float getoff(int y)
{
balance=balance-y;
return balance;
}
void inquire()
{
cout<<"余额为:"<<balance<<'\n';
}
void printaaa()
{ cout<<"ID="<<ID<<'\n';
cout<<"用户名:"<<name<<'\n';
}
private :
string name;
int ID;
float balance;
int key;
};
void main()
{
banksystem user1(201,"xiaowang",1000,123456);
user1.enter();
}
实验六 C++程序设计之数组与指针篇
练习1.
定义一个如下初值的二维整型数组,并以指针和地址
两种方式输出该数组
下标操作:
#include <iostream.h>
#include <iomanip.h>
void main ( )
{ int a[5][6] ;
int i,j; ⎥⎥⎥⎥⎥⎥⎦
⎤⎢⎢⎢⎢⎢⎢⎣⎡464544434241363534333231262524232221161514131211654321
for(i=0;i<5;i++)
for(j=0;j<6;j++)
a[i][j]=i*10+j+1;
for(i=0;i<5;i++)
{ for(j=0;j<6;j++)
cout<<setw(5)<<a[i][j] ;
cout << endl ;
}
}
指针操作:
#include<iostream.h>
void main()
{
int a[5][6] ;
int i,j;
for(i=0;i<5;i++)
for(j=0;j<6;j++)
a[i][j]=i*10+j+1;
int n;
int *p;
p=&a[0][0];
for(n=0;n<30;n++)
{
cout<<*p++<<" ";
if((n+1)%6==0)
cout<<endl;
}
}
练习2.
通过指针实现:统计从键盘所输入的字符串的长度。
#include <iostream>
using namespace std;
int main()
{int length(char *p);
int len;
char str[40];
cout<<"请输入字符串:";
cin>>str;
len=length(str);
cout<<"字符串长度为:"<<len<<endl;
return 0;
}
int length(char *p)
{int n;
n=0;
while (*p!='\0')
{n++;
p++;
}
return(n);
}
练习3.使用数组实现选择排序算法
#include <iostream>
using namespace std;
int main()
{int length(char *p);
int len;
char str[40];
cout<<"请输入未排序前的一组整数,以回车结束:";
for(k=0;n!='\n';i++)
{
cin>>str[k];
}
length(str);
cout<<"排序后的一组整数为:"<<len<<endl;
return 0;
}
int length(char *p);
{
int temp;
for(i=0,j=1;str[i]<str[j];i++,j++)
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
没有上机环境无法调试,不过大概的思路和构架应该没问题
实验七C++程序设计之继承与派生篇
练习2.
❖编程实现:定义point作为基类,在此基类上派生出圆circle类,该类含有计算面积的成员函数;由circle类派生出圆柱cylinder类,该类含有计算柱体体积和表面积的成员函数。
❖定义circle和cylinder对象,并求circle对象的面积,cylinder对象的表面积和体积。
#include<iostream>
using namespace std;
class point
{
public:
point(){};
point(int,int){};
};
class circle:public point
{
public:
int a;
circle(){};
circle(int r1):r(r1){}
float area()
{
float s;
s=3.14*r*r;
return s;
}
private:
int r;
};
class cylinder:public circle
{
public:
cylinder(){};
cylinder(int h1,int m1):h(h1),circle(m1){};
float cubage()
{
float v;
v=area()*h;
return v;
}
private:
int h;
};
int main()
{
int m,h;
cout<<"请输入正整数圆半径:";
cin>>m;
circle s1(m);
cout<<"请输入正整数圆柱高:";
cin>>h;
cylinder v1(h,m);
cout<<"此圆的面积为:"<<s1.area()<<'\n';
cout<<"此圆柱的体积为:"<<v1.cubage()<<'\n';
return 0;
}
实验八C++程序设计之多态性
练习1.计算某些几何图形的面积,程序中用到的五个类之间的关系如图:
要求:
(1)使用虚函数设计程序,首先在类Shape中创建虚函数area,然后依次继承到子类中(2)在main函数中,使用Shape类型指针引用各子类的对象及成员,即创建Shape类型指针,然后创建Triangle /Rectangle /Circle /Square的对象并进行初始化,之后将指针依次指向Triangle/Rectangle/Circle/Square的对象,并用指针引用方式引用他们的area函数用于输出各种图形的面积。
#include<iostream>
using namespace std;
class shape
{
double x,y;
public:
shape(){}
shape(double i,double j)
{x=i;y=j;}
virtual double area()
{return 0.0;}
};
class rectangle:public shape//矩形
{
double p,q;
public:
rectangle(double u,double d)
{p=u;q=d;}
virtual double area()
{return p*q;}
};
class triangle:public shape//三角形
{
double l,h;
public:
triangle(double f,double o)
{l=f;h=o;}
virtual double area()
{return l*h/2;}
};
class square:public shape//正方形
{
double n;
public:
square(double m)
{n=m;}
virtual double area()
{return n*n;}
};
class circle:public shape//圆
{
double r;
public:
circle(double t)
{r=t;}
virtual double area()
{return 3.14*r*r;}
};
void fun(shape &s)
{cout<<s.area()<<'\n'<<endl;}
void main()
{
rectangle rec(10,10);
fun(rec);
triangle tri(10,10);
fun(tri);
square squ(10);
fun(squ);
circle cir(10);
fun(cir);
}
练习2.
编程实现:
❖定义员工类Employee,此类具有私有的数据成员用以表示员工的姓名,工号和薪资。
并有带参构造函数用于相关数据成员的初始化,另外,由一个公有成员函数show 用于输出员工的所有数据信息。
❖在Employee类上派生出销售经理类Salemanager,该类具有一个新的属性--销售业绩;并有带参构造函数用于相关数据成员的初始化,另外,由一个公有成员函数show 用于输出销售经理的所有信息。
❖在Employee类上派生出技术工人类Technician,该类具有一个新的属性—工作时间;
并有带参构造函数用于相关数据成员的初始化,另外,由一个公有成员函数show
用于输出技术工人的所有信息。
❖定义Salemanager和Technician对象,用于表示某两个员工,并将相关信息进行输出。
#include<iostream>
#include<string>
using namespace std;
class employee
{
public:
employee(string name1,int id1,float salary1):name(name1),id(id1),salary(salary1){}
virtual void show()
{
cout<<"员工姓名:"<<name<<'\n';
cout<<"员工编号:"<<id<<'\n';
cout<<"员工工资:"<<salary<<'\n';
}
string get_name()
{
return name;
}
int get_id()
{
return id;
}
float get_salary()
{
return salary;
}
private:
string name;
int id;
float salary;
};
class salemanager:public employee
{
public:
salemanager(string name1,int id1,float salary1,int a):employee(string name1,int id1,float salary1)
{
achievement=a;
}
virtual void show()
{
cout<<"经理姓名:"<<get_<<'\n';
cout<<"经理编号:"<<get_id.id<<'\n';
cout<<"经理工资:"<<get_salary.salary<<'\n';
cout<<"经理业绩:"<<achievement<<'\n';
}
private:
float achievement;
};
class technician:public employee
{
public:
technician(string name1,int id1,float salary1,int w):employee(string name1,int id1,float salary1)
{
worktime=w;
}
virtual void show()
{
cout<<"工人姓名:"<<get_<<'\n';
cout<<"工人编号:"<<get_id.id<<'\n';
cout<<"工人工资:"<<get_salary.salary<<'\n';
cout<<"工人工时:"<<worktime<<'\n';
}
private:
int worktime;
};
void fun(employee &s)
{
cout<<s.show()<<'\n'<<endl;
}
void main()
{
salemanager sal(lihua,001,2000,10000);
fun(str);
technician tec(xiaoming,002,500,12);
fun(tec;)
}
练习3.
(1)设计一个矩阵类matrix,并定义其构造函数用于对象初始化(2)在此类中以成员函数的形式重载“+”运算符实现两个矩阵求和。
(3)在main函数中定义两个矩阵对象,求两个矩阵的和
之后
(4)在此类中以友元函数的形式重载“+”运算符实现两个矩阵求和。
(3)在main函数中定义两个矩阵对象,求两个矩阵的和
#include<iostream>
#include<string>
using namespace std;
class matrix
{
int n[2][3];
public:
matrix(){}
matrix(int ap[2][3])
{
for(int i=0;i<2;i++)
for (int j=0;j<3;j++)
{n[i][j]=ap[i][j];}
}
void print()
{
cout<<"矩阵为:"<<endl;
for(int i=0;i<2;i++)
for (int j=0;j<3;j++)
{cout<<n[i][j]<<" ";}
cout<<endl;
}
friend matrix operator + (matrix a,matrix b)
{
matrix c;
int i,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
c.n[i][j]=a.n[i][j]+b.n[i][j];
return c;
}
};
void main()
{
int a[2][3]={0,0,1,1,1,2};
int b[2][3]={2,2,3,1,1,3};
matrix m1(a),m2(b),m3;
m3=m1+m2;
m3.print();
}
实验九C++程序设计之模板
练习1
要求:
1)编写求两个数中最大值的函数模板,并计算几个模板函数的值,参数类型分别为int ,float,double
2)重载函数模板,求实现三个数中最大值的函数模板,并计算类型分别int ,float,double 的模板函数的值
#include<iostream>
using namespace std;
template<class T>
T max(T x,T y)
{
return(x>y)?x:y;
}
template<class T>
T max(T x,T y,T z)
{
T temp;
if(x<y)
{
temp=y;
y=x;
x=temp;
}
if(x<z)
{
temp=z;
z=x;
x=temp;
}
return x;
}
void main()
{
int x1,y1,z1;
float x2,y2,z2;
double x3,y3,z3;
char m;
int n;
cout<<"请选择参数个数2或3:"<<endl;
cin>>n;
if(n!=3)
{
cout<<"请选择数据类型:整形i,浮点型f,双精度型d"<<endl;
cin>>m;
if(m=='i')
{
cout<<"请输入2个整型数据,用空格分隔:"<<endl;
cin>>x1>>y1;
cout<<"The max of x1,y1 is:"<<max(x1,y1)<<endl;
}
else if(m=='f')
{
cout<<"请输入2个实型数据,用空格分隔:"<<endl;
cin>>x2>>y2;
cout<<"The max of x2,y2 is:"<<max(x2,y2)<<endl;
}
else if(m=='d')
{
cout<<"请输入2个双精度数据,用空格分隔:"<<endl;
cin>>x3>>y3;
cout<<"The max of x3,y3 is:"<<max(x3,y3)<<endl;
}
}
else
{
cout<<"请选择数据类型:整形i,浮点型f,双精度型d"<<endl;
cin>>m;
if(m=='i')
{
cout<<"请输入3个整型数据,用空格分隔:"<<endl;
cin>>x1>>y1>>z1;
cout<<"The max of x1,y1,z1 is:"<<max(x1,y1,z1)<<endl;
}
else if(m=='f')
{
cout<<"请输入3个实型数据,用空格分隔:"<<endl;
cin>>x2>>y2>>z2;
cout<<"The max of x2,y2,z2 is:"<<max(x2,y2,z2)<<endl;
}
else if(m=='d')
{
cout<<"请输入3个双精度数据,用空格分隔:"<<endl;
cin>>x3>>y3>>z3;
cout<<"The max of x3,y3,z3 is:"<<max(x3,y3,z3)<<endl;
}
}
}
练习2
#include<iostream.h>
template< typename T >
class A
{ public :
A( T x ) { t = x ; }
void out() { cout << t << endl ; }
protected :
T t ;
} ;
要求:
1)从类A公有派生出另外一个类模板B,B有一个新的数据成员类型为Tb,并有一个新的成员函数display用于输出派生类新数据成员的值;在main函数中创建派生类B的对象并赋予T和Tb不同的数据类型,调用对象的成员函数输出所有数据成员的值
2)从类A公有派生出另外一个一般类C,C有一个新的数据成员类型为double类型,并有一个新的成员函数display,display定义中,首先调用模板A的out函数输出t的值,然后输出派生类新数据成员的值;在main函数中创建派生类C的对象调用对象的成员函数输出所有数据成员的值
#include<iostream>
using namespace std;
template<typename T>
class A
{
public:
A(T x){t=x;}
void out()
{
cout<<t<<endl;
}
protected:
T t;
};
template<class T,class Tb>
class B:public A<T>
{
public:
B(Tb p1,T a):A<T>(a),p(p1){}
void display()
{
cout<<p<<endl;
}
private:
Tb p;
};
class C:public A<int>
{
public:
C(double s1,int a):A<int>(a),s(s1){}
void display()
{
out();
cout<<s<<endl;
}
private:
double s;
};
void main()
{
B<int,float> b1(100,200);
b1.display();
C c1(0.5,5);
c1.display();
}
实验十一C++程序设计之I/O
练习1 按下列要求编写一个程序
将下列信息存放到一个文件中。
关于大学生的信息:
姓名、学号、某门功课的成绩
关于硕士生的信息:
姓名、学号、某门功课的成绩、导师的
姓名
注意:
1大学生类为父类,硕士生类为子类
2使用运算符重载的方式输出相关信息
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
using namespace std;
class collegestudent
{
public:
collegestudent(){}
collegestudent(int id1,string name1,float score1):id(id1),name(name1),score(score1){} string name;
int id;
float score;
friend ostream & operator<<(ostream &os,collegestudent &obj);
};
class masterstudent:public collegestudent
{
public:
masterstudent(){}
masterstudent(int id1,string name1,float score1,string teachername1):id(id1),name(name1),score(score1),teachername(teachername1){} string name;
int id;
float score;
string teachername;
friend ostream & operator<<(ostream &os,masterstudent &obj);
};
ostream & operator<<(ostream &os,collegestudent &obj)
{
os<<"="<<<<endl;
os<<"sc1.id="<<obj.id<<endl;
os<<"sc1.score="<<obj.score<<endl;
return os;
}
ostream & operator<<(ostream &os,masterstudent &obj)
{
os<<"="<<<<endl;
os<<"ms1.id="<<obj.id<<endl;
os<<"ms1.score="<<obj.score<<endl;
os<<"ms1.teachername="<<obj.teachername<<endl;
return os;
}
void main()
{
collegestudent sc1(001,"xiaoming",99);
masterstudent ms1(002,"lily",100,"liuxiang");
fstream outfile;
outfile.open("file1.txt",ios::out);
if(!outfile)
{
cout<<"file1.txt can't open.\n";
abort();
}
outfile<<sc1<<endl;
outfile<<ms1<<endl;
outfile.close();
}
试用try ,catch 和throw 语句实现求解实系数一元二次方程的实数根。
1.系数自行输入
2.a=0时抛出异常二次项系数异常, catch 语句块捕获异常并进行提示:该方程不是二次方程
3.当方程没有实数根式产生异常,有throw 抛出无实数根异常,catch 语句块捕获异常并进行提示:该方程没有实数根;
#include<math.h>
void main()
{
double a,b,c,x;
cout<<"请输入整数a:"<<endl;
cin>>a;
cout<<"请输入整数b:"<<endl;
cin>>b;
cout<<"请输入整数c:"<<endl;
cin>>c;
double d;
double x1,x2;
d=b*b-4*a*c;
try
{
if(a==0) throw a;
if(d<0) throw d;
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
}
catch(int)
{
cout<<"该方程不是二次方程:"<<endl;
} catch(double)
2=++c bx ax
{
cout<<"该方程没有实数根:"<<endl;
}
}。