C++类和对象

你知道什么最可怕么?就是明明是因为失望才离开,他却以为是你不够喜欢。。。

​----  网易云热评

一、类定义

#include <iostream>using namespace std;//实现一个表示学生的类//struct Student{class Student{public:    //行为:成员函数    void eat(const string& food){        cout << "我在吃" << food << endl;    }    void sleep(int hour){        cout << "我睡了" << hour << "小时"            << endl;    }    void learn(const string& course){        cout << "我在学" << course << endl;    }    void who(void){        cout << "我叫" << m_name << ",今年"            << m_age << "岁,学号是" << m_no            << endl;    }public:    /* 私有成员不能在类的外部直接访问,但是可以     * 提供公有的成员函数来间接访问,在函数中可     * 以对非法数据加以限定,控制业务逻辑的合理     * 性----封装思想*/    void setName(const string& name){        if(name == "二"){            cout << "你才二" << endl;        }        else{            m_name = name;        }    }    void setAge(int age){        if(age < 0){            cout << "无效年龄" << endl;        }        else{            m_age = age;        }    }    void setNo(int no){        if(no < 10000){            cout << "无效学号" << endl;        }        else{            m_no = no;        }    }private:    //属性:成员变量    string m_name;    int m_age;    int m_no;};int main(void){    //原来:定义结构体类型变量    //现在:创建对象 或 实例化对象    Student s;    /*s.m_name = "张飞";    s.m_name = "二";    s.m_age = 28;    s.m_no = 10086;*/    s.setName("张翼德");    s.setName("二");    s.setAge(29);    s.setAge(-2);    s.setNo(10011);    s.setNo(290);    s.who();    s.eat("拉州拉面");    s.sleep(8);    s.learn("C++编程");    return 0;}

 

二、构造函数,主要负责对象的初始化(初始化成员变量)

#include <iostream>using namespace std;class Student{public:    Student(const string &name,int age,int no){        cout << "构造函数" << endl;        m_name = name;        m_age = age;        m_no = no;    }           void who(void){        cout << "我叫" << m_name << ",今年" <<            m_age << "岁,学号是" << m_no <<                endl;    }private:    string m_name;    int m_age;    int m_no;};int main(void){    //创建对象/实例化对象/构造对象    //(...):指定构造函数需要的实参    Student s("张飞",28,10011);    s.who();    //构造函数不能显式调用    //s.Student("张三",29,10012);    return 0;}
#include <iostream>using namespace std;class A{public:    A(void){        cout << "A的无参构造函数" << endl;        m_i = 0;    }    A(int i){        cout << "A的有参构造函数" << endl;        m_i = i;    }    int m_i;};class B{public:    int m_j;//基本类型成员变量    A m_a;//成员子对象};int main(void){    B b;    cout << "基本类型:" << b.m_j << endl;//?    cout << "类类型:" << b.m_a.m_i << endl;//0    return 0;}
#include <iostream>using namespace std;class A{public:    A(int data = 0){        cout << "A(int=0)" << endl;        m_data = data;    }    //拷贝构造函数    /*A(const A& that){        cout << "A(const A&)" << endl;        m_data = that.m_data;    }*/    int m_data;};int main(void){    const A a1(100);    //A a2(a1);//拷贝构造    A a2 = a1;//和上面写法等价    cout << a1.m_data << endl;//100    cout << a2.m_data << endl;//100    return 0;}
#include <iostream>using namespace std;class Student{public:    //先定义成员变量,再赋初值    /*    Student(const string&name,int age,int no){        cout << "构造函数" << endl;        m_name = name;        m_age = age;        m_no = no;    }*/    //定义成员变量同时初始化    Student(const string&name,int age,int no)        :m_name(name),m_age(age),m_no(no){}    void who(void){        cout << "我叫" << m_name << ",今年" <<            m_age << "岁,学号是" << m_no <<                endl;    }private:    string m_name;    int m_age;    int m_no;};int main(void){    //创建对象/实例化对象/构造对象    //(...):指定构造函数需要的实参    Student s("张飞",28,10011);    s.who();    //构造函数不能显式调用    //s.Student("张三",29,10012);    return 0;}

 

三、对象

#include <iostream>using namespace std;class Student{public:    Student(const string&name,int age,int no){        cout << "构造函数" << endl;        m_name = name;        m_age = age;        m_no = no;    }           void who(void){        cout << "我叫" << m_name << ",今年" <<            m_age << "岁,学号是" << m_no <<                endl;    }private:    string m_name;    int m_age;    int m_no;};int main(void){    //创建对象/实例化对象/构造对象    //(...):指定构造函数需要的实参    //Student s("张飞",28,10011);    Student s = Student("张飞",28,10011);    s.who();    //在栈区创建多个对象    Student sarr[3] = {        Student("貂蝉",20,10012),        Student("小乔",22,10013),        Student("孙尚香",25,10014)};        sarr[0].who();    sarr[1].who();    sarr[2].who();    //在堆区创建/销毁单个对象    Student* ps =        new Student("扈三娘",30,10015);    ps->who();//(*ps).who()    delete ps;    ps = NULL;        //在堆区创建/销毁多个对象    Student* parr = new Student[3]{        Student("孙二娘",35,10016),        Student("白骨精",40,10017),        Student("潘金莲",36,10018)};    parr[0].who();//(*(parr+0)).who()    parr[1].who();//(*(parr+1)).who()    parr[2].who();        delete[] parr;    parr = NULL;    return 0;}

 

欢迎关注公众号:顺便编点程

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web安全工具库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值