2013年5月24日 星期五

C++ virtual function sample


#include<iostream>
using namespace std;


class Employee{
    private:
 
    protected:    
      int base_salary;  
    public:
         
           Employee()
           {
              //cout<< "hello!"<< endl;
              base_salary = 24000;    
           }        
           virtual void calculateSalary()=0;  
   
};

class Manager : public  Employee
{
    protected:
       int Manager_salary;    
    public:
         
       void calculateSalary()
       {
          Manager_salary = base_salary + 30000;
          cout << "Manager Salary :" << Manager_salary << endl;
       }        
   
   
   
};

class Staff : public   Employee
{
    protected:
       int Staff_salary;    
    public:
         
       void calculateSalary()
       {
          Staff_salary = base_salary + 10000;
          cout << "Staff Salary :" << Staff_salary << endl;
       }        
   
   
   
};



int main()
{

  Manager obj;
  obj.calculateSalary();
  Staff obj_1;
  obj_1.calculateSalary();


   
 system("PAUSE");
    return 0;  
}


C++ overloading Sample


C++ overloading
Def:  the same class , the same method, but different argument .
        function , friend function , constructor , member function can implement "Overloading".






#include<iostream>
using namespace std;



class Base{
      friend void Base_friend( Base &T ,int a);
      friend void Base_friend( Base &T ,int a , int b);
private:

    int x;      
       
public:
    Base()
    {
         cout << "Base Construster" << endl;
    }  
   
    Base (int _x)   //Base Constructor overloading
    {
         cout << "Base Constructor overloading" << endl;
         x=_x;
         cout << "x=" << x << endl;
       
    }

    void People()
    {
      cout<< "Base People" << endl;  
    }                
   
    void People(int a)   // member function overloading  
    {
       cout << "People" <<" "<< a << endl;
    }
   

     
     
};

//friend function
void Base_friend( Base &friend_obj ,int a)
{
  friend_obj.x = a;
  cout << "Base_friend x="<<friend_obj.x << endl;    
   
}  

//friend function
void Base_friend( Base &friend_obj ,int a , int b)
{
  friend_obj.x = a;
  cout << "Base_friend overloading x="<<friend_obj.x << endl;
  cout << "b =" << b << endl;
   
}
///////////////////////////////////////////////////////////////////
// function overloading
void People()
{
    cout<< "People" << endl;
}

void People (int x)
{
   cout << "People" << " "<< x << endl;
}  


////////////////////////////////////////////////////////////////////
int main()
{
People();
People (30);
Base obj;
obj.People();
obj.People(7);
Base obj1(2);
Base_friend(obj1 ,8);
Base_friend(obj1 ,8 , 10);
system("PAUSE");
    return 0;
}


2013年5月14日 星期二

C++ 使用物件的方法--五種


#include <iostream>
using namespace std;

class Count{

public:
  void setX(int a);    
  void show();    
private:
  int x;                          
};

void Count::setX(int a)      
{
     x =a;    
}

void Count::show()
{
 cout << x <<endl;  
}

  
int main(void)
{
   
   Count a;
   a.setX(2);
   a.show();
//////////////////////////////  
   Count b;
   Count *ptr = &b; //pointer
   b.setX(12);
   b.show();
//////////////////////////////
   Count c;
   Count &ref = c;
   ref.setX(45);
   ref.show();  
////////////////////////////////////////  
   Count *test = new Count;
   test->setX(44);
   test->show();
///////////////////////////////////////
   (*test).setX(443);
   (*test).show();
/////////////////////////////////////////////    
   
   
    system("PAUSE");
    return 0;
 
}