⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cadremanager.cpp

📁 是关于c++语言中派生类和对象的例子。是初学者了解此应用的很好的范例。同时
💻 CPP
字号:
#include "iostream.h"
#include "string.h"

/*********公有派生*******/
/*
class  CBase {
       int  x;
       void PrintX(){cout<<x<<endl;}
   public:
       void  SetX (int  m)
       { x = m ; }
       void  ShowX ( )
       { PrintX(); }
} ;

class  CDerived : public CBase {
       int y;
   public:
        void  SetXY (int m, int n) 
        { SetX(m); 
           x = m;  
           y = n;  
        }
		void  ShowXY ( ) 
        { ShowX ( ) ;
          cout << x << endl; 
          cout << y << endl;
        }
};

void main ( )
{ CDerived  Obj;
  Obj.SetXY(10, 20);  
  Obj.ShowXY ( );
  Obj.SetX(10); 
  Obj.PrintX();  
  Obj.ShowX( );
}
*/
/*********私有派生***********/
/*
class  CBase {
       int  x;
   public:
       void  SetX (int  m)
       { x = m ; }
       void  ShowX ( )
       { cout << x << endl; }
} ;

class  CDerived : private CBase {
       int y;
   public:
        void  SetXY (int m, int n) 
        { SetX(m);  
          x = m;    
           y = n;  
        }
		void  ShowXY ( ) 
        { ShowX ( ) ;
          cout << x << endl;               
          cout << y << endl;
        }
};

void main ( )
{ CDerived  Obj;
  Obj.SetXY(10, 20);
  Obj.ShowXY ( );
  Obj.SetX(10);    
  Obj.ShowX( );  
}
*/

/*********类的保护成员的访问*********/
/*
class  CSamp {
       int  a;
   protected:
       int  b;
   public:
       int  c;
       CSamp (int  m, int n)
       { a = m ;     b = n ;  
       }

       int  Get_A ( )
       { 
		   return  a; 
       }
	   int  Get_B ( )
       { return  b; 
       }
} ;

void main ( )
{ CSamp  Obj(20, 30);
  Obj.b = 99 ;       
  Obj.c = 50 ; 
  cout << Get_A() << endl ;
  cout << Get_B() << endl ;
  cout << Obj.b << endl ; 
  cout << Obj.c << endl ;
}
*/

/**********工院干部类**********/
/*
class cadre
{
  private:
    char *name;
    char sex;
    int age;
    float salary;
    friend class manager;
  public:
    cadre()
    {
       name=NULL;sex=' ';age=0;salary=0;
    }    
	cadre(char *name1,char sex1,int age1,float salary1)
    {
      name=new char[strlen(name1)+1];
      strcpy(name,name1);
      sex=sex1;
      age=age1;
      salary=salary1;
    }
    void print()
    {
      cout<<name<<",'"<<sex<<"',"<<age<<","<<salary<<"\n";
    }
    void salary_add(int x){salary+=x;}
};
class manager:public cadre
{
  private:
    char *rank;
  public:
    manager(char *name1,char sex1,int age1,float salary1,char *rank1):
    cadre(name1,sex1,age1,salary1)
    {
      rank=new char[strlen(rank1)+1];
      strcpy(rank,rank1);
    }
    void print()
    {
      cout<<name<<",'"<<sex<<"',"<<age<<","<<salary<<",";
      cout<<rank<<"\n";
    }
};

void main()
{
  cadre per1("Zhang 3",'M',31,1876);
  per1.salary_add(34);
  per1.print();
  manager mana1("Li 4",'F',29,980,"Captain");
  mana1.salary_add(25);
  mana1.print();
}
*/

/***********派生类的构造和析构函数**********/
/*
class  CBase{
    public:
        CBase( )  { cout << "Constructing base class! \n";   }
       ~CBase( )  { cout << "Destructing base class! \n";   }
} ;
class  CDerived : public CBase{
    public:
        CDerived( )  { cout << "Constructing derived class! \n";   }
       ~CDerived ( )  { cout << "Destructing derived class! \n";   }
} ;
void main ( )
{ 
	CDerived  ob; 
}
*/
/*****************************/
/*
class  CBase{
         int  x;
    public:
        CBase( int n )
        { 
			cout << "Constructing base class! \n";    
			x = n;
		}
      ~CBase( )  { cout << "Destructing base class! \n";   }
        void ShowX ( ) { cout << "X = " << x << endl; }
} ;
class  CDerived : public CBase{
        int  y;
    public:
        CDerived( int n, int m) : CBase ( m)   //m进行参数传递
        { 
			cout << "Constructing derived class! \n";   
			y = n;  
		}
      ~CDerived ( )  { cout << "Destructing derived class! \n";   }
        void ShowY ( ) { cout << "Y = " << y << endl; }
} ;

void  main ()
{ CDerived  obj (30, 40);
  obj.ShowX ( );
  obj.ShowY ( );
}
*/

/**********多继承时的二义性**********/
/*
class A {
  public:
      void f();
     };
class B {
   public:
      void f();
      void g();
     };
class C: public A,public B
     {
      public:
      void g();
      void h();
     };

void  main ()
{
	C obj;
	obj.f();
}
*/

/***********虚基类**********/

class A
{   
public:
	A(const char *s)
	{ 
		cout<<s<<endl;
	}
};

class B:virtual public A
{ 
public:
	B(const char *s1,const char *s2):A(s1)
	{  
		cout<<s2<<endl;
	}
};

class C:virtual public A
{   
public:
	C(const char *s1,const char *s2):A(s1)
	{ 
		cout<<s2<<endl;
	}
};

class D:public B,public C
{    
public:
	D(const char *s1,const char *s2,const char *s3,const char *s4):B(s1,s2),C(s1,s3),A(s1)
	{ 
		cout<<s4<<endl;
	}
};
void main()
{
	D *ptr=new D("class A","class B","class C","class D");
	delete ptr;
}


 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -