....FCC布置的一些CPP作业..
<<面向对象程序设计与++语言>> 第二版.
朱战立
<<面向对象程序设计与++语言>> 第二版.
朱战立
ZZ.7.8
Jun 15, 2007
//7.8
//设计一个分数类模板
// 要求:
// 1.分数类中包含的分数运算有2个分数的加减乘除运算..
// 2.分数的输出格式是分子/分母.
// 3.编写一个测试程序来测试..
|ZZ| c++ 5.17 覆盖...
May 18, 2007
..今天刚做的...谢谢fcc老师的帮忙....
//5.17
//先设计一个长方形类.再通过继承长方形类设计一个正方形类,
//正方形类中通过覆盖父类的成员函数得到一些新的功能..
#include<iostream.h>
class Rectangle //长方形类.
{
public:
int x,y;
int area()
{
return x*y;
}
};
class square:public Rectangle //派生类...正方形类..
{
public:
int area() //覆盖长方形类中的 area.
{
return x*x;
}
square(int a) //构造函数.
{
x=a;
}
};
void main()
{
square f(4);
cout<<f.area()<<endl;
}
//先设计一个长方形类.再通过继承长方形类设计一个正方形类,
//正方形类中通过覆盖父类的成员函数得到一些新的功能..
#include<iostream.h>
class Rectangle //长方形类.
{
public:
int x,y;
int area()
{
return x*y;
}
};
class square:public Rectangle //派生类...正方形类..
{
public:
int area() //覆盖长方形类中的 area.
{
return x*x;
}
square(int a) //构造函数.
{
x=a;
}
};
void main()
{
square f(4);
cout<<f.area()<<endl;
}
------------------------------------07.05.18
|ZZ| c++ 第一次作业..
May 16, 2007
这道题是上机作业...第一堂课就布置的...当时做的有个错误但是老师帮忙改了..没想到昨晚再写..就发现自己还犯当时的那个错误..真是够傻的..不举一反三也就算了..做过的竟然还.....
再谢谢..帮忙帮我改的人.....此题依然仅供参考.....若发现错误..请帮忙指出...在此感谢..
//编写一个重载函数max,分别求两个整数或3个整数的最大值。
//形参个数不同。
#include<iostream.h>
int max(int x,int y) //函数1.
{
if(x>y) return x;
else return y;
}
int max(int x,int y,int z) //函数2.
{
if(x>y&&x>z) return x;
else if(x>y&&x<=z) return z;
else if(x<=y&&y>z) return y;
else return z;
}
void main()
{
int x,y,z,t1,t2;
cout<<"input x,y,z"<<endl;
cin>>x>>y>>z;
t1=max(x,y); //调用函数1.
cout<<"t1="<<t1<<endl;
t2=max(x,y,z); //调用函数2.
cout<<"t2="<<t2<<endl;
}
//形参个数不同。
#include<iostream.h>
int max(int x,int y) //函数1.
{
if(x>y) return x;
else return y;
}
int max(int x,int y,int z) //函数2.
{
if(x>y&&x>z) return x;
else if(x>y&&x<=z) return z;
else if(x<=y&&y>z) return y;
else return z;
}
void main()
{
int x,y,z,t1,t2;
cout<<"input x,y,z"<<endl;
cin>>x>>y>>z;
t1=max(x,y); //调用函数1.
cout<<"t1="<<t1<<endl;
t2=max(x,y,z); //调用函数2.
cout<<"t2="<<t2<<endl;
}
|ZZ| C++ .P151. 5.15
May 14, 2007
..
难得自己做出一道题。。真是超级兴奋了。。。
但是.因为这道题是自己做出来的。。。。所以。。可能有些毛病。。但是编译结果都是可以出来的。。。也没 错误。。。。。有的只是过程写的不好。。或者。可能有些没按要求做 再或者。某一些奇怪的问题。。。。。
。。 好了 问题交代完毕。此题仅供参考。。正文如下。。
//5.15
//设计一个点类,它仅包含2个属性:横坐标和纵坐标。通过继承点类再设计
//一个圆类,除了有一个圆心外,还有半径,还应该能够计算圆的周厂和面积等。
//编写一个测试程序来测试所设计的类能否实现预定的功能。
#include<iostream.h>
class point
{
public:
int x;
int y;
point(int iniX=0, int iniY=0):
x(iniX),y(iniY)
{}
private:
};
class circle:public point //派生类。
{
int Radius;
public :
double GetArea()
{
return 3.14*Radius*Radius;
}
double GetCh()
{
return 2*3.14*Radius;
}
circle(int x,int y,int r)
{
Radius=r;
}
};
#include<iostream.h>
#include<math.h>
void main()
{
circle f(2,3,4);
cout<<f.GetArea()<<endl;
cout<<f.GetCh()<<endl;
}
//设计一个点类,它仅包含2个属性:横坐标和纵坐标。通过继承点类再设计
//一个圆类,除了有一个圆心外,还有半径,还应该能够计算圆的周厂和面积等。
//编写一个测试程序来测试所设计的类能否实现预定的功能。
#include<iostream.h>
class point
{
public:
int x;
int y;
point(int iniX=0, int iniY=0):
x(iniX),y(iniY)
{}
private:
};
class circle:public point //派生类。
{
int Radius;
public :
double GetArea()
{
return 3.14*Radius*Radius;
}
double GetCh()
{
return 2*3.14*Radius;
}
circle(int x,int y,int r)
{
Radius=r;
}
};
#include<iostream.h>
#include<math.h>
void main()
{
circle f(2,3,4);
cout<<f.GetArea()<<endl;
cout<<f.GetCh()<<endl;
}
------------------------------------07。05。14 23:21
|ZZ|c++ .长方形类.方法2.P91. 3.32
May 14, 2007
ZZ谢谢..一些人的帮忙.....相当感谢...
面向对象程序设计与c++语言(第2版)
P91. 3.32
#include<iostream.h>
class Rectangle
{
int c,k;
public:
int GetArea()
{
return c*k;
}
int GetCh()
{
return 2*(c+k);
}
Rectangle (int a,int b):c(a),k(b)
{
}
};
void main()
{
Rectangle f(4,5);
cout<<f.GetArea()<<endl;
cout<<f.GetCh()<<endl;
}
class Rectangle
{
int c,k;
public:
int GetArea()
{
return c*k;
}
int GetCh()
{
return 2*(c+k);
}
Rectangle (int a,int b):c(a),k(b)
{
}
};
void main()
{
Rectangle f(4,5);
cout<<f.GetArea()<<endl;
cout<<f.GetCh()<<endl;
}




