#include<iostream>
#include<cstdio>
#include<string>
using namespace std; // 标准命名空间
/*
类:一种新的数据类型--class 类名
对象:类的具体化
行为:所有对象公有动作--成员函数-public(一般习惯)
属性:所有成员公有特性--数据成员-protected
1类中成员默认属性:private:
2类型的权限限定词
private:
protected:
public:
3.类中不需要对象去访问属性
外部访问不行通过对象对象.成员或者对象->成员
4.通过成员函数对类中属性进行初始化
5.构建对象方法
1.类名 对象名 构建一个对象
2.new 一个对象
*/
class girlfriend
{
private: //私有
protected: //保护
int name;
int num;
public: //共有 对外唯一接口
//行为
void shopping()
{
cout << "我在购物" << endl;
}
void Makelove()
{
cout << "makelove" << endl;
}
void printInfor()//行为和操作
{
cout << "name:" << name << endl;
cout << "name:" << num << endl;
}
//修改属性
void InitInfo(int Name, int Num)
{
//数组交换必须用strcpy
name = Name;
num = Num;
}
};
int main()
{
//类名 对象名
girlfriend yourgirfriend;
yourgirfriend.InitInfo(1002,1007);
yourgirfriend.printInfor();
//new一个
//new girlfirend:调用一个无参构建函数,创建一个无名对象,返回一个对象首地址;
//ZQF指向无名对象首地址
//int *p=new int;
girlfriend *ZQF = new girlfriend;
ZQF->Makelove();
ZQF->InitInfo(200, 250);
ZQF->printInfor();
delete ZQF;
system("pause");
return 0;
}
c++类和对象
最新推荐文章于 2023-12-03 18:43:42 发布