目录
面向对象基础
类与对象
概念
类的内容
class MobilePhone// 帕斯卡命名法(大驼峰命名法)
{
public:
string brand; // 品牌
string model; // 型号
int weight; // 重量
void play_music()
{
cout << "音乐ing" << endl;
}
void run_game()
{
cout << "LOL、原神、王者荣耀、开心消消乐、" << endl;
}
void call()
{
cout << "hello" << endl;
}
};
创建对象
1.栈内存对象
#include <iostream>
using namespace std;
int main()
{
MobilePhone mp;
mp.brand = "xiaomi";
mp.model = "11 Pro";
mp.weight = 200;
cout << mp.brand << " " << mp.model << " " << mp.weight << " " << endl;
mp.play_music();
mp.run_game();
mp.call();
return 0;
}
2.堆内存对象
#include <iostream>
using namespace std;
int main()
{
MobilePhone* mp = new MobilePhone; // 堆内存对象
mp->brand = "huawei";
mp->model = "mate 30";
mp->weight = 200;
cout << mp->brand << " " << mp->model << " " << mp->weight << endl;
mp->play_music();
mp->run_game();
mp->call();
delete mp; // 手动销毁
mp = NULL;
return 0;
}
封装
#include <iostream>
using namespace std;
class MobilePhone
{
private: // 私有权限,private是最封闭的权限,只能在类内访问
string brand; // 品牌
string model; // 型号
int weight = 200; // 重量
public: // 权限:public最开放的权限
// geter 读函数
string get_brand()
{
return brand;
}
// seter 写函数
void set_brand(string b)
{
brand = b;
}
// 读函数
string get_model()
{
return model;
}
// seter 写函数
void set_model(string m)
{
model = m;
}
// get
int get_weight()
{
return weight;
}
};
int main()
{
MobilePhone mp1; // 栈内存对象
mp1.set_brand("pingguo");
mp1.set_model("15 Pro");
cout << mp1.get_brand() << " " << mp1.get_model() << " " << mp1.get_weight() << endl;
MobilePhone *mp2 = new MobilePhone; // 堆内存对象
mp2->set_brand("pingguo");
mp2->set_model("16 pro");
cout << mp2->get_brand() << " " << mp2->get_model() << " " << mp2->get_weight() << endl;
return 0;
}
构造函数
基本使用
#include <iostream>
using namespace std;
class MyPhone{
private:
string pinpai;//品牌
string xinghao;//型号
int zhongliang;//重量
public:
MyPhone()
{
cout <<"调用无参构造函数"<<endl;
pinpai="小米";
xinghao="11 Pro";
zhongliang=205;
}
MyPhone(string a,string b,int c)
{
cout <<"调用有参构造函数"<<endl;
pinpai=a;
xinghao=b;
zhongliang=c;
}
string getPinpai()
{
return pinpai;
}
void setPinpai(string value)
{
pinpai = value;
}
string getXinghao()
{
return xinghao;
}
void setXinghao(string value)
{
xinghao = value;
}
int getZhongliang()
{
return zhongliang;
}
};
int main()
{
MyPhone *mp=new MyPhone("苹果","16 Pro",200);
cout <<"手机为"<<mp->getPinpai()<<" "<<mp->getXinghao()<<" 重量为:"<<mp->getZhongliang()<<"g"<<endl;
delete mp;
mp=NULL;
MyPhone a;
cout <<"手机为"<<a.getPinpai()<<" "<<a.getXinghao()<<" 重量为:"<<a.getZhongliang()<<"g"<<endl;
return 0;
}
构造初始化列表
#include <iostream>
using namespace std;
class MobilePhone
{
private:
string brand;
string model;
int weight;
public:
MobilePhone():brand("8848"),model("M6巅峰版"),weight(300){}
MobilePhone(string b,string m,int w):brand(b),model(m),weight(w){}
string get_brand()
{
return brand;
}
string get_model()
{
return model;
}
int get_weight()
{
return weight;
}
};
int main()
{
MobilePhone mp1("小米","su7",300);
cout << mp1.get_brand() << " " << mp1.get_model() << " " << mp1.get_weight() << endl;
return 0;
}
隐式调用与显式调用
#include <iostream>
using namespace std;
class Student
{
private:
int age;
public:
explicit Student(int a):age(a)
{
cout << "构造函数" << endl;
}
Student():age(1)
{
cout << "无参构造函数" << endl;
}
int get_age()
{
return age;
}
};
int main()
{
Student s1(12); // 显式调用
cout << s1.get_age() << endl;
Student s3 = Student(14); // 显式调用
cout << s3.get_age() << endl;
// Student s4 = 15; // 隐式调用
// cout << s4.get_age() << endl;
Student* s2 = new Student(13); // 显式调用
cout << s2->get_age() << endl;
Student s5;
cout << s5.get_age() << endl;
return 0;
}
拷贝构造函数
#include <iostream>
using namespace std;
class Student
{
private:
int age;
public:
Student(int a):age(a)
{
cout << "构造函数" << endl;
}
Student(const Student &st)
{
age = st.age;
cout << "拷贝构造函数被调用了" << endl;
}
int get_age()
{
return age;
}
};
int main()
{
Student s1(12);
cout << s1.get_age() << endl;
Student s2(s1); // 拷贝构造函数
cout << s2.get_age() << endl;
return 0;
}
浅拷贝
#include <iostream>
#include <string.h>
using namespace std;
class Dog
{
private:
char *name;
public:
Dog(char *n)
{
name = n;
}
void show_name()
{
cout << name << endl;
}
};
int main()
{
char arr[20] = "旺财";
Dog d1(arr);
Dog d2(d1); // 拷贝构造函数
strcpy(arr,"大黄"); // 更改外部内存,对象内部的数据也跟着进行更改,因为操作的同一块内存空间
d1.show_name(); // 大黄
d2.show_name(); // 大黄
return 0;
}
深拷贝
#include <iostream>
#include <string.h>
using namespace std;
class Dog
{
private:
char *name;
public:
Dog(char *n)
{
name = new char[20];
strcpy(name,n);
}
Dog(Dog &d)
{
name = new char[20];
strcpy(name,d.name);
}
void show_name()
{
cout << name << endl;
}
};
int main()
{
char arr[20] = "旺财";
Dog d1(arr);
Dog d2(d1); // 拷贝构造函数
strcpy(arr,"大黄");
d1.show_name(); // 旺财
d2.show_name(); // 旺财
return 0;
}
析构函数
构造函数 | 析构函数 |
创建对象时手动调用 | 当对象销毁时,自动调用 |
函数名称是类名 | 函数名称是~类名 |
构造函数可以重载 | 析构函数没有参数,不能重载 |
用于创建对象时并初始化 | 用于销毁对象时释放资源 |
有返回值但是不写,返回值是新创建的对象 | 没有返回值 |
#include <iostream>
#include <string.h>
using namespace std;
class Dog
{
private:
char *name;
public:
Dog(char *n)
{
name = new char[20];
strcpy(name,n);
}
Dog(Dog &d)
{
name = new char[20];
strcpy(name,d.name);
}
void show_name()
{
cout << name << endl;
}
~Dog()
{
cout << "析构函数被调用了" << endl;
delete []name;
}
};
int main()
{
char arr[20] = "旺财";
Dog d1(arr);
Dog d2(d1); // 拷贝构造函数
strcpy(arr,"大黄");
d1.show_name(); // 旺财
d2.show_name(); // 旺财
Dog *d3 = new Dog(arr);
delete d3; // 销毁时调用析构函数
return 0;
}
作用域限定符:
名字空间
#include <iostream>
#include <string.h>
using namespace std;
namespace my_space// 名字空间
{
int a = 3;
int b = 4;
}
using namespace my_space;
int a = 2;
int main()
{
int a = 1;
cout << a << endl; // 就近原则,打印1
cout << ::a << endl; // ::全局作用域(匿名名字空间)2
cout << my_space::a << endl; // 3
cout << b << endl; // 4
return 0;
}
类内声明,类外定义
#include <iostream>
using namespace std;
class Demo
{
public:
// 类内声明
Demo();
void test(string str);
};
// 类外定义
Demo::Demo()
{
cout << "创建了一个对象" << endl;
}
void Demo::test(string str)
{
cout << "string:" << str << endl;
}
int main()
{
Demo d1;
d1.test("hello");
return 0;
}