6.1 类与对象 【C++】

在这里插入图片描述
引言:(1)在工程中,一般定义多文件架构,每个文件存一种类(或者几个相关的类)

新建类
在这里插入图片描述

或者
在这里插入图片描述

在这里插入图片描述

1.定义类的头文件

解释:
this 是指针,类型是Person*

在这里插入图片描述用this来区分 是 “ 参数的sName " 还是 “ 对象的sName ”

在这里插入图片描述

#pragma once       //类似ifndef  确保只包含一次
#include <string>  
using namespace std;  // 这两个要加,不然编译不过
	// 尽量不要把某些可以放在Person.cpp的头文件或其他文件放在Person.h中
	//如  <iostream>
       // 因为如果另一个.cpp也包含了Person.h的话
       // 那它就重复多次执行了这些文件,编译时间会增加
       // 只有该Person.h需要用到该头文件,如string, 才引用

enum GenderType {  //枚举型(默认)是整数,占4字节
	 gtMale =0, gtFemale =1
};

class Person      // 一共 28 + 28 + 4 + 4 = 64字节
{
public:
	 // 4个属性
	 string sName;        // 28字节
	 string sID;    // 28字节
	 GenderType gender;  // 4字节 枚举
	 int iWeight;   // 4 字节
	 // 构造函数
	 Person(string name, string id);  	//Person(string sName, string sID);
 
	 // 三个方法  成员函数
	 void speak();
	 void eat(int weight);
	 string description();       

	// 析构函数
	 ~Person();
};

【注意:要在Person.h头文件种加 using namespace std;
虽然是在Person.cpp用到的,但是如果将该语句放在.cpp中,会出错
原因: 因为Person.h文件中用到的string 需要是 std::string ,
所以需要用到 该语句 】

2.定义类的cpp

#include "stdafx.h"
#include "Person.h"
#include <iostream> 

void Person::speak() {
	 cout << "Pesron::speak()" << endl;
	 cout << "i am" << sName << "," << "Nice to meet you here." << endl;
}


//表面只有一个参数,其他还有个this指针
// void Person::eat(this, int weight)
// 这样就可以区分 dora还是peter 的 eat
void Person::eat(int weight) {  
	 cout << sName << " this pointer:" << this << endl;    // 打印this指针的地址
	 iWeight += weight;
	 cout << "I just eated " << weight << " gram's food" << endl;
}

string Person::description() {
 	char szText[512];
        sprintf_s(szText, "ID: %s\tName:%s\n", sID.c_str(), sName.c_str());
 	string s = szText;
	sprintf_s(szText, "Gender: %s\tWeight: %d", gender == gtMale ? "Male" : "Female", iWeight);
	string t = szText;
	return s + t;

// string s = "ID: " + sID + "\tName: " + sName;
// string t = "Gender: " + string(gender) + "\tWeight" + string(iWeight);
}


// 	Peson的构造函数
Person::Person(string name, string id)
{
// this 是一个指针,类型是 Person*
// this->sName = sName; //this->sName这个是(Person)对象的sName  
       //这个 sName是参数
       // 该语句功能:区分两个不同的sName
       //也可改为
       // 参数是 (string name, string id)
       //    sName = name ;
       //    sID = id;
// this->sID = sID;
	 sName = name;
	 sID = id;
	 gender = gtMale;
	 iWeight = 50;
}


// 析构函数
Person::~Person()
{
}

3.定义主函数cpp

#include "stdafx.h"
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;
#if 1
// 类与对象
// 如何定义和使用新的数据类型

int main(void) {
// Person p;   // 类型   Person相当于int
// 本质上跟  int i = int (0);  没有区别 
	 Person dora = Person("Dora CHEN", "F001");   // 第一个Person是类型Person,如int类型
	 					      // 第二个Person是执行构造函数Person
	 Person peter = Person("Peter Henry", "F002");
	 dora.gender = gtFemale;
 	 dora.iWeight = 20000;
	 peter.iWeight = 100000;
	 cout << "addr of dora:" << &dora << endl;
	 cout << "addr of peter:" << &peter << endl;

//数据是相互独立的,而代码是共享的,而共享的代码是靠this指针来区分的
// 隐含 this指针  区分 dora / peter 的 eat
 	dora.eat(199);
 	peter.eat(100);

	cout << dora.description() << endl;
	cout << "sizeof dora:" << sizeof(dora) << endl;
 	cout << "sizeof peter:" << sizeof(peter) << endl;

	cout << "sizeof string:" << sizeof(string) << endl;
	cout << "sizeof int:" << sizeof(int) << endl;
	cout << "sizeof GenderType:" << sizeof(GenderType) << endl;

	return 0;
} 
#endif

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值