实现重载函数Double(x),返回值为输入参数的两倍;参数分别为整型、长整型、浮点型、双精度型,返回值类型与参数一样。
解:
源程序:
#include <iostream.h>
int Double(int);
long Double(long);
float Double(float);
double Double(double);
int main()
{
int myInt = 6500;
long myLong = 65000;
float myFloat = 6.5F;
double myDouble = 6.5e20;
int doubledInt;
long doubledLong;
float doubledFloat;
double doubledDouble;
cout << "myInt: " << myInt << "\n";
cout << "myLong: " << myLong << "\n";
cout << "myFloat: " << myFloat << "\n";
cout << "myDouble: " << myDouble << "\n";
doubledInt = Double(myInt);
doubledLong = Double(myLong);
doubledFloat = Double(myFloat);
doubledDouble = Double(myDouble);
cout << "doubledInt: " << doubledInt << "\n";
cout << "doubledLong: " << doubledLong << "\n";
cout << "doubledFloat: " << doubledFloat << "\n";
cout << "doubledDouble: " << doubledDouble << "\n";
return 0;
}
int Double(int original)
{
cout << "In Double(int)\n";
return 2 * original;
}
long Double(long original)
{
cout << "In Double(long)\n";
return 2 * original;
}
float Double(float original)
{
cout << "In Double(float)\n";
return 2 * original;
}
double Double(double original)
{
cout << "In Double(double)\n";
return 2 * original;
}
程序运行输出:
myInt: 6500
myLong: 65000
myFloat: 6.5
myDouble: 6.5e+20
In Double(int)
In Double(long)
In Double(float)
In Double(double)
DoubledInt: 13000
DoubledLong: 130000
DoubledFloat: 13
DoubledDouble: 1.3e+21
定义一个Rectangle类,有长itsWidth、宽itsLength等属性,重载其构造函数Rectangle()和Rectangle(int width, int length)。
解:
源程序:
#include <iostream.h>
class Rectangle
{
public:
Rectangle();
Rectangle(int width, int length);
~Rectangle() {}
int GetWidth() const { return itsWidth; }
int GetLength() const { return itsLength; }
private:
int itsWidth;
int itsLength;
};
Rectangle::Rectangle()
{
itsWidth = 5;
itsLength = 10;
}
Rectangle::Rectangle (int width, int length)
{
itsWidth = width;
itsLength = length;
}
int main()
{
Rectangle Rect1;
cout << "Rect1 width: " << Rect1.GetWidth() << endl;
cout << "Rect1 length: " << Rect1.GetLength() << endl;
int aWidth, aLength;
cout << "Enter a width: ";
cin >> aWidth;
cout << "\nEnter a length: ";
cin >> aLength;
Rectangle Rect2(aWidth, aLength);
cout << "\nRect2 width: " << Rect2.GetWidth() << endl;
cout << "Rect2 length: " << Rect2.GetLength() << endl;
return 0;
}
程序运行输出:
Rect1 width: 5
Rect1 length: 10
Enter a width: 20
Enter a length: 50
Rect2 width: 20
Rect2 length: 50
定义计数器Counter类,对其重载运算符 + 。
解:
源程序:
typedef unsigned short USHORT;
#include <iostream.h>
class Counter
{
public:
Counter();
Counter(USHORT initialValue);
~Counter(){}
USHORT GetItsVal()const { return itsVal; }
void SetItsVal(USHORT x) {itsVal = x; }
Counter operator+ (const Counter &);
private:
USHORT itsVal;
};
Counter::Counter(USHORT initialValue):
itsVal(initialValue)
{
}
Counter::Counter():
itsVal(0)
{
}
Counter Counter::operator+ (const Counter & rhs)
{
return Counter(itsVal + rhs.GetItsVal());
}
int main()
{
Counter varOne(2), varTwo(4), varThree;
varThree = varOne + varTwo;
cout << "varOne: " << varOne.GetItsVal()<< endl;
cout << "varTwo: " << varTwo.GetItsVal() << endl;
cout << "varThree: " << varThree.GetItsVal() << endl;
return 0;
}
程序运行输出:
varOne: 2
varTwo: 4
varThree: 6
定义一个哺乳动物Mammal类,再由此派生出狗Dog类,二者都定义 Speak()成员函数,基类中定义为虚函数,定义一个Dog类的对象,调用Speak函数,观察运行结果。
解:
源程序:
#include <iostream.h>
class Mammal
{
public:
Mammal():itsAge(1) { cout << "Mammal constructor...\n"; }
~Mammal() { cout << "Mammal destructor...\n"; }
virtual void Speak() const { cout << "Mammal speak!\n"; }
};
class Dog : public Mammal
{
public:
Dog() { cout << "Dog Constructor...\n"; }
~Dog() { cout << "Dog destructor...\n"; }
void Speak()const { cout << "Woof!\n"; }
};
int main()
{
Mammal *pDog = new Dog;
pDog->Speak();
return 0;
}
程序运行输出:
Mammal constructor...
Dog constructor...
Woof!
Dog destructor...
Mammal destructor...
定义一个Shape抽象类,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积,GetPerim()函数计算对象的周长。
解:
源程序:
#include <iostream.h>
class Shape
{
public:
Shape(){}
~Shape(){}
virtual float GetArea() =0 ;
virtual float GetPerim () =0 ;
};
class Circle : public Shape
{
public:
Circle(float radius):itsRadius(radius){}
~Circle(){}
float GetArea() { return 3.14 * itsRadius * itsRadius; }
float GetPerim () { return 6.28 * itsRadius; }
private:
float itsRadius;
};
class Rectangle : public Shape
{
public:
Rectangle(float len, float width): itsLength(len), itsWidth(width){};
~Rectangle(){};
virtual float GetArea() { return itsLength * itsWidth; }
float GetPerim () { return 2 * itsLength + 2 * itsWidth; }
virtual float GetLength() { return itsLength; }
virtual float GetWidth() { return itsWidth; }
private:
float itsWidth;
float itsLength;
};
void main()
{
Shape * sp;
sp = new Circle(5);
cout << "The area of the Circle is " << sp->GetArea () << endl;
cout << "The perimeter of the Circle is " << sp->GetPerim () << endl;
delete sp;
sp = new Rectangle(4,6);
cout << "The area of the Rectangle is " << sp->GetArea() << endl;
cout << "The perimeter of the Rectangle is " << sp->GetPerim () << endl;
delete sp;
}
程序运行输出:
The area of the Circle is 78.5
The perimeter of the Circle is 31.4
The area of the Rectangle is 24
The perimeter of the Rectangle is 20
对Point类重载++(自增)、--(自减)运算符
解:
#include <iostream.h>
class Point
{
public:
Point& operator++();
Point operator++(int);
Point& operator--();
Point operator--(int);
Point() { _x = _y = 0; }
int x() { return _x; }
int y() { return _y; }
private:
int _x, _y;
};
Point& Point::operator++()
{
_x++;
_y++;
return *this;
}
Point Point::operator++(int)
{
Point temp = *this;
++*this;
return temp;
}
Point& Point::operator--()
{
_x--;
_y--;
return *this;
}
Point Point::operator--(int)
{
Point temp = *this;
--*this;
return temp;
}
void main()
{
Point A;
cout << "A的值为:" << A.x() << " , " << A.y() << endl;
A++;
cout << "A的值为:" << A.x() << " , " << A.y() << endl;
++A;
cout << "A的值为:" << A.x() << " , " << A.y() << endl;
A--;
cout << "A的值为:" << A.x() << " , " << A.y() << endl;
--A;
cout << "A的值为:" << A.x() << " , " << A.y() << endl;
}
程序运行输出:
A的值为:0 , 0
A的值为:1 , 1
A的值为:2 , 2
A的值为:1 , 1
A的值为:0 , 0
定义一个基类BaseClass,从它派生出类DerivedClass,BaseClass有成员函数fn1()、fn2(),fn1()是虚函数,DerivedClass也有成员函数fn1()、fn2(),在主程序中定义一个DerivedClass的对象,分别用BaseClass和DerivedClass的指针来调用fn1()、fn2(),观察运行结果。
解:
#include <iostream.h>
class BaseClass
{
public:
virtual void fn1();
void fn2();
};
void BaseClass::fn1()
{
cout << "调用基类的虚函数fn1()" << endl;
}
void BaseClass::fn2()
{
cout << "调用基类的非虚函数fn2()" << endl;
}
class DerivedClass : public BaseClass
{
public:
void fn1();
void fn2();
};
void DerivedClass::fn1()
{
cout << "调用派生类的函数fn1()" << endl;
}
void DerivedClass::fn2()
{
cout << "调用派生类的函数fn2()" << endl;
}
void main()
{
DerivedClass aDerivedClass;
DerivedClass *pDerivedClass = &aDerivedClass;
BaseClass *pBaseClass = &aDerivedClass;
pBaseClass->fn1();
pBaseClass->fn2();
pDerivedClass->fn1();
pDerivedClass->fn2();
}
程序运行输出:
调用派生类的函数fn1()
调用基类的非虚函数fn2()
调用派生类的函数fn1()
调用派生类的函数fn2()