C++/Class/Inheritance

Материал из C\C++ эксперт
Версия от 10:25, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Access control under inheritance

  
#include <iostream>
using namespace std;
class Enemy
{
public:
    Enemy(): m_Damage(10) {} 
    
    void Attack() const
    { cout << "Attack inflicts " << m_Damage << " damage points!\n"; }  
protected:
    int m_Damage;
};
class Boss : public Enemy
{
public:
    Boss(): m_DamageMultiplier(3) {}
        
    void SpecialAttack() const
    { cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); 
      cout << " damage points!\n"; }
private:
    int m_DamageMultiplier;
};
int main()
{ 
    Enemy enemy1;
    enemy1.Attack();
    Boss boss1;
    boss1.Attack();
    boss1.SpecialAttack();
    return 0;
}


A simple example of inheritance.

  
#include <iostream>
using namespace std;
class BaseClass {
  int i;
public:
  void setInt(int n);
  int getInt();
};
class DerivedClass : public BaseClass {
  int j;
public:
  void setJ(int n);
  int mul();
};
void BaseClass::setInt(int n)
{
  i = n;
}
int BaseClass::getInt()
{
  return i;
}
void DerivedClass::setJ(int n)
{
  j = n;
}
int DerivedClass::mul()
{
  return j * getInt();
}
int main()
{
  DerivedClass ob;
  ob.setInt(10);        // load i in BaseClass
  ob.setJ(4);          // load j in DerivedClass
  cout << ob.mul();     // displays 40
  return 0;
}


call contructor from parent class

   
#include <iostream>
#include <string.h>
using namespace std;
class Book 
{
  public:
    Book(char *title) { strcpy(Book::title, title); }; 
    void show_title(void) { cout << title << endl; };
  protected:
    float cost;
    void show_cost(void) { cout << cost << endl; };
  private:
    char title[64];
};
class LibraryCard : public Book 
{
  public:
    LibraryCard(char *title, char *author, char *publisher) : Book(title) 
      { 
        strcpy(LibraryCard::author, author); 
        strcpy(LibraryCard::publisher, publisher); 
        cost = 49.95;
      };
    void show_library(void) 
  { 
        show_title();
        show_cost();
        cout << author << " " << publisher; 
  };
  private:
    char author[64];
    char publisher[64];
};

int main(void)
{
   LibraryCard card("A", "B", "C");
   card.show_library();
}


Call parent constructor and pass in parameter

  
#include <iostream>
using namespace std;
class base 
{
 protected:
   int i;
 public:
   base(int x) 
   {
     i=x;
     cout << "Constructing base.\n";
   }
   ~base(void) {cout << "Destructing base.\n";}
 };
class derived : public base 
{
   int j;
 public:
   derived(int x, int y): base(y){
     j=x; 
     cout << "Constructing derived.\n";
   }
   ~derived(void) {cout << "Destructing derived.\n";}
   void show(void) {cout << i << ", " << j << endl;}
 };
int main(void)
{
   derived object(3,4);
   object.show();
}


Cascade constructor and destructor call

  
#include <iostream>
using namespace std;
class base 
{
 public:
   base(void) {cout << "Constructing base.\n";}
   ~base(void) {cout << "Destructing base.\n";}
 };
class derived1 : public base 
{
 public:
   derived1(void) {cout << "Constructing derived1.\n";}
   ~derived1(void) {cout << "Destructing derived1.\n";}
 };
class derived2 : public derived1 
{
 public:
   derived2(void) {cout << "Constructing derived2.\n";}
   ~derived2(void) {cout << "Destructing derived2.\n";}
 };
int main(void)
{
   derived2 object;
}


Demonstrate inheriting a protected base class.

  

#include <iostream>
using namespace std;
class BaseClass {
  int i;
protected:
  int j;
public:
  int k;
  void setInt(int a) { 
     i = a; 
  }
  int getInt() { 
     return i; 
  }
};

class DerivedClass : protected BaseClass {  // Inherit BaseClass as protected.
public:
  void setj(int a) { 
     j = a; 
  } 
  void setk(int a) { 
     k = a; 
  } 
  int getj() { 
     return j; 
  }
  int getk() { 
     return k; 
  }
};
int main()
{
  DerivedClass ob;
  ob.setk(10);
  cout << ob.getk() << " ";
  ob.setj(12);
  cout << ob.getj() << " ";
  return 0;
}


Inherit base as private

  
#include <iostream>
using namespace std;
class BaseClass {
  int x;
public:
  void setx(int n) { 
     x = n; 
  }
  void showx() { 
     cout << x << "\n"; 
  }
};
// Inherit BaseClass as private.
class DerivedClass : private BaseClass {
  int y;
public:
  
  void setxy(int n, int m) { 
     setx(n);             // setx is accessible from within DerivedClass
     y = m; 
  }
  
  void showxy() { 
     showx();             // showx is accessible from within DerivedClass
     cout << y << "\n"; 
  }
};
 
int main()
{
  DerivedClass ob;
  ob.setxy(10, 20); 
  ob.showxy();
  return 0;
}


Make field public during private inheritance

  
#include <iostream>
using namespace std;
class base 
{
   int i;
 public:
   int j, k;
   void seti(int x) {i = x;}
   int geti(void) {return i;}
};
class derived : private base 
{
 public:
                  
   base::j;       
   base::seti;    
   base::geti;    
   int a;
};
int main(void)
{
   derived object;
   object.j = 20; // legal because j is public
   object.a = 40;
   object.seti(10);
   cout << object.geti() << ", " << object.j << ", " << object.a;
}


Public inheritance

  
#include <iostream>
using namespace std;
class BaseClass {
protected:
  int i, j; // private to BaseClass, but accessible to DerivedClass
public:
  void set(int a, int b) { 
     i = a; 
     j = b; 
  }
  void show() { 
     cout << i << " " << j << endl; 
  }
};
class DerivedClass : public BaseClass {
  int k;
public:
  // DerivedClass may access BaseClass"s i and j
  void setk() { 
     k = i*j; 
  }
  void showk() { 
     cout << k << endl; 
  }
};
int main()
{
  DerivedClass ob;
  ob.set(2, 3);
  ob.show();   
  ob.setk();
  ob.showk();
  return 0;
}


Share member variables between sub class

  
#include <iostream>
using namespace std;
class AreaClass {
public:
  double height;
  double width;
};
class Rectangle : public AreaClass {
public:
  Rectangle(double h, double w) { 
     height = h; 
     width = w; 
  }
  double area() { 
     return height * width; 
  }
};
class Isosceles : public AreaClass {
public:
  Isosceles(double h, double w) { 
     height = h; 
     width = w; 
  }
  double area() { 
     return 0.5 * width * height; 
  }
};
class cylinder : public AreaClass {
public:
  cylinder(double h, double w) { 
     height = h; 
     width = w; 
  }
  double area()
  { 
    return (2 * 3.1416 * (width/2) * (width/2)) + (3.1416 * width * height); 
  }
};
int main()
{
  Rectangle rectangleObject(10.0, 5.0);
  Isosceles triangleObject(4.0, 6.0);
  cylinder cylinderObject(3.0, 4.0);
  cout << "Rectangle: " << rectangleObject.area() << endl;
  cout << "Triangle: " << triangleObject.area() << endl;
  cout << "Cylinder: " << cylinderObject.area() << endl;
  return 0;
}


Three level public inheriance

  
#include <iostream>
using namespace std;
class BaseClass {
protected:
  int i, j;
public:
  void set(int a, int b) { 
     i = a; 
     j = b; 
  }
  void show() { 
     cout << i << " " << j << endl; 
  }
};
// i and j inherited as protected.
class DerivedClass1 : public BaseClass {
  int k;
public:
  void setk() { 
     k = i*j; 
  } 
  void showk() { 
     cout << k << endl; 
  }
};

class DerivedClass2 : public DerivedClass1 {
  int m;              // i and j inherited indirectly through DerivedClass1.
public:
  void setm() { 
     m = i-j; 
  }
  void showm() { 
     cout << m << endl; 
  }
};
int main()
{
  DerivedClass1 object1;
  DerivedClass2 object2;
  object1.set(2, 3);
  object1.show();
  object1.setk();
  object1.showk();
  object2.set(3, 4);
  object2.show();
  object2.setk();
  object2.setm();
  object2.showk();
  object2.showm();
  return 0;
}


Virtual functions retain virtual nature when inherited.

  
#include <iostream>
using namespace std;
class BaseClass {
public:
  virtual void myFunction()
  {
    cout << "Using BaseClass version of myFunction()\n";
  }
};
class DerivedClass1 : public BaseClass {
public:
  void myFunction() 
  {
    cout << "Using DerivedClass1"s version of myFunction()\n";
  }
};
class DerivedClass2 : public DerivedClass1 {
public:
  void myFunction() 
  {
    cout << "Using DerivedClass2"s version of myFunction()\n";
  }
};
int main()
{
  BaseClass *p;
  BaseClass ob;
  DerivedClass1 derivedObject1;
  DerivedClass2 derivedObject2;
  p = &ob;
  p->myFunction();                   // use BaseClass"s myFunction()
  p = &derivedObject1;
  p->myFunction();                   // use DerivedClass1"s myFunction()
  p = &derivedObject2;
  p->myFunction();                   // use DerivedClass2"s myFunction()
  return 0;
}