C++类的学习1(二)

简介: 的定义一般包括两部分,一是类的属性,二是他所拥有的方法。类的实例化是指给类的加载并初始化过程,比如一个people类,我们具体到每一个人就是类的实例化,此外一个类可以在此类上进行扩展。比如people类,我们分为 外国people 和 中国people,那么people叫做基类,外国people叫做派生类或子类


想知道构造函数怎样工作的吗?


我来告诉你


首先我们来看定义未知数的顺序;

我们先定义height,在定义weight,那么构造函数是按照先初始化height在初始化weight的顺序构造的,并不是按照构造函数的顺序,举个例子:

#include <iostream>
using namespace std;
class people{
  public:
    people(int x);
    void a();
    ~people();
  private:
    string name="小明";
    int height;
    int weight; 
}; 
people::people(int x=9):height(x),weight(height){}   
//先初始化height,再将height赋值给weight    
people::~people(){
}
void people::a(){
      cout<<name<<' '<<height<<' '<<weight;
    }
int main(){
  people cao(170);
  cao.a();
  return 0;
} 

#include <iostream>
using namespace std;
class people{
  public:
    people(int x);
    void a();
    ~people();
  private:
    string name="小明";
    int height;
    int weight; 
}; 
people::people(int x=9):height(weight),weight(x){}  
//先初始化height,但height依赖于weight,由于weight没有初始化,所以出现错误;
people::~people(){
}
void people::a(){
      cout<<name<<' '<<height<<' '<<weight;
    }
int main(){
  people cao(170);
  cao.a();
  return 0;
} 

 


 3.类的继承


子承父业吗,子类继承父类,分为三种继承方式;


(1)公共继承:父类的公共属性在子类还是公共属性,保护属性还是保护属性,私有属性子类访                              问不到


(2)保护继承:父类的公共属性和保护属性在子类中都变为保护属性;私有还是访问不到


(3)私有继承:父类的公共属性在子类变为保护属性,父类的保护属性变为私有属性,私有访问                             不到;


举例说明:


(1)公共继承:

#include <iostream>
using namespace std;
class father{
  public:
    string a="father 公共";
  protected:
    string b="father 保护";
  private:
    string c="father 私有";
};
class son:public father{
  public:
//    string a="son 公共";
//    string b="son 保护";
//    c="son 私有"; 
  void dain(){
    cout<<a<<endl<<b<<endl<<endl;
//    cout<<c;  //报错 
  }
};
int main(){
  son linyuan;
  linyuan.dain();
  cout<<linyuan.a<<endl; 
//  cout<<linyuan.b<<endl;    //报错 
  return 0;
}

(2)保护继承

#include <iostream>
using namespace std;
class father{
  public:
    string a="father 公共";
  protected:
    string b="father 保护";
  private:
    string c="father 私有";
};
class son:protected father{
  public:
//    string a="son 公共";
//    string b="son 保护";
//    c="son 私有"; 
  void dain(){
    cout<<a<<endl<<b<<endl<<endl;
//    cout<<c;  //报错 
  }
};
int main(){
  son linyuan;
  linyuan.dain();
//  cout<<linyuan.a<<endl; 
//  cout<<linyuan.b<<endl;    //报错 
  return 0;
}


(3)私有继承


#include <iostream>
using namespace std;
class father{
  public:
    string a="father 公共";
  protected:
    string b="father 保护";
  private:
    string c="father 私有";
};
class son:private father{
  public:
//    string a="son 公共";
//    string b="son 保护";
//    c="son 私有"; 
  void dain(){
    cout<<a<<endl<<b<<endl<<endl;
//    cout<<c;  //报错 
  }
};
int main(){
  son linyuan;
  linyuan.dain();
//  cout<<linyuan.a<<endl; 
//  cout<<linyuan.b<<endl;    //报错 
  return 0;
}


那么继承方式讲完了,其实C++类还支持同时继承多个类例如;

#include <iostream>
using namespace std;
class father1{
  public:
    string name1;
    father1(){
      name1="小刚"; 
    }
};
class father2{
  public:
    string name1;
    father2(){
      name1="小明"; 
    }
};
class son:public father1,public father2{
  public:
    string name1;
    son(){
      name1="木木渊"; 
    }
}; 
int main(){
  son a;
  cout<<a.name1<<endl;
  cout<<a.father1::name1<<endl;
  cout<<a.father2::name1;
} 

派生类和父类继承元素重名时可以通过命名空间来分别 ,函数也是如此实现多继承;


接下来我们看一下析构函数的情况;

#include <iostream>
using namespace std;
class father1{
  public:
    string name1;
    father1(){
      cout<<"father1 构造函数"<<endl; 
      name1="小刚"; 
    }
    ~father1(){
      cout<<"father1 析构函数"<<endl; 
    }
};
class father2{
  public:
    string name1;
    father2(){
      name1="小明"; 
      cout<<"father2 构造函数"<<endl; 
    }
    ~father2(){
      cout<<"father2 析构函数"<<endl; 
    }
};
class son:public father1,public father2{
  public:
    string name1;
    son(){
      name1="木木渊"; 
      cout<<"son 构造函数"<<endl;
    }
    ~son(){
      cout<<"son 析构函数"<<endl; 
    }
}; 
int main(){
  son a;
  cout<<"main 要结束了"<<endl; 
} 

很明显son先继承father1,在继承father2,因此构造函数先是father1,再father2,最后son,析构函数正好相反;

相关文章
|
1天前
|
编译器 C语言 C++
【C++入门学习指南】:函数重载提升代码清晰度与灵活性
【C++入门学习指南】:函数重载提升代码清晰度与灵活性
9 0
|
1天前
|
存储 Java C++
【C++类和对象】探索static成员、友元以及内部类
【C++类和对象】探索static成员、友元以及内部类
|
1天前
|
安全 程序员 编译器
【C++类和对象】初始化列表与隐式类型转换
【C++类和对象】初始化列表与隐式类型转换
|
1天前
|
安全 编译器 C++
【C++类和对象】const成员函数及流插入提取
【C++类和对象】const成员函数及流插入提取
|
1天前
|
存储 C++
【C++类和对象】日期类的实现(下)
【C++类和对象】日期类的实现
|
1天前
|
编译器 C++
【C++类和对象】日期类的实现(上)
【C++类和对象】日期类的实现
|
1天前
|
编译器 C++ 索引
【C++类和对象】拷贝构造与赋值运算符重载(下)
【C++类和对象】拷贝构造与赋值运算符重载
|
1天前
|
存储 编译器 C++
【C++类和对象】拷贝构造与赋值运算符重载(上)
【C++类和对象】拷贝构造与赋值运算符重载
|
1天前
|
编译器 C++
【C++类和对象】构造函数与析构函数
【C++类和对象】构造函数与析构函数
【C++类和对象】构造函数与析构函数
|
1天前
|
存储 编译器 C语言
【C++类和对象】类和对象的引入(下)
【C++类和对象】类和对象的引入