一、类定义
#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; }