重载方法(Overloading)
就像函数重载,类方法也可以被重载--包括构造方法。
#include <string>
#include <iostream>
using namespace std ;
class Dog
{
int age, weight ;
string color ;
public:
// Add an overloaded method.
void bark( string noise ) { cout << noise << endl ; }
void bark() { cout << "WOOF!" << endl ; }
// Constructor for no arguments.
Dog() ;
// Add overloaded constructor declaration for two arguments.
Dog( int, int ) ;
// Add overloaded constructor declaration for three arguments.
Dog( int, int, string ) ;
// Destructor declaration.
~Dog() ;
int getAge() { return age; }
int getWeight() { return weight; }
string getColor() { return color; }
} ;
// Constructor definition (the default).
Dog::Dog()
{
age = 1 ;
weight = 2 ;
color = "black" ;
}
// Overloaded constructor definition (two args).
Dog::Dog( int age, int weight )
{
this -> age = age ;
this -> weight = weight ;
color = "white" ;
}
// Overloaded constructor definition (three args).
Dog::Dog( int age, int weight, string color )
{
this -> age = age ;
this -> weight = weight ;
this -> color = color ;
}
// Destructor definition.
Dog::~Dog()
{
cout << "Object destroyed." << endl ;
}
int main()
{
Dog fido( 3, 15, "brown" ) ;
cout << "Fido is a " << fido.getColor() << " dog" << endl ;
cout << "Fido is " << fido.getAge() << " years old" << endl ;
cout << "Fido weighs " << fido.getWeight() << " pounds" << endl ;
fido.bark() ;
Dog pooch ( 4, 18, "gray" ) ;
cout << "Pooch is a " << pooch.getAge() ;
cout << " year old " << pooch.getColor() ;
cout << " dog who weighs " << pooch.getWeight() ;
cout << " pounds." ;
pooch.bark() ;
// New Dog object rex.
Dog rex ;
cout << "Rex is a " << rex.getAge() ;
cout << " year old " << rex.getColor() ;
cout << " dog who weighs " << rex.getWeight() ;
cout << " pounds." ;
rex.bark( "GRRR!" ) ;
Dog sammy( 2, 6 ) ;
cout << "Sammy is a " << sammy.getAge() ;
cout << " year old " << sammy.getColor() ;
cout << " dog who weighs " << sammy.getWeight() ;
cout << " pounds." ;
sammy.bark( "BOWOW!" ) ;
return 0 ;
}
继承类的属性
派生类除了自己的成员外,还继承它所派生的父(基)类的成员。
继承基类成员的能力允许创建共享某些共同属性的派生类,这些属性已在基类中定义。例如,一个 "多边形 "基类可以定义所有多边形共有的宽度和高度属性。矩形 "和 "三角形 "类可以从 "多边形 "类中派生出来--继承宽度和高度属性,此外还有定义其独特特征的自身成员。
派生类的声明在其类名后面加上冒号:,然后是访问符和它所派生的类。
#include <iostream>
using namespace std ;
class Polygon
{
protected:
int width, height ;
public:
void setValues( int w, int h ) { width = w ; height = h ; }
} ;
class Rectangle: public Polygon
{
public:
int area() { return ( width * height ) ; }
} ;
class Triangle: public Polygon
{
public:
int area() { return ( ( width * height ) / 2 ) ; }
} ;
int main()
{
Rectangle rect ; rect.setValues( 4, 5 ) ;
Triangle trgl ; trgl.setValues( 4, 5 ) ;
cout << "Rectangle area : " << rect.area() << endl ;
cout << "Triangle area : " << trgl.area() << endl ;
return 0 ;
}
可以继承多个类。例如class Box : public A, public B, public C { } ;
调用基类构造函数
尽管派生类继承了其父基类的成员,但它们并不继承其构造函数和析构函数。当派生类的新对象被创建时,基类的默认构造函数总是被调用,而当对象被销毁时,基类的析构函数被调用。这些调用是在派生类的构造器和析构器方法的调用之外进行的。
基类的默认构造函数没有参数,但该类也可能有重载的构造函数。如果你喜欢在创建派生类的新对象时调用基类的重载构造函数,你可以在派生类中创建匹配的重载构造函数--拥有相同数量和类型的参数。
#include <iostream>
using namespace std ;
class Parent
{
public:
Parent() { cout << "Default Parent constructor called." ; }
Parent(int a) { cout << endl << "Overloaded Parent constructor called." ; }
} ;
class Daughter: public Parent
{
public:
Daughter() { cout << endl << " Derived Daughter class default constructor called." ; }
} ;
class Son: public Parent
{
public:
Son(int a ) : Parent(a) {cout << endl << " Derived Son class overloaded constructor called." << endl ; }
} ;
int main()
{
Daughter emma ;
Son andrew(0) ;
return 0 ;
}
重写基类方法
可以在派生类中声明覆盖基类中的匹配方法--如果两个方法声明的名称、参数和返回类型都相同的话。这就有效地隐藏了基类方法,因为它变得不可访问,除非它被明确地调用。
#include <string>
#include <iostream>
using namespace std ;
class Man
{
public :
void speak() { cout << "Hello!" << endl ; }
void speak( string msg ) { cout << " " << msg << endl ; }
} ;
class Hombre : public Man
{
public :
void speak( string msg ) { cout << msg << endl ; }
} ;
int main()
{
Man henry ;
Hombre enrique ;
henry.speak() ;
henry.speak( "It's a beautiful evening." ) ;
enrique.speak( "Hola!" ) ;
enrique.Man::speak( "Es una tarde hermosa." ) ;
return 0 ;
}
在派生类中声明的覆盖方法隐藏了基类中的两个重载类。试着调用enrique.speak()--编译器会抱怨没有匹配的方法来调用。
小结
- 面向对象编程的第一个原则是将数据和功能封装在类中。
- 访问符public、private和protected控制着类外成员的可访问性。
- 类的声明描述了数据结构,基于类可以创建实例对象。
- 公有的setter和getter类方法存储和检索私有类变量成员的数据。
- 范围解析操作符::可以明确地识别一个类。
- 与传递的参数同名的类成员可以通过this->指针来明确识别。
- 对象被创建时,会调用构造器方法,当它被销毁时,会调用析构器方法。
- 类的变量可以由构造函数自动初始化。
- 类方法可以像其他函数一样重载。
- 面向对象编程的第二个原则是继承,允许派生类继承其父基类的属性。
- 在派生类的声明中,类的名称后面有冒号,访问符,以及它的基类名称。
- 派生类的实例对象被创建时,除了派生类的构造函数外,基类的默认构造函数也会被调用。
- 派生类的方法可以覆盖其基类中的匹配方法--也可以覆盖基类中该名称的所有重载方法。
网友评论