C++基础实例-类(6)

栈与基础类

#if 0
#include<iostream>
using namespace std;
const int MAX=5;     //假定栈中最多保存5个数据

//定义名为stack的类,其具有栈功能
class  stack {
    //数据成员
    float  num[MAX];  //存放栈数据的数组
    int  top;          //指示栈顶位置的变量
public:
    //成员函数
    void init(void) { top=0; }    //初始化函数
    void push(float x)          //入栈函数
    {
        if (top==MAX){
            cout<<"Stack is full !"<<endl;
            return;
        };
        num[top]=x;
        top++;
    }
    float pop(void)          //出栈函数
    {
        top--;
        if (top<0){
        cout<<"Stack is underflow !"<<endl;
        return 0;
        };
        return num[top];
    }
}

//以下是main()函数,用stack类创建栈对象,并使用这些对象
main(void)
{
    //声明变量和对象
    int i;
    float x;
    stack a,b;    //声明(创建)栈对象

    //以下对栈对象初始化
    a.init();
    b.init();

    //以下利用循环和push()成员函数将2,4,6,8,10依次入a栈对象
    for (i=1; i<=MAX; i++)
        a.push(2*i);

    //以下利用循环和pop()成员函数依次弹出a栈中的数据并显示
    for (i=1; i<=MAX; i++)
       cout<<a.pop()<<"  ";
    cout<<endl;

    //以下利用循环和push()成员函数将键盘输入的数据依次入b栈
    cout<<"Please input five numbers."<<endl;
    for (i=1; i<=MAX; i++) {
         cin>>x;
         b.push(x);
    }
 
    //以下利用循环和pop()成员函数依次弹出b栈中的数据并显示
    for (i=1; i<=MAX; i++)
       cout<<b.pop()<<"  ";
}
#endif

构造函数

#if 0
/*构造函数*/
#include<iostream>
using namespace std;
const int MAX=5;   	//假定栈中最多保存5个数据

//定义名为stack的具有栈功能的类
class  stack {
    //数据成员
    float  num[MAX];     //存放栈数据的数组
    int  top;             //指示栈顶位置的变量
public:
    //成员函数
    stack(char c)          //初始化函数-构造函数
    {
        top=0; 
        cout<<"Stack "<<c<<" initialized."<<endl;
    } 	
    void push(float x)      //入栈函数
    {
        if (top==MAX){
            cout<<"Stack is full !"<<endl;
            return;
        };
        num[top]=x;
        top++;
    }
    float pop(void)           //出栈函数
    {
        top--;
        if (top<0){
        cout<<"Stack is underflow !"<<endl;
        return 0;
        };
        return num[top];
    }
}

//以下是main()函数,用stack类创建栈对象,并使用这些对象
main(void)
{
    //声明变量和对象
    int i;
    float x;
    stack a('a'),b('b');    //声明(创建)栈对象并初始化

    //以下利用循环和push()成员函数将2,4,6,8,10依次入a栈
    for (i=1; i<=MAX; i++)
        a.push(2.0*i);

    //以下利用循环和pop()成员函数依次弹出a栈中的数据并显示
    for (i=1; i<=MAX; i++)
       cout<<a.pop()<<"  ";
    cout<<endl;
}
#endif

#if 0
#include<iostream>
using namespace std;
main()
{
    //定义一个名为student的类
    class student {
          int num;
          char *name;
          float grade;
    public:
         //定义构造函数
        student(int n,char *p,float g): num(n),name(p),grade(g){}
        display(void) {
              cout<<num<<" ,"<<name<<","<<grade<<endl;
          }
    }; 

    student a(1001,"XieCh",95),b(1002,"TanXu",96.5);   //创建对象,并初始化
    //student c;  错误,没提供参数

    a.display();            //显示对象a中的数据
    b.display();            //显示对象b中的数据
}
#endif

构造函数重载

#if 0
/*构造函数重载*/
#include <iostream.h>
#include <stdlib.h>
//定义timer类
class timer{
    long minutes;
public:
    //无参数构造函数
    timer(void) { 
        minutes =0;
    };
    //字符指针参数的构造函数
    timer(char *m) { 
        minutes = atoi(m);
    };
    //整数类型的构造函数
    timer(int h, int m) { 
        minutes = 60*h+m ;
    };
    //双精度浮点型构造函数
    timer(double h) { 
        minutes = (int) 60*h ;
    };
    long getminutes(void) { return minutes ; };
};
//main()函数的定义
main(void)
{
    //使用double类型的构造函数创建对象
    timer start(8.30),finish(17.30);
	cout<<"finish(17.30)-start(8.30)=";
    cout<<finish.getminutes()-start.getminutes()<<endl;  

    //使用char指针类型的构造函数创建对象
    timer start0("500"),finish0("800");   //创建对象
	cout<<"finish0(\"800\")-start0(\"500\")=";
    cout<<finish0.getminutes()-start0.getminutes()<<endl;  

    //使用无参数构造函数和整型构造函数创建对象
    timer start1;   
    timer finish1(3,30);  
	cout<<"finish1(3,30)-start1=";
    cout<<finish1.getminutes()-start1.getminutes()<<endl;  

    return 0;
}
#endif

#if 0
#include <iostream>
using namespace std;
//定义rect类
class rect {
    int length;
    int width;
    int area;
public:
    rect(int l=1,int w=1)
    {
        length=l;
        width=w;
        area=length*width;
    }
    void show_rect(char *name)
    { 
		cout<<name<<":"<<endl;
        cout<<"length="<<length<<endl;
        cout<<"width="<<width<<endl;
        cout<<"area="<<area<<endl;
    }
};
//测试使用rect类
void main(void)
{
    //用rect类创建对象
    rect a;
    rect b(2);
    rect c(2,3);

    //调用对象的函数显示对象中的数据
    a.show_rect("a");
    b.show_rect("b(2)");
    c.show_rect("c(2,3)");
}
#endif

析构函数

#if 0
/*析构函数*/
#include<iostream>
using namespace std;
const int MAX=5;   	//假定栈中最多保存5个数据

//定义名为stack的具有栈功能的类
class  stack {
    //数据成员
    double  num[MAX];   //存放栈数据的数组
    int  top;             //指示栈顶位置的变量
public:
    //成员函数
    stack(char *name)  //构造函数
    {
        top=0; 
        cout<<"Stack "<<name<<" initialized."<<endl;
    }
    ~stack(void)   //析构函数
    {
        cout << "Stack destroyed." << endl;  //显示信息
    }

    void push(double x)	//入栈函数
    {
        if (top==MAX)
		{
            cout<<"Stack is full !"<<endl;
            return;
        };
        num[top]=x;
        top++;
    }
    double pop(void) 	//出栈函数
    {
        top--;
        if (top<0)
		{
        cout<<"Stack is underflow !"<<endl;
        return 0;
        };
        return num[top];
    }
}

//以下是main()函数,其用stack类创建栈对象,并使用了这些对象
main(void)
{
    double x;
    //声明(创建)栈对象并初始化
    stack a("a"),b("b");    

    //以下利用循环和push()成员函数将2,4,6,8,10依次入a栈
    for (x=1; x<=MAX; x++)
        a.push(2.0*x);

    //以下利用循环和pop()成员函数依次弹出a栈中的数据并显示
	cout<<"a: ";
    for (int i=1; i<=MAX; i++)
       cout<<a.pop()<<"  ";
    cout<<endl;

    //从键盘上为b栈输入数据,并显示
    for(i=1;i<=MAX;i++) 
	{

        cout<<i<<" b:";
        cin>>x;
        b.push(x);
    }
	cout<<"b: ";
    for(i=1;i<=MAX;i++) 
       cout<<b.pop()<<" ";
    cout<<endl;
}
#endif

类外部定义成员函数

#if 0
/*类外部定义成员函数*/
#include<iostream>
using namespace std;
#define MAX 5
//定义stack类接口
class stack
{
    int num[MAX];
    int top;
public:
     stack(char *name);   //构造函数原型
    ~stack(void);         //析构函数原型
    void push(int n);
    int pop(void);
};
//main()函数测试stack类
main(void)
{
    int i,n;
    //声明对象
    stack a("a"),b("b");    

    //以下利用循环和push()成员函数将2,4,6,8,10依次入a栈
    for (i=1; i<=MAX; i++)
        a.push(2*i);

    //以下利用循环和pop()成员函数依次弹出a栈中的数据,并显示
	cout<<"a: ";
    for (i=1; i<=MAX; i++)
       cout<<a.pop()<<"  ";
    cout<<endl;

    //从键盘上为b栈输入数据,并显示
    for(i=1;i<=MAX;i++) {
        cout<<i<<" b:";
        cin>>n;
        b.push(n);
    }
	cout<<"b: ";
    for(i=1;i<=MAX;i++) 
       cout<<b.pop()<<" ";
    cout<<endl;

    return 0;
}
//-------------------------
//   stack成员函数的定义
//-------------------------
//定义构造函数
stack::stack(char *name)
{
    top=0;
    cout << "Stack "<<name<<" initialized." << endl;
}
//定义析构函数
stack::~stack(void)
{
    cout << "stack destroyed." << endl;  //显示信息
}
//入栈成员函数
void stack::push(int n)
{
    if (top==MAX){
        cout<<"Stack is full !"<<endl;
        return;
    };
    num[top]=n;
    top++;
}
//出栈成员函数
int stack::pop(void)
{
    top--;
    if (top<0){
        cout<<"Stack is underflow !"<<endl;
        return 0;
    };
    return num[top];
}
#endif

public

#if 0
/*public*/
#include<iostream>
using namespace std;
//定义一个全部为public:模式的类
class ex 
{
public:
     int value;
     void set(int n) 
	 { 
         value=n;
     }
     int get(void) 
	 {
        return value;
     }
};
//测试使用ex类
main()
{
    ex a;    //创建对象

    //以下通过成员函数访问对象数据
    a.set(10);
	cout<<"a.get()=";
    cout<<a.get()<<endl;

    //以下直接访问对象的数据成员
    a.value=20; 
	cout<<"a.value=";
    cout<<a.value<<endl;
}
#endif

类接口

#if 0
/*类接口*/
#include <iostream>
using namespace std;
//类接口定义
class ex_class
{
private:
    int iv;
    double dv;
public:
    ex_class(void);
    ex_class(int n,double x);
    void set_ex_class(int n,double x);
    void show_ex_class(char*);
};

//定义ex_class类的构造函数
ex_class::ex_class(void):iv(1), dv(1.0) { }
ex_class::ex_class(int n,double x):iv(n), dv(x) { }

//定义ex_class类的成员函数
void ex_class::set_ex_class(int n,double x)
{
    iv=n;
    dv=x;
}
void ex_class::show_ex_class(char *name)
{
    cout<<name<<": "<<endl;
    cout <<"iv=" <<iv<< endl;
    cout <<"dv=" <<dv<< endl;
}
//使用ex_class类
void main(void)
{
    ex_class obj1;
    obj1.show_ex_class("obj1");
    obj1.set_ex_class(5,5.5);
    obj1.show_ex_class("obj1");

    ex_class obj2(100,3.14);
    obj2.show_ex_class("obj2");
    obj2.set_ex_class(2000,1.732);
    obj2.show_ex_class("obj2");
}
#endif

static

#if 0
/*static*/
#include<iostream>
using namespace std;
//定义一个含有static数据成员的类
class ex
{
    static int num;      //static数据成员
public:
    ex() {num++;}
    ~ex() {num--;}
    disp_count() {
       cout<<"The current instances count:";
       cout<<num<<endl;
    }
};
int ex::num=0;    //设置static数据成员的初值
//main()函数测试ex类
main()
{
    ex a;
    a.disp_count();

    ex *p;
    p=new ex;//分配内存
    p->disp_count();

    ex x[10];
    x[0].disp_count();

    delete p;//释放内存
    a.disp_count();
}
#endif

类作用域符引用成员函数

#if 0
/*类作用域符引用成员函数*/
#include<iostream>
using namespace std;
//定义一个含有static数据成员的类
class ex
{
    static int num;      //static数据成员
public:
    ex() {num++;}
    ~ex() {num--;}
    static disp_count(void) //static成员函数
    {
        cout<<"The current instances count:";
        cout<<num<<endl;
    }
};
int ex::num=0;    //设置static数据成员的初值
//main()函数测试ex类
main()
{
    ex a;
    a.disp_count();

    ex *p;
    p=new ex;
    p->disp_count();

    ex x[10];
	ex::disp_count();   //直接用类作用域符引用静态成员函数

    delete p;
    ex::disp_count();  //直接用类作用域符引用静态成员函数
}
#endif

内联函数

#if 0
/*类的内联函数*/
#include <iostream>
using namespace std;

class ex_class 
{
    int value;
public:
    ex_class(int n) 
	{
        value=n;
        cout << "Stack initialized." << endl;
    }
    ~ex_class() 
	{    
        cout << "The Object destroyed." <<endl;  
    }
    void set_value(int n);
    void show_val(char *name);
} ;

//在类外定义内联成员函数
inline void ex_class::set_value(int n) 
{
    value=n;
}
//在类外定义非内联成员函数
void ex_class::show_val(char *name) 
{
	cout<<name<<": ";
    cout<<value<<endl;
}
//在main()函数中测试ex_class类
main(void)
{
    //创建对象x和y
    ex_class x(10),y(20);

    //显示对象的数据
    x.show_val("x");
    y.show_val("y");

    //设置新值给对象
    x.set_value(1);
    y.set_value(2);

    //显示对象的数据
    x.show_val("x");
    y.show_val("y");

    return 0;
}
#endif

空类

#if 0
/*空类*/
#include <iostream>
using namespace std;
//定义空类empty
class empty
{
};
//在main()函数中用空类创建对象
main()
{
    empty a,*p;  //编译通过
    cout<<"This is a empty class."<<endl;
}
#endif

struct定义类

#if 0
/*struct定义类*/
#include<iostream>
using namespace std;
//用struct关键字定义ex_class类
struct ex_class 
{
    ex_class(int n=1): value(n) {}
    void set_value(int n) 
	{
         value=n;
    }
    show_obj(char *name) {
		cout<<name<<": "<<value<<endl;
    }
private:
    int value;
}
//测试 ex_class类
main()
{
    //用ex_class创建对象
    ex_class a,b(3);
    
    a.show_obj("a");
    b.show_obj("b");

    a.set_value(10);
    b.set_value(20);

    a.show_obj("a");
    b.show_obj("b");
}
#endif

双亲类

#if 0
/*双亲类*/
#include <iostream.h>
#include<string.h>
//定义双亲(parent)类
class parent 
{
    char  f_name[20];
    char  m_name[20];
    char  tel[10];
public:
    // parent类的构造函数,其带有缺省值
    parent(char *p1="",char *p2="",char *p3="") 
	{
        strcpy(f_name,p1);
        strcpy(m_name,p2);
        strcpy(tel,p3);
    }
    //显示parent对象的数据
    show_parent(void) 
	{    
        cout<<"The parent:"<<endl;
        cout<<"    father's name:"<<f_name<<endl;
        cout<<"    mother's name:"<<m_name<<endl;
        cout<<"    tel:"<<tel<<endl;
    }
};
//定义student类
class student 
{
    int       num;
    char      name[20];
    float     grade;
    parent    pt;      
public:
    // student类的构造函数
    student(int n,char *str,float g,class parent t) 
	{
        num=n;
        strcpy(name,str);
        grade=g;
        pt=t;
    }
    //显示student对象的数据
    show_student(void) 
	{
        cout<<"num:"<<num<<endl;
        cout<<"name:"<<name<<endl;
        cout<<"grade:"<<grade<<endl;
        pt.show_parent();
    }
};
//main()函数测试student类的对象
main(void)
{
    //创建双亲对象
    parent p1("Xiej","Yin","83665215");

	//创建学生对象
    student st(10001,"XieCh",91.5,p1); 
	
    //显示学生信息
	cout<<"p1:"<<endl;
	p1.show_parent();

    //显示学生信息
	cout<<"st:"<<endl;
    st.show_student();
}
#endif

重载成员函数

#if 0
/*重载成员函数*/
#include <iostream.h>
#include <stdlib.h>
//定义timer类
class timer{  
    long minutes;
public:
    //定义重载成员函数
    settimer(char *m) 
	{ 
        minutes = atoi(m);
    };
    //定义重载成员函数
    settimer(int h, int m) 
	{ 
        minutes = 60*h+m ;
    };
    //定义重载成员函数
    settimer(double h) 
	{ 
        minutes = (int) 60*h ;
    };
    long getminutes(void) { return minutes; };
};
//main()函数的定义
main(void){
    timer start,finish;   //创建对象

    //使用重载成员函数
    start.settimer(8,30);
    finish.settimer(9,40); 
	cout<<"finish.settimer(9,40)-start.settimer(8,30):";
    cout<<finish.getminutes()-start.getminutes()<<endl;  

    //使用重载成员函数
    start.settimer(2.0);
    finish.settimer("180"); 
	cout<<"finish.settimer(\"180\")-start.settimer(2.0):";
    cout<<finish.getminutes()-start.getminutes()<<endl;  

  return 0;
}
#endif

重载函数

#if 0
/*重载函数*/
#include <iostream>
using namespace std;
//定义复数类
class complex
{
    float  real;       //实部
    float  image;     //虚部
public:
    //重载的运算符"+"的原型
    complex operator+ (complex right);
    //重载赋值运算符"="的定义
    complex operator= (complex right);
    void set_complex(float re, float im);
    void put_complex(char *name);
};
//重载加法运算符"+"的定义
complex complex::operator+ (complex right) 
{
    complex temp;
    temp.real = this->real + right.real;
    temp.image = this->image + right.image;
    return temp;
}
//重载加赋值运算符"="的定义
complex complex::operator= (complex right) 
{   
        this->real = right.real;
        this->image = right.image;
        return *this;
}
//定义set_complex()成员函数
void complex::set_complex(float re, float im) 
{
        real = re;
        image = im;
}
//定义put_complex()成员函数
void complex::put_complex(char *name) 
{
        cout<<name<<": ";
        cout << real << ' ';
        if (image >= 0.0 ) cout << '+';
        cout << image << "i\n";
}
//在main()函数中使用complex类的对象
main(void)
{
    complex A, B, C;  //创建复数对象

    //设置复数变量的值
    A.set_complex(1.2, 0.3);
    B.set_complex(-0.5, -0.8);

    //显示复数数据
    A.put_complex("A");
    B.put_complex("B");

    //赋值运算,显示结果
    C = A;
    C.put_complex("C=A");

    //加法及赋值运算,显示结果
    C = A + B;
    C.put_complex("C=A+B");
    return 0;
}
#endif

友元类

#if 0
/*友元类*/
#include <iostream>
using namespace std;
//定义YourClass类,
class YourClass
{
//指定YourOtherClass是它的友元类
friend class YourOtherClass;  
private:
    int num;
public:
    YourClass(int n){num=n;}
    display(char *YCname){
        cout<<YCname<<".num :";
	    cout<<num<<endl;
    }
};
//定义YourOtherClass,它是YourClass类的友元类
class YourOtherClass
{
public:
    //使用YourClass类的私有成员
    void disp1(YourClass yc,char *YCname){ 
         cout<<YCname<<".num :";
        cout<<yc.num<<endl;   
    }
    //使用YourClass类的公共成员
    void disp2(YourClass yc,char* YCname){
        yc.display(YCname);       
    }
};
//在main()函数中创建和使用YourClass和YourOtherClass类对象
main(void)
{
    //声明YourClass类对象
    YourClass a(20),b(30);

    //显示a和b对象的值
    cout<<"YourClass:"<<endl;
    a.display("a");
    b.display("b");

    //声明YourOtherClass类对象
    YourOtherClass temp;

    //通过temp显示a和b对象的值
    cout<<"YourOtherClass:"<<endl;
    temp.disp1(a,"a");
    temp.disp2(b,"b");
}
#endif

#if 0
#include<iostream>
using namespace std;
//Y类的不完全定义
class Y;

//X类的定义    
class X 
{   
public:
    void disp(Y py,char *name);   //成员函数原型
};

//定义Y类
class Y 
{  
    //声明本类的友元函数
    //X类的disp()为本类的友元函数
    friend void X::disp(Y py,char *name);
     //普通函数putY() 为本类的友元函数
    friend void putY(Y& yc,char *name);         
private: //私有成员
    int num;
    dispY(char *name)
	{    
        cout<<name<<".num="<<num<<endl;
    }
public: //公共成员函数
    Y(int n)
	{  
       num=n;
    }
};

//X类成员函数的实现
void X::disp(Y py,char *name)
{
    cout<<"In X::disp():"<<endl;
    py.dispY(name);   //访问Y类的私有函数
}

//普通函数putY()的定义
void putY(Y& yc,char *name){
    cout<<"In getY:"<<endl;
    yc.dispY(name);
    cout<<name<<".num=";
    cout<<yc.num<<endl;
}

//在main()函数测试X和Y类的功能
main()
{
    //创建Y和X类的对象
    Y y1(10),y2(20);
    X x;

    //不可用Y类对象的私有成员函数显示
    //y1.dispY("y1");
    //y2.dispY("y2");

    //调用X类对象的友元函数显示
    x.disp(y1,"y1");
    x.disp(y2,"y2");

    //用getY函数显示Y类的对象显示
    putY(y1,"y1");
    putY(y2,"y2");
}
#endif
<<与>>重载
#if 0
/*输入输出运算符重载*/
#include <iostream>
using namespace std;
//定义日期类
class Date    
{
    //定义友元重载输入运算符函数
    friend istream& operator >> (istream& input,Date& dt ); 
    //定义友元重载输出运算符函数
    friend ostream& operator<< (ostream& output,Date& dt ); 
    int mo, da, yr;
public:
    Date(void)
	{  //无参数构造函数
        yr = 0;
        mo = 0; 
        da = 0; 
    }
    Date( int y, int m, int d )   //带参数构造函数
    {
        yr = y;
        mo = m; 
        da = d; 
    }
};
//定义">>"运算符重载函数
istream& operator >> ( istream& input, Date& dt )
{
    cout<<"Year:";
    input>>dt.yr;
    cout<<"Month:";
    input>>dt.mo;
    cout<<"Day:";
    input>>dt.da;
    return input;
}

//定义"<<"运算符重载函数
ostream& operator<< ( ostream& output, Date& dt )
{
   output<< dt.yr << '/' << dt.mo << '/' << dt.da<<endl;
   return output;
}

//在main()函数中测试Date类的插入(<<)和提取(>>)运算符
void main()
{
    //声明对象
    Date dt1(2016,5,4),dt2;

    //显示dt1对象
    cout<<dt1;

    //对dt2对象进行输入和输出
    cin>>dt2;
    cout<<dt2;
}
#endif
删除对象
#if 0
/*删除对象*/
#include<iostream>
using namespace std;
//定义ex类
class ex_class 
{
    int a;
    double b; 
public:
    ex_class(int n=1,double x=1.0):a(n),b(x) {}
    void show_value(char *name) 
	{
        cout<<name<<" :"<<endl;
        cout<<"a="<<a<<endl;
        cout<<"b="<<b<<endl;
    }
};

//main()函数
main()
{
    //创建ex_class的对象并显示
    ex_class obj1,obj2(10,3.5);    
    obj1.show_value("obj1");
    obj2.show_value("obj2"); 

    //创建ex_class的指针变量
    ex_class *p;

    //p指向obj1并显示
    p=&obj1;
    p->show_value("p->obj1");

    //p指向obj2并显示
    p=&obj2;
    (*p).show_value("(*p)obj2");

    //p指向动态创建的对象并显示
    p=new ex_class;
    p->show_value("p->new");

    delete p;   //删除对象

}
#endif

派生类

#if 0
#include<iostream>
using namespace std;
//基类Box
class Box {
    int width,height;
public:
    void SetWidth(int w) 
	{
        width=w;
    }
    void SetHeight(int h) 
	{
        height=h;
    }
    int GetWidth() {return width;}
    int GetHeight() {return height;}
};
//派生类ColoredBox
class ColoredBox:public Box
{
    int color;
public:
    void SetColor(int c)
	{
        color=c;
    }
    int GetColor() {return color;}
};
// 在main()中测试基类和派生类
main(void)
{
    //声明并使用ColoredBox类的对象
    ColoredBox cbox;
    cbox.SetColor(3);       //使用自己的成员函数
    cbox.SetWidth(15);     //使用基类的成员函数
    cbox.SetHeight(10);    //使用基类的成员函数
 
    cout<<"cbox:"<<endl;
    cout<<"Color:"<<cbox.GetColor()<<endl;    //使用自己的成员函数
    cout<<"Width:"<<cbox.GetWidth()<<endl;   //使用基类的成员函数
    cout<<"Height:"<<cbox.GetHeight()<<endl;  //使用基类的成员函数
    //cout<<cbox.width; Error!  
}
#endif

派生类与基类private

#if 0
/*派生类与基类private*/
#include<iostream>
using namespace std;
//基类First
class First 
{
    int val1;
public:
    SetVal1(int v) 
	{
        val1=v;
    }
    void show_First(void) 
	{
        cout<<"val1="<<val1<<endl;
    }
};
//派生类Second
class Second:private First //private模式
{   
    int val2;
public:
    void SetVal2(int v1,int v2) 
	{
        SetVal1(v1);     //可见,合法
        val2=v2;
    }
    void show_Second(void) 
	{
    // cout<<"val1="<<val1<<endl; 不能访问First私有成员
        show_First();
        cout<<"val2="<<val2<<endl;
    }
};
main() 
{
    Second s1;
    //s1.SetVal1(1);    //不可见,非法
    s1.SetVal2(1,2);    //合法
    //s1.show_First();  //不可见,非法
    s1.show_Second();
}
#endif

派生类与public

#if 0
/*派生类与public*/
#include<iostream>
using namespace std;
//基类First
class First 
{
    int val1;
public:
    SetVal1(int v) 
	{
        val1=v;
    }
    void show_First(void) 
	{
        cout<<"val1="<<val1<<endl;
    }
};
//派生类Second
class Second:public First //public模式
{  
    int val2;
public:
    void SetVal2(int v1,int v2) 
	{
        SetVal1(v1);     //可见,合法
        val2=v2;
    }
    void show_Second(void) {
    // cout<<"val1="<<val1<<endl; 不能访问First私有成员
        show_First();
        cout<<"val2="<<val2<<endl;
    }
};
main() 
{
    Second s1;
    //调用Second类定义的成员函数
    s1.SetVal2(2,3);    
    cout<<"s1.show_Second():"<<endl;
    s1.show_Second();

    //调用First类定义的成员函数
    s1.SetVal1(10);  
    cout<<"s1.show_First():"<<endl;	
    s1.show_First(); 
}
#endif

多重派生类

#if 0
/*多派生类*/
#include<iostream>
using namespace std;
//定义最底层基类,它作为其他类的基类
class First 
{
    int val1;
public:
    First(void) 
	{
        cout<<"The First initialized"<<endl;
    }
};
//定义派生类,它作为其他类的基类
class Second :public First 
{   
    int val2;
public:
    Second(void) 
	{
        cout<<"The Second initialized"<<endl;
    }
};
//定义最上层派生类
class Three :public Second 
{
    int val3;
public:
    Three() 
	{
        cout<<"The Three initialized"<<endl;
    }
};
//定义各基类的对象,测试构造函数的执行情况
//定义各基类的对象,测试构造函数的执行情况
main() { 
	cout<<"First f1;"<<endl;
    First f1;
    cout<<"Second s1;"<<endl;
    Second s1;
    cout<<"Three t1;"<<endl;
    Three t1;
}
#endif

派生类有、无参构造函数

#if 0
/*派生类有/无参构造函数*/
#include<iostream>
using namespace std;
//定义基类First
class First 
{
    int  num;
    float grade;
public:
    //构造函数带参数
    First(int n,float v ) : num(n),grade(v)
    {
        cout<<"The First initialized"<<endl;
    }
    DispFirst(void) 
	{
        cout<<"num="<<num<<endl;
        cout<<"grade="<<grade<<endl;
    }
};

//定义派生类Second
class Second :public First 
{  
    double val;
public:
    //无参数构造函数,要为基类的构造函数设置参数
    Second(void):First(1000,0) 
	{
        val=1.0;
        cout<<"The Second initialized"<<endl;
    }

    //带参数构造函数,为基类的构造函数设置参数
    Second(int n,float x,double dx):First(n,x) 
	{
        val=dx;
        cout<<"The Second initialized"<<endl;
    }
    Disp(char *name)
	{
        cout<<name<<".val="<<val<<endl;
        DispFirst();
    }
};

//main()函数中创建和使用派生类对象
main() 
{
    //调用派生类的无参数构造函数
	cout<<"Second s1;"<<endl;
    Second s1;
	cout<<"s1.Disp(\"s1\");"<<endl;
	s1.Disp("s1");

    //调用派生类的有参数构造函数
	cout<<"Second s2(10002,95.7,3.1415926); "<<endl;
    Second s2(1002,95.7,3.14159); 
	cout<<"s2.Disp(\"s2\");"<<endl;	
    s2.Disp("s2");
}
#endif

#if 0
#include<iostream>
using namespace std;
//定义最低层基类First,它作为其他类的基类
class First 
{
    int val1;
public:
    First() 
	{
        cout<<"The First initialized"<<endl;
    }
    ~First() 
	{
        cout<<"The First destroyed"<<endl;
    }
};
//定义派生类Second,它作为其他类的基类
class Second :public First 
{   //默认为private模式
    int val2;
public:
    Second() 
	{
        cout<<"The Second initialized"<<endl;
    }
    ~Second() 
	{
        cout<<"The Second destroyed"<<endl;
    }
};
//定义最上层派生类Three
class Three :public Second 
{
    int val3;
public:
    Three() 
	{
        cout<<"The Three initialized"<<endl;
    }
    ~Three() 
	{
        cout<<"The Three destroyed"<<endl;
    }
};
//main()函数中测试构造函数和析构函数的执行情况
main() 
{ 
    Three t1;
    cout<<"---- Use the t1----"<<endl;
}
#endif

派生类与protected

#if 0
/*派生类与protected*/
#include<iostream>
using namespace std;
//基类
class First 
{
    int val1;
protected:
    void SetVal1(int v) 
	{
        val1=v;
    }
public:
    show_First(void) 
	{
        cout<<"val1="<<val1<<endl;
    }
};
//派生类
class Second:public First 
{   
    int val2;
protected:
    void SetVal2(int v) 
	{
        SetVal1(v);  //使用First 基类的保护成员
        val2=v;
    }
public:
    show_Second(void) 
	{
        show_First();
        cout<<"val2="<<val2<<endl;
    }
};
//派生类
class Third:public Second 
{   
    int val3;
public:
    void SetVal3(int n) 
	{
         SetVal1(n);  //使用First 基类的保护成员
         SetVal2(n);  //使用Second基类的保护成员
         val3=n;
    }
    show_Third(void) 
	{
        show_Second();
        cout<<"val3="<<val3<<endl;
    }
};
//main()函数的定义
main(void)
{
    First f1;
    //f1.SetVal1(1);   不可访问

    Second s1;
    //s1.SetVal1(1);   不可访问
    //s1.SetVal2(2);   不可访问

    Third  t1;
    //t1.SetVal1(1);   不可访问
    //t1.SetVal2(2);   不可访问
    t1.SetVal3(10);

	//显示t1对象的数据
	cout<<"t1.show_Third();"<<endl;
    t1.show_Third();
    cout<<"t1.show_Second();"<<endl;
    t1.show_Second();
    cout<<"t1.show_First();"<<endl;
    t1.show_First();
}
#endif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值