C++/Class/Base Class

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Base class and derived class

  
#include <iostream>
using namespace std;
class BaseClass {
  int i, j;
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(int x) { 
     k = x; 
  }
  void showk() { 
     cout << k << endl; 
  }
};
int main()
{
  DerivedClass ob(3);
  ob.set(1, 2); // access member of BaseClass
  ob.show();    // access member of BaseClass
  ob.showk();   // uses member of DerivedClass class
  return 0;
}


class to represent a book

   
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
class Book 
{
  public: 
    char title[256];
    char author[64];
    float price;
    void show_title(void) { cout << title << "\n"; };
    float get_price(void) { return(price); };
    void show_book(void); 
    void assign_publisher(char *name) { strcpy(publisher, name); };
  private:
    char publisher[256];
    void show_publisher(void) { cout << publisher << "\n"; };
};
void Book::show_book(void) 
{ 
    show_title(); 
    show_publisher(); 
};
  
int main(void)
{
   Book tips, diary; 
  
   strcpy(tips.title, "C");
   strcpy(tips.author, "D");
   tips.price = 49.95;
   tips.assign_publisher("E");
   
   strcpy(diary.title, "F");
   strcpy(diary.author, "G");
   diary.price = 9.95;
   diary.assign_publisher("None");
   tips.show_book();
   diary.show_book();
 
}


Init member variables from base class

  
#include <iostream>
using namespace std;
class BaseClass {
  int i, j;
public:
  BaseClass(int x, int y) { 
     i = x; 
     j = y; 
  }
  void showij() { 
  
     cout << i << " " << j << "\n"; 
  }
};
class DerivedClass : public BaseClass {
  int k;
public:
  DerivedClass(int a, int b, int c) : BaseClass(b, c) {
    k = a;
  }
  void show() { 
    cout << k << " "; showij(); 
  }
};
int main()
{
  DerivedClass ob(1, 2, 3);
  ob.show();
  return 0;
}


Pass arguments to base class

  
#include <iostream>
using namespace std;
class BaseClass {
  int i;
public:
  BaseClass(int n) { 
    cout << "Constructing base class\n"; 
    i = n;
  }
  ~BaseClass() { 
    cout << "Destructing base class\n"; 
  }
  void showi() { 
    cout << i << "\n"; 
  }
};
class DerivedClass : public BaseClass {
  int j;
public:
  DerivedClass(int n, int m) : BaseClass(m) { 
    cout << "Constructing DerivedClass class\n"; 
    j = n;
  } 
  ~DerivedClass() { 
    cout << "Destructing DerivedClass class\n"; 
  }
  void showj() { 
    cout << j << "\n"; 
  }
};
int main()
{
  DerivedClass o(10, 20);
  o.showi();
  o.showj();
  
  return 0;
}


Use Base keyword to call method in parent class from subclass

  
#include <iostream>
using namespace std;
class Base {
 public:
   void display(void) { cout << "This is the base class" << endl; };
};
class Derived: public Base {
 public:
   void display(void) { cout << "This is the derived class" << endl; };
};
int main(void){
   Derived my_class;
   my_class.display();
   my_class.Base::display();
}


Using base pointers on DerivedClass class objects.

  
// 
#include <iostream>
#include <cstring> 
using namespace std;
class BaseClass {
  char author[80];
public:
  void put_author(char *s) { 
     strcpy(author, s); 
  }
  void show_author() { 
     cout << author << endl; 
  }
} ;
class DerivedClass : public BaseClass {
  char title[80];
public:
  void put_title(char *num) {
    strcpy(title, num);
  }
  void show_title() {
    cout << "Title: ";
    cout <<  title << endl;
  }
};
int main()
{
  BaseClass *p;
  BaseClass baseObject;
  DerivedClass *dp;
  DerivedClass derivedObject;
  p = &baseObject;  
  p->put_author("Tom Clancy");
  p = &derivedObject;
  p->put_author("William Shakespeare");
  baseObject.show_author();
  derivedObject.show_author();
  cout << endl;
  dp = &derivedObject;
  dp->put_title("The Tempest");
  p->show_author(); 
  dp->show_title( );
  return 0;
}