c++类和对象

#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;


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值