C++类的学习1(一)

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

1.C++类的定义


C++使用class关键字定义一个类:


public:公共的行为或属性(类外可以调用);


private:表示该部分内容是私密的,不能被外部访问或调用,只能在类内调用;


protected:保护成员,和私有成员类似,不过在派生类可以调用;


比如我们建立一个people类

#include <iostream>
using namespace std;
class people{
  public:
    void a(){
      cout<<name;
    }
  private:
    string name="小明";
    void b(){
      cout<<"dsfsfds";  //不必在意;
    }
}; 
int main(){
  people cao;   //实例化
  cao.a();
  return 0;
} 

 

类的传参,比如我们要传入人的身高和体重,可以通过一个函数传入,传给私有变量;

#include <iostream>
using namespace std;
class people{
  public:
    void a(){
      cout<<name<<' '<<height<<' '<<weight;
    }
    void chuanru(int x,int y){   
//x,y是函数内的的变量,因此需要赋值给整个类的变量,作用域不同
      height=x;
      weight=y;
    } 
  private:
    string name="小明";
    int height;
    int weight; 
}; 
int main(){
  people cao;
  cao.chuanru(170,130);
  cao.a();
  return 0;
} 

此外类内的函数或者方法可以放到类外面,需要先在类内声明函数,通过“::”作用域操作符实现例如:

#include <iostream>
using namespace std;
class people{
  public:
    void a();
    void chuanru(int x,int y);   //函数声明
  private:
    string name="小明";
    int height;
    int weight; 
}; 
void people::a(){           //表面是people的a函数
      cout<<name<<' '<<height<<' '<<weight;
    }
void people::chuanru(int x,int y){
      height=x;
      weight=y;
    } 
int main(){
  people cao;
  cao.chuanru(170,130);
  cao.a();
  return 0;
} 

2.初识构造函数和析构函数


先不管那些拷贝构造函数和转换构造函数,我们先学习普通的,

#include <iostream>
using namespace std;
class people{
  public:
    people(int x=8,int y=9){    //构造函数,可以设置默认值
      height=x;
      weight=y;
    }
    void a(){
      cout<<name<<' '<<height<<' '<<weight;
    }
  private:
    string name="小明";
    int height;
    int weight; 
}; 
int main(){
  people cao(170,130);   //构造函数传参,
  cao.a();
  return 0;
} 

同理构造函数也可以放到类外面


#include <iostream>
using namespace std;
class people{
  public:
    people(int x,int y);
    void a();
  private:
    string name="小明";
    int height;
    int weight; 
}; 
people::people(int x=9,int y=8){    //类内或类外只能有一个地方初始化
      height=x;
      weight=y;
    }
void people::a(){
      cout<<name<<' '<<height<<' '<<weight;
    }
int main(){
  people cao(170,130);
  cao.a();
  return 0;
} 


构造函数用来初始化类,析构函数与构造函数相反;在对象生命周期结束后自动调用,用于在对象删除之前的清理工作,清理对象释放内存;

#include <iostream>
using namespace std;
class people{
  public:
    people(int x,int y);
    void a();
    ~people();
  private:
    string name="小明";
    int height;
    int weight; 
}; 
people::people(int x=9,int y=8){
      height=x;
      weight=y;
    }
people::~people(){
}
void people::a(){
      cout<<name<<' '<<height<<' '<<weight;
    }
int main(){
  people cao(170,130);
  cao.a();
  return 0;
} 

是生命周期结束,举个例子:

#include <iostream>
using namespace std;
class people{
  public:
    people(string n,int x,int y); 
    void a();
    ~people();
  private:
    string name;
    int height;
    int weight; 
}; 
people::people(string n,int x=9,int y=8){
      name=n;
      height=x;
      weight=y;
      cout<<name<<' '<<"调用构造函数"<<endl; 
    }
people::~people(){
  cout<<name<<' '<<"调用析构函数"<<endl; 
}
void people::a(){
      cout<<name<<' '<<height<<' '<<weight<<endl;;
    }
void shiyan(){
  people lin("小刚",180,190);
} 
int main(){
  people cao("小明",170,130);
  shiyan();
  cout<<"main还没结束!"<<endl; 
  return 0;
} 


小明是在main函数内,小刚是在shiyan()函数内,调用完shiyan() 函数后小刚生命周期结束,调用小刚的析构函数,而小明是等到main函数结束后调用析构函数;


其实构造函数还有初始化列表形式:

#include <iostream>
using namespace std;
class people{
  public:
    people(int x,int y);   //构造函数
    void a();
    ~people();            //析构函数
  private:
    string name="小明";
    int height;
    int weight; 
}; 
people::people(int x=9,int y=8):height(x),weight(y){}    //初始化列表    
people::~people(){
}
void people::a(){
      cout<<name<<' '<<height<<' '<<weight;
    }
int main(){
  people cao(170,130);
  cao.a();
  return 0;
} 


相关文章
|
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++类和对象】类和对象的引入