***c++平时练习题,部分课后习题***上机练习题1-5
第一次作业
在屏幕上输出Hello,World!
程序参考的输入(没有输入:)
程序参考的输出:
Hello,World!
代码:
#include
using namespace std;
int main()
{
cout<<“Hello,World!”<<endl;
return 0;
}
第二次作业
2.1(习题4-8)定义一个Dog类,包含age,weight等属性,以及对这些属性操作的方法。实现并测试这个类。
程序参考的输入(提示“Input Age and Weight:”):
Input Age and Weight:3 20
程序参考的输出:
Dog Age:3 years
Dog Weight:20kg
参考代码:
#include
using namespace std;
class Dog
{
public:
Dog()
{
cout<<“Input Age and Weight:”;
cin>>age>>weight;
}
void showDog()
{
cout<<“Dog Age:”<<age<<" years"<<endl;
cout<<“Dog Weight:”<<weight<<“kg”<<endl;
}
private:
int age,weight;
};
int main()
{
Dog d;
d.showDog();
return 0;
}
我的:
#include
using namespace std;
class Date
{
public:
Date()
{
cout<<“Input Age and Weight:”;
cin>>a>>w;
}
void showdog()
{
cout<<“Dog Age:”<<a<<" years"<<endl;
cout<<“Dog Weight:”<<w<<“kg”<<endl;
}
private:
int a,w;
};
int main()
{
Date d;
d.showdog();
}
2.2(习题4-13)定义一个Circle类,有数据成员radius(半径),成员函数getArea()计算圆的面积。构造一个Circle的对象进行测试(注:圆周率取值3.14)。
程序参考的输入(提示“Input Radius:”):
Input Radius:10
程序参考的输出:
Area:314
参考代码:
#include
using namespace std;
const float PI = 3.14;
class Circle
{
public:
Circle()
{
cout<<“Input Radius:”;
cin>>radius;
}
float getArea()
{
return PIradiusradius;
}
private:
float radius;
};
int main()
{
Circle c;
cout<<“Area:”<<c.getArea()<<endl;
return 0;
}
我的:
#include
#include <math.h>
using namespace std;
class Circle
{
public:
Circle()
{
cout<<“Input Radius:”;
cin>>radius;
}
void getArea()
{
area=( 3.14radiusradius);
cout<<“Area:”<<area<<endl;
}
private:
float radius,area;
};
int main()
{
Circle c;
c.getArea();
}
第三次作业
3.1(习题4-9)设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算矩形的面积。
程序参考的输入(提示“Input Point A:”输入左下角的坐标;提示“Input Point B:”输入左下角的坐标):
Input Point A:0 0
Input Point B:8.2 4.5
程序参考的输出:
Rectangle Area:36.9
参考代码:
#include
using namespace std;
class Rectangle
{
public:
float getArea()
{
return (x2-x1)(y2-y1);
}
Rectangle()
{
cout<<“Input Point A:”;
cin>>x1>>y1;
cout<<“Input Point B:”;
cin>>x2>>y2;
}
private:
float x1,y1,x2,y2;
};
int main()
{
Rectangle r;
cout<<“Rectangle Area:”<<r.getArea()<<endl;
return 0;
}
我的:
#include
using namespace std;
class Rectangle{
public:
void getarea(){
area=(b_x - a_x)(b_y - a_y);
cout<<“Rectangle Area:”<<area<<endl;
}
void show(){
cout<<“Input Point A:”;
cin>>a_x>>a_y;
cout<<“Input Point B:”;
cin>>b_x>>b_y;
}
private:
float a_x,a_y;
float b_x,b_y;
float area;
};
int main(){
Rectangle r;
r.show();
r.getarea();
}
3.2(习题4-20)定义一个复数类Complex,使得下面的代码能够工作。(注:下列代码需放在主函数中。)
Complex c1(3,5); //用复数3+5i初始化c1
Complex c2=4.5; //用实数4.5初始化c2
c1.add(c2); //将c1与c2相加,结果保存在c1中
c1.show(); //将c1输出(这时的结果应该是7.5+5i)
参考代码;
#include
using namespace std;
class Complex
{
public:
Complex(float r=0,float i=0)
:real®,imag(i)
{}
void add(const Complex &c)
{
real+=c.real;
imag+=c.imag;
}
void show()
{
cout<<real<<"+"<<imag<<“i”<<endl;
}
private:
float real,imag;
};
int main()
{
Complex c1(3,5);
Complex c2=4.5;
c1.add(c2);
c1.show();
return 0;
}
我的:
#include
using namespace std;
class Complex{
public:
Complex(float r1,float i1){
r=r1;
i=i1;
}
Complex(float r1){
r=r1;
i=0;
}
void add(Complex &c);
void show();
private:
float r,i;
};
void Complex::add(Complex &c){
r+=c.r;
i+=c.i;
}
void Complex::show(){
cout<<r<<’+’<<i<<‘i’<<endl;
}
int main(){
Complex c1(3,5);
Complex c2=4.5;
c1.add(c2);
c1.show();
return 0;
}
第四次作业
4.1(习题5-3)下面程序的运行结果是什么?实际运行一下,看看与你的设想有何不同。
注:只需调试一下代码即可,原样输入。
#include
using namespace std;
int x=5,y=7;
void myFunction(){
int y=10;
cout<<“x from myFunction:”<<x<<endl;
cout<<“y from myFunction:”<<y<<endl;
}
int main()
{
cout<<“x from main:”<<x<<endl;
cout<<“y from main:”<<y<<endl;
myFunction();
cout<<“Back from myFunction!”<<endl;
cout<<“x from main:”<<x<<endl;
cout<<“y from main:”<<y<<endl;
return 0;
}
代码:
#include
using namespace std;
int x=5,y=7;
void myFunction(){
int y=10;
cout<<“x from myFunction:”<<x<<endl;
cout<<“y from myFunction:”<<y<<endl;
}
int main()
{
cout<<“x from main:”<<x<<endl;
cout<<“y from main:”<<y<<endl;
myFunction();
cout<<“Back from myFunction!”<<endl;
cout<<“x from main:”<<x<<endl;
cout<<“y from main:”<<y<<endl;
return 0;
}
我的:
#include
using namespace std;
int x=5,y=7;
void myFunction(){
int y=10;
cout<<“x from myFunction:”<<x<<endl;
cout<<“y from myFunction:”<<y<<endl;
}
int main()
{
cout<<“x from main:”<<x<<endl;
cout<<“y from main:”<<y<<endl;
myFunction();
cout<<“Back from myFunction!”<<endl;
cout<<“x from main:”<<x<<endl;
cout<<“y from main:”<<y<<endl;
return 0;
}
4.2(习题5-7)定义一个Cat类,拥有静态数据成员numOfCats,记录Cat的个体数目;静态成员函数getNumOfCats(),读取numOfCats。设计程序测试这个类,体会静态数据成员和静态成员函数的用法。
主函数如下:
int main()
{
Cat c;
cout<<“Num:”<<Cat::getNumOfCats()<<endl;
Cat carr[4];
cout<<“Num:”<<Cat::getNumOfCats()<<endl;
return 0;
}
参考的输出结果为:
Num:1
Num:5
参考代码:
#include
using namespace std;
class Cat
{
public:
Cat()
{
numOfCats++;
}
~Cat()
{
numOfCats–;
}
static int getNumOfCats()
{
return numOfCats;
}
private:
static int numOfCats;
};
int Cat::numOfCats = 0;
int main()
{
Cat c;
cout<<“Num:”<<Cat::getNumOfCats()<<endl;
Cat carr[4];
cout<<“Num:”<<Cat::getNumOfCats()<<endl;
return 0;
}
我的:
#include
using namespace std;
class Cat{
public:
void get(int m){
numOfCats+=m;
}
static int getNumOfCats(){
cout<<numOfCats<<endl;
return 0;
}
private:
static int numOfCats;
};
int Cat::numOfCats=1;
int main()
{
Cat c;
cout<<“Num:”;
Cat::getNumOfCats();
c.get(4);
cout<<“Num:”;
Cat::getNumOfCats();
return 0;
}
第五次作业
5.1(习题5-13)定义类X,Y,Z,函数h(X*),满足:类X有私有成员i,Y的成员函数g(X*)是X的友元函数,实现对X的成员i加1;类Z是类X的友元类,其成员函数f(X*)实现对X的成员i加5;函数h(X*)是X的友元函数,实现对X的成员i加10。主函数实现测试。主函数需要定义如下:
int main()
{
int a;
cin>>a;
X x(a);
Y y;
Z z;
y.g(x);
z.f(x);
h(x);
x.print();
}
参考的输入:
3
参考的输出:
19
参考代码:
#include
using namespace std;
class X;
class Y
{
public:
void g(X &x);
};
class Z
{
public:
void f(X &x);
};
class X
{
public:
X(int i):i(i){}
friend void Y::g(X &x);
friend class Z;
friend void h(X &x);
void print()
{
cout<<i;
}
private:
int i;
};
void Y::g(X &x)
{
x.i += 1;
}
void Z::f(X &x)
{
x.i += 5;
}
void h(X &x)
{
x.i += 10;
}
int main()
{
int a;
cin>>a;
X x(a);
Y y;
Z z;
y.g(x);
z.f(x);
h(x);
x.print();
}
我的:
#include
using namespace std;
class X;
class Y
{public:
void g(X*);
};
class X
{
private: int i;
public:
X(int a){i=a;}
friend void h(X*);
friend void Y::g(X*);
friend class Z;
void print(){
cout<< i<<endl;
}
};
void h(Xx) {x->i+=10;}
void Y::g(Xx) {x->i++;}
class Z
{
public:
void f(X*x) {x->i+=5;}
};
int main()
{
int a;
cin>>a;
X x(a);
Y y;
Z z;
y.g(&x);
z.f(&x);
h(&x);
x.print();
}
5.2(习题5-14)定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数getTotalWeight(),计算二者的重量和。
参考的输入(数字前为提示文字):
Input boat weight:3
Input car weight:5
参考的输出:
Total weight:8
参考代码:
#include
using namespace std;
class Car;
class Boat
{public:
Boat()
{
cout<<“Input boat weight:”;
cin>>weight;
}
friend int getTotalWeight(Boat &b,Car &c);
private:
int weight;
};
class Car
{
public:
Car()
{
cout<<“Input car weight:”;
cin>>weight;
}
friend int getTotalWeight(Boat &b,Car &c);
private:
int weight;
};
int getTotalWeight(Boat &b,Car &c)
{
return b.weight+c.weight;
}
int main()
{
Boat b;
Car c;
cout<<“Total weight:”<<getTotalWeight(b,c);
}
我的:
#include
#include
using namespace std;
class Car;
class Boat
{
public:
Boat(){
cout<<“Input boat weight:”;
float b;
cin>>b;
weight = b;}
friend void getTotalWeight(Boat ,Car);
private: float weight;
};
class Car
{
public:
Car(){
cout<<“Input car weight:”;
float c;
cin>>c;
weight = c;
}
friend void getTotalWeight(Boat ,Car );
private:
float weight;
};
void getTotalWeight(Boat b,Car c)
{
float g;
g=b.weight+c.weight;
cout <<“Total weight:”<< g<<endl;
}
int main()
{
Boat b;
Car c;
getTotalWeight(b,c);
}