c++快速入门7:类和对象B:重载、继承和重写

重载方法(Overloading)

就像函数重载,类方法也可以被重载--包括构造方法。

c++快速入门7:类和对象B:重载、继承和重写

#include 
#include 
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 ;
}
c++快速入门7:类和对象B:重载、继承和重写

继承类的属性

派生类除了自己的成员外,还继承它所派生的父(基)类的成员。
继承基类成员的能力允许创建共享某些共同属性的派生类,这些属性已在基类中定义。例如,一个 "多边形 "基类可以定义所有多边形共有的宽度和高度属性。矩形 "和 "三角形 "类可以从 "多边形 "类中派生出来--继承宽度和高度属性,此外还有定义其独特特征的自身成员。

c++快速入门7:类和对象B:重载、继承和重写

派生类的声明在其类名后面加上冒号:,然后是访问符和它所派生的类。

#include 
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 
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 
#include 
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()--编译器会抱怨没有匹配的方法来调用。

小结

c++快速入门7:类和对象B:重载、继承和重写

展开阅读全文

页面更新:2024-04-01

标签:对象   冒号   重写   初始化   指针   变量   函数   实例   属性   入门   声明   名称   快速   方法   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top