简单的C++题.docx
《简单的C++题.docx》由会员分享,可在线阅读,更多相关《简单的C++题.docx(24页珍藏版)》请在冰点文库上搜索。
简单的C++题
/*定义一个Dog类,包含age,weight等属性,以及对这些属性操作的方法。
实现并测试这个类*/
#include
usingnamespacestd;
classDog
{
public:
Dog(inta,intb)
{
age=a;
weight=b;
}
~Dog(){}
intgetAge()
{
returnage;
}
intgetWeight()
{
returnweight;
}
voidsetAge(intage)
{
this->age=age;
}
voidsetWeight(intweight)
{
this->weight=weight;
}
private:
intage,weight;
};
intmain()
{
Dogjack(2,10);
cout<<"jackisaDogwhois";
cout<jack.setAge(7);
jack.setWeight(20);
cout<<"Nowjackis";
cout<return0;
}
/*定义一个Circle类,有数据成员radius(半径)成员函数getArea(),计算圆的面积,构造一个
Circle的对象进行测试。
*/
#include
usingnamespacestd;
classCircle
{
public:
Circle(floatradius)
{
this->radius=radius;
}
~Circle(){}
floatgetArea()
{
return3.14*radius*radius;
}
private:
floatradius;
};
intmain()
{
floatradius;
cout<<"请输入圆的半径:
";
cin>>radius;
Circlep(radius);
cout<<"半径为"<"<
return0;
}
/*定义一个Tree(树)类,有成员ages(树龄),
成员函数grow(intyears)对ages加上years,
age()显示tree对象的ages值。
*/
#include
usingnamespacestd;
classTree
{
public:
Tree(intages=0)
{
this->ages=ages;
}
~Tree(){show();}
voidgrow(intyears)
{
ages+=years;
}
voidshow()
{
cout<<"树的年龄为:
"<}
private:
intages;
};
intmain()
{
Treee1(10);
e1.show();
e1.grow(5);
return0;
}
/*定义一个复数类Complex,使得下面的代码能够工作
Complexc1(3,5);//用复数3+5i初始化c1
Complexc2=4.5;//用实数4.5初始化c2
c1.add(c2);//将c1与c2想加,结果保存在c1中
c1.show();//将c1输出(这时的结果应该是7.5+5i)*/
#include
usingnamespacestd;
classComplex
{
public:
Complex(doubler,doublei)
{
real=r;
image=i;
}
Complex(doubler)
{
real=r;
image=0;
}
voidshow()
{
cout<cout<cout<}
voidadd(Complexc2)
{
real+=c2.real;
image+=c2.image;
}
private:
doublereal;doubleimage;
};
intmain()
{
Complexc1(3,5);
Complexc2(4.5);
c1.show();
c1.add(c2);
c1.show();
return0;
}
/*定义一个Cat类,拥有静态数据成员numOfCats记录Cat的个体数目,静态成员函数getNumOfCats()存取numOfCats,设计程序测试这个类,体会静态数据成员和静态成员函数的用法。
*/
#include
usingnamespacestd;
classCat
{
public:
Cat(intage)
{itsAge=age;numOfCats++;}
virtual~Cat()
{numOfCats--;}
virtualintgetAge()
{returnitsAge;}
virtualvoidsetAge(intage)
{itsAge=age;}
staticintgetNumOfCats()
{returnnumOfCats;}
private:
intitsAge;
staticintnumOfCats;
};
intCat:
:
numOfCats=0;
voidtelepathicFunction()
{
cout<<"Thereare"<:
getNumOfCats()<<"catsalive!
\n";
}
intmain()
{
constintmaxCats=5;
Cat*catHouse[maxCats];
inti;
for(i=0;i{
catHouse[i]=newCat(i);
telepathicFunction();
}
for(i=0;i{
deletecatHouse[i];
telepathicFunction();
}
return0;
}
/*
在函数fn1()中定义一个静态变量n
fn1()中对n的值加1,在主函数中,
调用fn1()10次,显示n的值。
*/
#include
usingnamespacestd;
voidfn1()
{
staticintn=0;
n++;
cout<<"n的值为:
"<}
intmain()
{
inti;
for(i=0;i<10;i++)
fn1();
return0;
}
/*实现一个名为SimpleCircle的简单圆类
其数据成员int*itsRadius为一个指向其半径的指针
设计对数据成员的各种操作,给出这个类完整的实现
并且测试这个类
*/
#include
usingnamespacestd;
classSimpleCircle
{
public:
SimpleCircle()
{
itsRadius=newint(5);
}
SimpleCircle(intradius)
{
itsRadius=newint(radius);
}
SimpleCircle(constSimpleCircle&rhs)
{
intval=rhs.getRadius();
itsRadius=newint(val);
}
~SimpleCircle(){}
intgetRadius()const
{
return*itsRadius;
}
private:
int*itsRadius;
};
intmain()
{
SimpleCircleCircleOne,CircleTwo(9);
cout<<"CircleOne:
"<cout<<"CircleTwo:
"<return0;
}
/*定义一个Employee类,其中包括鞭尸姓名、地址城市、和邮编等属性,包括setName()和display()等函数。
display()使用cout语句显示姓名、地址、城市和邮编等属性。
函数setName()改变对象的姓名属性,实现并测试这个类。
*/
#include
#include
usingnamespacestd;
classEmployee
{
private:
charname[30];
charstreet[30];
charcity[15];
charzip[15];
public:
Employee(char*n,char*s,char*c,char*z)
{
strcpy(name,n);
strcpy(street,s);
strcpy(city,c);
strcpy(zip,z);
}
voidsetName(char*n)
{
strcpy(name,n);
}
voiddisplay()
{
cout<cout<}
};
intmain()
{
Employeee1("张三","平安大街","北京","100000");
e1.display();
e1.setName("李四");
e1.display();
cout<return0;
}
/*定义一个Shape,在此基础上派生出Rectangle和Circle,二者都有getArea()函数计算对象的面积。
使用Rectangle类创建一个派生类Square。
*/
#include
usingnamespacestd;
classShape
{
public:
Shape(){}
~Shape(){}
virtualfloatgetArea(){return-1;}
};
classCircle:
publicShape
{
public:
Circle(floatradius):
itsRadius(radius){}
~Circle(){}
floatgetArea(){return3.14*itsRadius*itsRadius;}
private:
floatitsRadius;
};
classRectangle:
publicShape
{
public:
Rectangle(floatlen,floatwidth):
itsLength(len),itsWidth(width){};
~Rectangle(){};
virtualfloatgetArea(){returnitsLength*itsWidth;}
virtualfloatgetLength(){returnitsLength;}
virtualfloatgetWidth(){returnitsWidth;}
private:
floatitsLength;
floatitsWidth;
};
classSquare:
publicRectangle
{
public:
Square(floatlen);
~Square(){};
};
Square:
:
Square(floatlen):
Rectangle(len,len)
{
}
intmain()
{
Shape*sp;
sp=newCircle(5);
cout<<"TheareaoftheCircleis"<getArea()<deletesp;
sp=newRectangle(4,6);
cout<<"TheareaoftheRctangleis"<getArea()<deletesp;
sp=newSquare(5);
cout<<"TheareaoftheSquareis"<getArea()<deletesp;
return0;
}
/*
定义一个哺乳动物类Mammal,再由此派生出Dog类,定义一个Dog类的对象,观察与派生类的构造函数的调用顺序
Manmalconstructor...
Dogconstructor...
Mammalsound!
Tailwagging...
jackis1yearold
Dogdestructor...
Mammaldestructor...*/
#include
usingnamespacestd;
enummyColor{
BLACK,WHITE
};
classMammal{
public:
//constructors
Mammal();
~Mammal();
//accesasors
intgetArea()const{
returnitsAge;
}
voidsetAge(intage){
itsAge=age;
}
intgetWeight()const{
returnitsWeight;
}
voidsetWeight(intweight){
itsWeight=weight;
}
//Othermenthods
voidspeak()const{
cout<<"Mammalsound!
\n";
}
protected:
intitsAge;
intitsWeight;
};
classDog:
publicMammal{
public:
Dog();
~Dog();
myColorgetColor()const{
returnitsColor;
}
voidsetColor(myColorcolor){
itsColor=color;
}
voidwagTail(){
cout<<"Tailwagging...\n";
}
private:
myColoritsColor;
};
Mammal:
:
Mammal():
itsAge
(1),itsWeight(5){
cout<<"Mammalconstructor...\n";
}
Mammal:
:
~Mammal(){
cout<<"Mammaldestructor...\n";
}
Dog:
:
Dog():
itsColor(WHITE){
cout<<"Dogconstructor...\n";
}
Dog:
:
~Dog(){
cout<<"Dogdestructor...\n";
}
intmain(){
Dogjack;
jack.speak();
jack.wagTail();
cout<<"jackis"<return0;
}
/*编写一个哺乳动物类Mammal,再由此派生出狗类Dog,
二者都声明speak()成员函数,该函数在基类中被声明为虚函数
,声明一个Dog类的对象,通过此对象调用speak函数,观察运行结果。
*/
#include
usingnamespacestd;
classMammal
{
public:
Mammal(){
cout<<"Mammalconstructor...\n";
}
virtual~Mammal(){
cout<<"Mammaldestructor...\n";
}
virtualvoidspeak()const{
cout<<"Mammalspeak!
\n";
}
};
classDog:
publicMammal{
public:
Dog(){
cout<<"DogConstructor...\n";
}
~Dog(){
cout<<"Dogdestructor...\n";
}
voidspeak()const{
cout<<"Woof!
\n";
}
};
intmain(){
Mammal*pDog=newDog;
pDog->speak();
deletepDog;
return0;
}
/*请编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,
二者都有计算对象面积的函数getArea(),计算对象周长的函数getPerim()。
*/
#include
usingnamespacestd;
classShape
{
public:
Shape(){}
~Shape(){}
virtualfloatgetArea()=0;
virtualfloatgetPerim()=0;
};
classCircle:
publicShape
{
public:
Circle(floatradius):
itsRadius(radius){};
~Circle(){}
floatgetArea(){return3.14*itsRadius*itsRadius;}
floatgetPerim(){return6.28*itsRadius;}
private:
floatitsRadius;
};
classRectangle:
publicShape
{
public:
Rectangle(floatlen,floatwidth):
itsLength(len),itsWidth(width){};
~Rectangle(){};
virtualfloatgetArea(){returnitsLength*itsWidth;}
floatgetPerim(){return2*itsLength+2*itsWidth;}
virtualfloatGetLength(){returnitsLength;}
virtualfloatGetWidth(){returnitsWidth;}
private:
floatitsLength;
floatitsWidth;
};
intmain()
{
Shape*sp;
sp=newCircle(5);
cout<<"TheareaoftheCircleis"<getArea()<cout<<"ThePerimeteroftheCircleis"<getPerim()<deletesp;
sp=newRectangle(4,6);
cout<<"TheareaoftheRectangleis"<getArea()<cout<<"TheperimteroftheRectangleis"<getPerim()<deletesp;
return0;
}
/*对类Point重载++(自增)、--(自减)运算符,要求同时重载前缀和后缀的形式。
*/
#include
usingnamespacestd;
classPoint
{
public:
Point&operator++();
Pointoperator++(int);
Point&operator--();
Pointoperator--(int);
Point(){_x=_y=0;}
intx(){return_x;}
inty(){return_y;}
private:
int_x,_y;
};
Point&Point:
:
operator++()
{
_x++;
_y++;
return*this;
}
PointPoint:
:
operator++(int)
{
Pointtemp=*this;
++*this;
returntemp;
}
Point&Point:
:
operator--()
{
_x--;
_y--;
return*this;
}
PointPoint:
:
operator--(int)
{
Pointtemp=*this;
--*this;
returntemp;
}
intmain()
{
Pointa;
cout<<"a的值为:
"<a++;
cout<<"a的值为:
"<++a;
cout<<"a的值为:
"<a--;
cout<<"a的值为:
"<--a;
cout<<"a的值为:
"<return0;
}
/*使用I/O流以文本方式建立一个文件test1.txt,
写入字符“已成功写入文件!
”,用其他字处理程序
(例如Windows的记事本程序Notepad)打开,看看是否正确写入。
*/
#include
usingnamespacestd;
intmain()
{
o