C++面向对象笔记

本文详细介绍了C++的面向对象特性,包括类的定义、内置函数inline、构造与析构函数、对象实例化、静态成员、多态性、继承、派生类构造函数、多继承、虚基类和异常处理等核心概念。通过对这些知识点的深入探讨,帮助读者掌握C++的面向对象编程技巧。
摘要由CSDN通过智能技术生成

类的定义

class obj {
  int a;
  string b;
  void function(){
  ...;
  }
};
obj x1;//实例化一个对象,于结构体类有多种实例化的方式。
  • 也可以用结构体定义类
    区别是class中的函数默认为private,结构体相反。
struct obj {
  int a;
  string b;
  void function(){
  ...;
  }
};
obj x1;
  • 函数写到类外面,在类中声明即可。
public:
   void play ();//声明函数
private:
   string name;
   int age;
};
void people :: play(){ //::意为play为people类中的函数
count<<"i am so happy !"<<endl;
};

内置函数inline

  • 对于内置函数的处理,是将程序嵌入到调用点,并非正常的调用,所以执行效率很高。
  • 由于调用函数的效率一般较低,而对于规模较小的函数一般采用内置的方式。
class people {
   inline void eat();
   void play (){//类中的函数默认为内置函数,不用声明。
     ...
    }
};


inline void people::eat(){//类外函数如果是内置函数要声明。
   ...
}

对象实例化

class dog{
   
 string name;
 int age;
  };
  dog xiaoha = {
  "hashiqi",3};//实例化一只哈士奇

构造函数

  • 构造函数自动执行且执行一次。
#include <iostream>
using namespace std;


class Time {
   public:
    Time(){     //构造成员函数
        hour=0;
        minute=0;
        sec=0;
      }
    void set_time();
    void show_time();
   private:
    int hour;
    int minute;
    int sec;
};

void Time::set_time(){
  cin>>hour>>minute>>sec;
};
void Time::show_time(){
    cout<<hour<<minute<<sec;
};


int main() {

    Time t1;
    t1.set_time();
    t1.show_time();
    return 0;
}
  • 带参构造函数
#include <iostream>
using namespace std;

class Box{
  //类名首字母最好大写
public:
    Box(int,int,int);//声明带参数的构造函数
    int volume ();//声明计算体积的函数
private:
    int height;
    int width;
    int length;
};

//实现构造函数以及成员函数
  Box::Box(int h,int w,int l){
    height = h;
    width = w;
    length = l;
}

int Box::volume(){

    cout<< height*width*length;
}

int main() {

    Box b1(12,13,14);
    b1.volume();

  return 0;
}

参数初始化列表对成员进行初始化

class People{
  public:
     People(int nu,int s,int na):num(s),sex(s),{strcpy(name,na);}
     //通过:后面的列表将对应的属性初始化;
     private:
       int num;
       char sex;
       char name[20];
};
//实例化对象如下
People p1(01,'F',"Eric");

构造方法的重载

#include <iostream>
using namespace std;

class Box{
  //类名首字母最好大写
public:
    Box ();
    Box(int h,int w,int l):height(h),width(w),length(l){}//构造函数的重载
    int volume ();//声明计算体积的函数
private:
    int height;
    int width;
    int length;
};

int Box::volume(){

    cout<< height*width*length;
}
int main() {

    Box b1(12,13,15);
    b1.volume();
  return 0;
}

析构函数

  • 是希望对象最后一次执行所要做的事
#include <iostream>
using namespace std;

class Box{
  //类名首字母最好大写
public:
    Box ();
    Box(int h,int w,int l):height(h),width(w),length(l){}//构造函数的重载
    ~Box(){ //析构函数
        cout<<'\n'<<"啊。我马上要死了,析构函数要杀了我。";
      }
    int volume ();//声明计算体积的函数
private:
    int height;
    int width;
    int length;
};

int Box::volume(){

    cout<< height*width*length;
}
int main() {

    Box b1(12,13
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值