C++例题及答案
作业1:输入两个变量,输出两个变量的最大值。要求:使用C++语言,使用max函数完成求最大值的操作。
#include
int max(int x, int y)
{ return (x>y?x:y);}
int main(int argc, char* argv[])
{
int a,b;
cout<<"输入两个数,求出最大值:" <cin>>a>>b;
cout<<"最大值为:"<cin>>a;
return 0;
}
作业2:(1) 输入下列简单C++程序,完成编译、连接、运行,熟悉C++程序的开发过程。#include
using namespace std;
const float PI = 3.1416;
void main()
{
int iType;
float radius, a, b, area;
cout << "图形的类型为?(1-圆形2-长方形3-正方形):";
cin >> iType;
switch(iType)
{
case 1:
cout << "圆的半径为:";
cin >> radius;
area = PI * radius * radius;
cout<<"面积为:"<
break;
case 2:
cout << "矩形的长为:";
cin >> a;
cout << "矩形的宽为:";
cin >> b;
area = a * b;
cout<<"面积为:"<
break;
case 3:
cout << "正方形的边长为:";
cin >> a;
area = a * a;
cout<<"面积为:"<
break;
default:
cout << "不是合法的输入值!"<}
}
(2) 改写以上程序,要求:编写三个内联函数分别完成1-圆形2-长方形3-正方形的求面积操作。
(3) 改写以上程序,要求:编写三个函数分别完成1-圆形2-长方形3-正方形的求面积操作,以上三个函数分别写在三个文件中。
(4) 思考:可否使用重载函数完成三种形状的求面积操作?
答案:(2) 改写以上程序,要求:编写三个内联函数分别完成1-圆形2-长方形3-正方形的求面积操作。
#include
using namespace std;
const double PI = 3.1416;
inline float cirlearea(float);
inline float rectarea(float);
inline float zrectarea(float);
void main()
{
int iType;
float radius, a, b, area;
cout << "图形的类型为?(1-圆形2-长方形3-正方形):";
cin >> iType;
switch(iType)
{
case 1:
cout << "圆的半径为:";
cin >> radius;
area = cirlearea(radius);
cout<<"面积为:"<
break;
case 2:
cout << "矩形的长为:";
cin >> a;
cout << "矩形的宽为:";
cin >> b;
area = a * b;
cout<<"面积为:"<
break;
case 3:
cout << "正方形的边长为:";
cin >> a;
area = a * a;
cout<<"面积为:"<
break;
default:
cout << "不是合法的输入值!"<}
}
float cirlearea(float r)
{
float area;
return area = PI * r * r;
}
float rectarea(float a,float b)
{
float area;
return area = a * b;
}
float zrectarea(float a)
{
area = a * a;
}
(3) 改写以上程序,要求:编写三个函数分别完成1-圆形2-长方形3-正方形的求面积操作,以上三个函数分别写在三个文件中。
#include
using namespace std;
const double PI = 3.1416;
inline float cirlearea(float,float);
inline float rectarea(float,float);
inline float zrectarea(float,float);
void main()
{
int iType;
float radius, a, b, area;
float (*carea)(float,float);
cout << "图形的类型为?(1-圆形2-长方形3-正方形):";
cin >> iType;
switch(iType)
{
case 1:
carea=cirlearea;
cout << "圆的半径为:";
cin >> radius;
area = (*carea)(radius,a=0);
cout<<"面积为:"<
break;
case 2:
carea=rectarea;
cout << "矩形的长为:";
cin >> a;
cout << "矩形的宽为:";
cin >> b;
area = (*carea)(a,b);
cout<<"面积为:"<
break;
case 3:
carea=zrectarea;
cout << "正方形的边长为:";
cin >> a;
area = (*carea)(a,a);
cout<<"面积为:"<
break;
default:
cout << "不是合法的输入值!"<}
}
float cirlearea(float r,float def)
{
float area;
return area = PI * r * r;
}
float rectarea(float a,float b)
{
return area = a * b;
}
float zrectarea(float a,float def)
{
float area;
return area = a * a;
}
作业3:
(1)设计一个类CRectangle,要求如下所述:
a. 该类中的私有成员变量存放Rectangle的左上角x,y和它的长、宽,并且它们的默认值都是10。
b. 通过函数设置其长和宽,并确保长和宽在(0,50)范围之内。
c. 定义求它的周长的函数Perimeter。
#include
using namespace std;
class Rect
{
private:
int length;
int width;
int perim;
public:
Rect()
{
length =10;
width =10;
}
Rect(int length,int width)
{
this->length = length;
this->width = width;
}
void setLength(int length)
{
if(length >0 && length <50)
{
this->length = length;
}
else
{
cout<<"INPUT ERROR!"<}
}
void setWidth(int width)
{
if(width >0 && width <50)
{
this->width = width;
}
else
{
cout<<"INPUT ERROR!"<}
}
float permi()
{
return length*width;
}
};
int main()
{
Rect a;
a.setLength(20);
a.setWidth(20);
cout<cin.get();
return 0;
}
(2)在实验任务一的基础上,要求有如下成员函数。
a. Move:从一个位置移动到另一个位置。
b. Size:改变矩形的大小。
c. Where:返回矩形左上角的坐标值。
d. Area:计算面积。
clase CRectangle
{
public:
void Move(int dx,int dy){left+=dx;top+=dy;};
void Size(int newW,int newH){width=newW;height=newH;};
void Where(int &x,int &y){x=left;y=top;};
int Area(){return(width*height);};
private:
int left,top;//矩形的左上角横坐标和纵坐标
int width,height;//矩形的宽度和高度
};
作业4:声明一个CPU类,要求:
(1)包含主频(frequency),字长(wordlength),CPU倍频系数(coefficient)属性,其中字长为枚举型enum cpu_wordlen={W16,W32,W64,W128,W256}, frequency是单位为GHz的实数,coefficient为浮点型数据。两个公有成员函数run和stop分别表示CPU的运行与停止。
(2)请在构造函数(带参数和不带参数)、拷贝构造函数、析构函数、run和stop函数体给出相应的提示(输出提示字符串,例如“the CPU is running!”)。
(3)说明并实现这个类,观察构造函数、拷贝构造函数和析构函数的调用顺序。
主函数内容如下所示:
int main()
{
double x=3.2,y=4.5;
CPU mycpu;
mycpu.run();
mycpu.stop();
CPU hiscpu(x,W64,y);
hiscpu.run();
hiscpu.stop();
CPU hercpu(hiscpu);
hercpu.run();
hercpu.stop();
return 0;
}
答案:
#include
using namespace std;
enum cpu_wordlen{W16,W32,W64,W128,W256};
class CPU
{
public:
CPU()
{
frequency=0;
wordlength=W16;
coefficient=0;
cout<<"frequency: "<cout<<"calling the constructor without parameters!"<}
CPU(float f,cpu_wordlen l,float c)
{
frequency=f;
wordlength=l;
coefficient=c;
cout<<"frequency: "<cout<<"calling the constructor with parameters!"<}
CPU(CPU &C)
{
frequency=C.frequency;
wordlength=C.wordlength;
coefficient=C.coefficient;
cout<<"calling the copy constructor!"<}
~CPU()
{
cout<<"calling the deconstructor!"<}
void run(){
cout<<"CPU is running!"<}
void stop(){
cout<<"CPU stops!"<}
private:
double frequency;
enum cpu_wordlen wordlength;
double coefficient;
};
int main()
{
double x=3.2,y=4.5;
CPU mycpu;
mycpu.run();
mycpu.stop();
CPU hiscpu(x,W64,y);
hiscpu.run();
hiscpu.stop();
CPU hercpu(hiscpu);
hercpu.run();
hercpu.stop();
return 0;
}
作业5:
声明一个学生信息类:
要求有学生的基本信息(自定),以及显示和设置这些信息的接口(公有成员函数),要求定义构造函数(带参数和不带参数)和析构函数以及复制构造函数。并进行测试。
(2)执行下列程序,观察结果,并给出所有对象的构造、复制构造和析构函数的调用顺序。#include
class point
{ int x,y;
public:
point(int a,int b)
{x=a;y=b;
cout<<"calling the constructor function."<}
point(point &p);
friend point move(point q);
~point(){cout<<"calling the destructor function.\n";}
int getx() {return x;}
int gety() {return y;}
};
point::point(point &p)
{x=p.x; y=p.y;
cout<<"calling the copy_initialization constructor function.\n";
}
point move(point q)
{cout<<"OK!\n";
int i,j;
i=q.x+10;
j=q.y+20;
point r(i,j);
return r;
}
void main()
{ point m(15,40),p(0,0);
point n(m);
p=move(n);
cout<<"p="<
}
答案:
#include
#include
using namespace std;
class Student
{
public:
Student(char*, int, bool);
Student(const Student&);
~Student();
Student& operator =(const Student&);
void print();
private:
char* name;
int age;
bool sex;
int len;
};
Student::Student(char* name, int age, bool sex)
{
len = strlen(name);
this->name = new char[len+1];
strcpy(this->name, name);
this->age = age;
this->sex = sex;
}
Student::Student(const Student& stu)
{
len = strlen(https://www.doczj.com/doc/4515300860.html,);
this->name = new char[len+1];
strcpy(this->name, https://www.doczj.com/doc/4515300860.html,);
this->age = stu.age;
this->sex = stu.sex;
}
Student::~Student()
{
len = 0;
delete[] name;
}
Student& Student::operator =(const Student& stu)
{
len = strlen(https://www.doczj.com/doc/4515300860.html,);
this->name = new char[len+1];
strcpy(this->name, https://www.doczj.com/doc/4515300860.html,);
this->age = stu.age;
this->sex = stu.sex;
return *this;
}
void Student::print()
{
cout << name << '\t';
if (sex) cout << "男";
else cout << "女";
cout << '\t' << age << endl;
}
int main()
{
Student s1("张三", 20, true);
Student s2(s1);
Student s3 = s1;
s1.print();
s2.print();
s3.print();
return 0;
}
(2)
M: point(int a,int b)构造,point(point &p)复制
P:point(int a,int b)构造,move(point q) ,gety(),getx() N:point(point &p)复制,
R:point(int a,int b)构造,point(point &p)复制
~point()析构,~point()析构
~point()析构
作业6:
1.设计一个解决王婆卖瓜问题的程序。王婆卖瓜,每卖一个瓜,需记录该瓜的重量,还要记录所卖出的总重量和总个数。同时还允许退瓜。设计一个具有静态数据、函数成员的watermelon类。
实现提示:西瓜类中,设计3个数据成员(重量weight、总重量total_weiht、总个数total_number)。因为不论西瓜是否存在,总重量total_weiht和总个数total_number这两个数据总是要保留的,因此这两个数据要申明为静态数据成员。
成员函数:卖瓜用构造函数模拟,退瓜用析构函数模拟,瓜重用显示disp()成员函数模拟。为了用不与特定对象相联系的静态成员函数来访问静态数据,还需要定义一个显示总重量和总数的静态成员函数total_disp()。
2.设计一个程序,其中有3个类,即CBank,BBank和GBank,分别为中国银行类、工商银行类和农业银行类。每个类都包含一个私有数据balance,用于存放储户在该行的存款数,另有一个友元函数total用于计算储户在这3家银行中的总存款数。
答案:
1.
#include
#include
class CMelonSale
{
public:
CMelonSale(float vWeight)
{
m_Weight = vWeight;
m_TotalWeight += m_Weight;
m_WeightStore.push_back(m_Weight);
m_TotalNum++;
}
~CMelonSale()
{
m_TotalNum--;
}
static void totalDisP()
{
std::cout<<"The Total Num is"<std::cout<<"The Total Weight are"<}
static void showWeight()
{
std::vector::iterator itr;
for (itr = m_WeightStore.begin();itr!=m_WeightStore.end();itr++)
{
std::cout<<*itr<}
protected:
private:
static int m_TotalNum ;
static float m_TotalWeight ;
float m_Weight;
static std::vector m_WeightStore;
};
int CMelonSale::m_TotalNum = 0;
float CMelonSale::m_TotalWeight = 0.0; std::vector CMelonSale::m_WeightStore ; int main()
{
CMelonSale a(0.6);
CMelonSale b(0.8);
CMelonSale c(0.9);
CMelonSale d(1.1);
CMelonSale::totalDisP();
CMelonSale::showWeight();
return 0;
}
2.
#include
using namespace std;
class CBank
{
private:
float balance;
public:
CBank(float balance = 0)
{
this->balance = balance;
}
friend class user;
};
class BBank
{
private:
float balance;
public:
BBank(float balance = 0)
{
this->balance = balance;
friend class user;
};
class GBank
{
private:
float balance;
public:
GBank(float balance = 0)
{
this->balance = balance;
}
friend class user;
};
class user
{
private:
CBank C;
BBank B;
GBank G;
public:
user(CBank c,BBank b,GBank g)
{
C = c;
B = b;
G = g;
}
void total()
{
cout<}
};
int main()
{
CBank c(100.1);
BBank b(100.2);
GBank g(100.3);
user u(c,b,g);
u.total();
}
作业7:
1.要求用多文件结构实现该问题(可以使用类向导)。声明一个学生类和一个教师类:
要求有学生和教师的基本信息(自定),以及显示和设置这些信息的接口(公有成员函数),要求定义构造函数(带参数和不带参数)和析构函数以及复制构造函数。并进行测试。
#include
using namespace std;
class Student
{public:
Student(int,char[],char,float);
int get_num(){return num;}
char * get_name(){return name;}
char get_sex(){return sex;}
void display()
{cout<<"num:"<private:
int num;
char name[20];
char sex;
float score;
};
Student::Student(int n,char nam[],char s,float so)
{num=n;
strcpy(name,nam);
sex=s;
score=so;
}
class Teacher
{public:
Teacher(){}
Teacher(Student&);
Teacher(int n,char nam[],char sex,float pay);
void display();
private:
int num;
char name[20];
char sex;
float pay;
};
Teacher::Teacher(int n,char nam[],char s,float p)
{num=n;
strcpy(name,nam);
sex=s;
pay=p;
}
Teacher::Teacher(Student& stud)
{num=stud.get_num();
strcpy(name,stud.get_name());
sex=stud.get_sex();
pay=1500;}
void Teacher::display()
{cout<<"num:"<int main()
{Teacher teacher1(10001,"Li",'f',1234.5),teacher2;
Student student1(20010,"kuang",'m',89.5);
cout<<"student1:"<student1.display();
teacher2=Teacher(student1);
cout<<"teacher2:"<teacher2.display();
return 0;
}
作业8:1)学生信息管理系统
–创建一个班级学生的信息,班级人数手动输入
–创建一个学生信息类
(2)完善以上程序:加入一个班级类,并测试。
–班级类中包含一个动态对象数组用于存储学生信息,构造函数中根据学生人数确定数组长度–析构函数中删除动态数组
–采用多文件结构
答案:
#include
class Person
{
char name[10];
char sex;
int age;
public:
void input()
{
cout<<"请输入姓名:";
cin>>name;
cout<<"请输入性别:";
cin>>sex;
cout<<"请输入年龄:";
cin>>age;
}
void display()
{
cout<<"姓名:"<}
};
class Student:public Person
{
char sno[10];
int score;
public:
void input()
{
Person::input();
cout<<"请输入学号:";
cin>>sno;
cout<<"请输入成绩:";
cin>>score;
}
void display()
{
Person::display();
cout<<"学号;"<}
};
void main()
{
Student s1;
s1.input();
s1.display();
}
作业9:由在校人员类(Person)作为基类派生出学生类(Student):
在校人员类有成员数据:编号(ID)、姓名(name)、性别(sex)、年龄(age),要求有如下成员函数:构造函数、获取编号的函数和输出所有成员的函数。把在校人员类作为基类,通过公有继承,派生学生类,派生类新增成员数据有数学(math)、物理(physical)、英语(english)和C++程序设计(cpp)四门课程以及总成绩(total);新增成员函数有构造函数和输出所有成员的函数。main()完成派生类对象的定义和有关成员函数的测试。
参考程序:
#include
#include
usingnamespacestd;
class Person{
public:
Person(int i,char *n, char s, int a){
ID=i;
name=n;
sex=s;
age=a;
};
……
};
class Student:public Person{
public:
Student(int i,char *n,char s,int a,float m,float p,float e,float c):Person(i,n,s,a){ math=m;
physical=p;
english=e;
cpp=c;
total=math+physical+english+cpp;
}
……
};
void main(){
Person p1(1,"张帅",'M',22);
p1.show();
cout<Student s1(9901,"林维",'S',21,65,70,75,88);
s1.show();
}
答案:
#include
#include
using namespace std;
class Person
{
public:
Person(int i,char *n, char s, int a)
{
ID=i;
name=n;
sex=s;
age=a;
};
int getID()
{
return ID;
}
v cout<<"ID:"<cout<<"name:"<cout<<"oid show(){
sex:"<cout<<"age:"<}
private:
int ID;
string name;
char sex;
int age;
};
class Student:public Person
{
public:
Student(int i,char *n,char s,int a,float m,float p,float e,float c):Person(i,n,s,a){
math=m;
physical=p;
english=e;
cpp=c;
total=math+physical+english+cpp;
}
void show(){
Person::show();
cout<<"math:"<