一个程序带你入门 类与对象(概念到析构函数)(个人笔记)

这篇文章介绍了C++中的结构体和类的概念,包括它们的成员函数、数据成员的访问权限以及如何定义和使用构造函数和析构函数。同时,讨论了成员函数的定义方式,如内联函数和对象的访问。还涉及了对象的初始化、重载构造函数以及带默认参数的构造函数。
摘要由CSDN通过智能技术生成

如果你想要学习哪部分知识只需要取消那部分的注释再运行

#include <iostream>
#include <cstring>
using namespace std;


//结构体补充
    // 结构体是一个数据类型,里面含有不同类型数据。
    // 例如:
    // struct Student
    // {
    //     int ID;
    //     string name;
    //     void Print(int x,int y)
    //     {
    //         if(x=2,y=3)
    //         {
    //             cout<<"这是个结构体的成员函数1"<<endl;//endl会换行
    //             cout<<"这是个结构体的成员函数2";
    //             cout<<"这是个结构体的成员函数3"<<endl;
    //         }
    //     }
    //     int age;
    //     int score;
    // };
    // int main()
    // {
    //     Student Bill;//定义了一个学生叫Bill,他的类型是Student,本质是个结构体,是个容器
    //     Bill.Print(2,3);//调用结构体的成员函数
    //     Bill.age=19;//当成元素使用
    //     cout<<"Bill's age is "<<Bill.age<<endl;
    //     return 0;
    // }


// 类的声明
    // class Student
    // {
    //     int ID;
    //     string name;
    //     void Print(int x,int y)
    //     {
    //         if(x=2,y=3)
    //         {
    //             cout<<"这是个类的成员函数1"<<endl;//endl会换行
    //             cout<<"这是个类的成员函数2";
    //             cout<<"这是个类的成员函数3"<<endl;
    //         }
    //     }
    //     int age;
    //     int score;
    // };
    // int main()
    // {
    //     Student Bill;//定义了一个学生叫Bill,他的类型是Student,本质是个类,是个容器
    //    Bill.Print(2,3);
    //     Bill.age=19;
    //     cout<<"Bill's age is "<<Bill.age<<endl;//上三行编译错误因为类里面的元素默认私有,结构体元素默认公有 
    //     return 0;
    // }

    // class Student
    // {
    //     private:
    //     public:
    //     int ID;
    //     string name;
    //     void Print(int x,int y)
    //     {
    //         if(x=2,y=3)
    //         {
    //             cout<<"这是个类的成员函数1"<<endl;//endl会换行
    //             cout<<"这是个类的成员函数2";
    //             cout<<"这是个类的成员函数3"<<endl;
    //         }
    //     }
    //     int age;
    //     int score;
    // };
    // int main()
    // {
    //     Student Bill;//定义了一个学生叫Bill,他的类型是Student,本质是个类,是个容器
    //    Bill.Print(2,3);
    //     Bill.age=19;
    //     cout<<"Bill's age is "<<Bill.age<<endl;//因为表明了public
    //     return 0;
    // }

    // class apple
    // {
    //     private:
    //         int apple1,apple2;
    //     public:
    //         int addapple(int x,int y)//类的外部接口,外面数值输入进入小括号里
    //         {
    //             apple1=x;
    //             apple2=y;
    //             return (apple1+apple2);
    //         }
    // };//结构体结尾加;
    // int main()
    // {
    //     apple A;
    //     cout<<A.addapple(4,6)<<endl;//用公共的成员函数来访问私有的数据
    //     return 0;
    // }


// 成员函数的定义
    //一、定义普通成员函数,在类外定义
        // class apple
        // {
        //     private:
        //         int apple1,apple2;
        //     public:
        //         int addapple(int,int);//类内定义函数原型,不需要参数表,只需参数表的类型
            
        // };
        //  int apple::addapple(int x,int y)
        // {
        //         apple1=x;
        //         apple2=y;
        //         return (apple1+apple2);
        //     }
        // int main()
        // {
        //     apple A;
        //     cout<<A.addapple(4,6)<<endl;
        //     return 0;
        // }


    //二、普通的定义在类的内部,代码上二,提高运行效率,但是让类长度增加,所以建议只用在简短的函数

    //三、类外定义,但是使用inline显式说明这是个内联函数
        //         class apple
        // {
        //     private:
        //         int apple1,apple2;
        //     public:
        //         inline int addapple(int,int);//类内定义函数原型,不需要参数表,只需参数表的类型
            
        // };
        //     inline int apple::addapple(int x,int y)
        //     {
        //             apple1=x;
        //             apple2=y;
        //             return (apple1+apple2);
        //         }
        // int main()
        // {
        //     apple A;
        //     cout<<A.addapple(4,6)<<endl;
        //     return 0;
        // }

//对象定义使用
        //        class apple
        // {
        //     private:
        //         int apple1,apple2;
        //     public:
        //         inline int addapple(int,int);
            
        // }A1,A2;//定义对象的特殊方法,简单方法是直接在其他地方定义

        //访问成员函数 对象名:成员函数名(实参表) 可以看出,实参表就是调用函数的时候输入的值

    //指向对象的指针访问类内成员
        //           class apple
        // {
        //     private:
        //         int apple1,apple2;
        //     public:
        //         inline int addapple(int,int);//类内定义函数原型,不需要参数表,只需参数表的类型
            
        // };
        //     inline int apple::addapple(int x,int y)
        //     {
        //             apple1=x;
        //             apple2=y;
        //             return (apple1+apple2);
        //         }
        // int main()
        // {
        //     apple A;
        //     apple *ptr;//指向对象A的指针
        //     ptr=&A;
        //     cout<<A.addapple(4,6)<<endl;
        //     cout<<ptr->addapple(3,3)<<endl;//ptr调用成员函数的时候采用的是->,对象调用成员函数采用的是.
        //     cout<<(*ptr).addapple(3,4)<<endl;//*prt调用成员函数采用.
        //     return 0;
        // }


//构造函数和析构函数
    //c++利用构造函数为对象初始化(为成员赋值),在创建对象的同时调用,省去了主函数等地方调用成员函数的步骤
    //特性:1.名字必须与类名相同2.可以有参数,但不能有返回值3.不需要用户调用,创建对象即调用
        // class machine{
        //     private:
        //     int head,hand;
        // public:
        //     machine(int a,int b)//构造函数,没有返回值,连void也没有
        //     {
        //         head=a;
        //         hand=b;
        //         cout<<"构造函数被调用"<<endl;
        //     }
        //     void addfigure()
        //     {
        //         cout<<(head+hand)<<endl;
        //     }
        // };

        // int main()
        // {
        //     machine A1(2,3);//创建的同时就调用了构造函数
        //     A1.addfigure();
        //     return 0;
        // }

    //建立对象的时候赋初值的两种方法
        // 一、类名 对象名(实参表)
        //     machine A1(2,3);
        // 二、类名*指针变量名=new 类名(实参表)
        //     machine*P1=new machine(2,3);
        //     P1->addfigure();
        //     delete P1;//使用完记得释放

    //构造函数可以在类外定义,但是不需要返回值,连void也没有
        // 对比:
        //     类内:
        //         int one,two;
        //         gouzao(int,int);
        //         int putong(int);
        //     类外:
        //         gouzao::gouzao(int a,int b)
        //         {
        //             one=a;
        //             two=b;
        //         }
        //         int gouzao::putong(a)
        //         {
        //             one=a;
        //             return one;
        //         }


//用成员初始化列表对数据成员初始化(只能应用于构造函数)
    // class lei
    // {
    //     int Q1,Q2;
    //     int W2;
    //     public:
    //     lei (int i,int j):Q1(i),Q2(j)//成员初始化列表
    //     { cout<<"Q1 Q2is"<<Q1<<Q2<<endl;}
    //     // int st(int i2):W2(i2)
    //     // {   
    //     //    return (W2);
    //     // }
        
    // };
    // int main()
    // {
    //     lei A(2,3);
    //     // cout<<(A.st(3))<<endl;//成员初始化列表只能应用于构造函数
    //      return 0;
    // }


//重载构造函数
    // class student{
    //     private:
    //         int hair,jump,run;
    //     public:
    //         student();
    //         student(int,int,int);//重载函数类内声明可省略形参
    //         void figure();
    // };
    // student::student()
    // {
    //     hair=168;
    //     jump=120;
    //     run=3;
    // }
    // student::student(int a,int b,int c)
    // {
    //     hair=a;
    //     jump=b;
    //     run=c;
    // }
    // inline void student::figure()
    // {
    //     cout<<hair<<jump<<run<<endl; 
    // }
    // int main()
    // {
    //     student A1;
    //     student A2(100,100,100);
    //     A1.figure();
    //     A2.figure();
    //     return 0;
    // }

// 带默认函数的构造参数
    //平时的stu::stu()就是默认构造函数,指定实参也可以是默认构造函数,所以一个类只有一个
    // class stu{
    //     private:
    //         int a,b;
    //     public:
    //     stu(int i=1,int j=2)//直接在括号里面给形参赋值
    //     {
    //         a=i;
    //         b=j;
    //     }
    //     void addd()
    //     {
    //         cout<<(a+b)<<endl;
    //     }
    // };
    // int main()
    // {
    //     stu A;
    //     A.addd();
    //     return 0;
    // }

// 析构函数
//     释放分配给对象的内存
    //     class stu{
    //     private:
    //         int a,b;
    //     public:
    //     stu(int i=1,int j=2)//直接在括号里面给形参赋值
    //     {
    //         a=1;
    //         b=2;
    //     }
    //     void addd()
    //     {
    //         cout<<(a+b)<<endl;
    //     }
    //     ~stu()
    //     {
    //         cout<<"析构函数已被调用"<<endl;
    //     }
    // };
    // int main()
    // {
    //     stu A;
    //     A.addd();
    //     return 0;
    // }

个人笔记,如果有错误请指出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值