Advertisement
B1KMusic

C++ class examples

Apr 17th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // Class
  6. class Animal{
  7.   public:
  8.   int lifespan;
  9.   Animal(){}
  10.   Animal(int _lifespan){
  11.     lifespan = _lifespan;
  12.   }
  13. };
  14.  
  15. // Inheritance
  16. class Dog : public Animal{
  17.   public:
  18.   string say;
  19.   Dog(string _say) : Animal(20){
  20.     say = _say;
  21.   }
  22. };
  23.  
  24. // Extension
  25. //    void Dog::whine(){
  26.   //    cout << "*whine*" << endl
  27. //    }
  28. // Apparently you can't do that in C++ unless it was already declared within the class
  29.  
  30. //Instantiation
  31. int main(){
  32.   Dog doggy("bark");
  33.   cout << "Dog says " << doggy.say << endl;
  34.   cout << "Dog's lifespan is " << doggy.lifespan << endl;
  35.   return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement