C++的使用小教程1——类的创建和使用

最近进入了研究生课题组,老师布置了一个我完全不会的任务……但是我还是得学习啊,毕竟我还要养我的女朋友。
在这里插入图片描述

1、类是什么

C ++ 中的是可以通过用户定义的一种数据类型。 在类当中,既可以包含数据,也可以包含函数,在对类内的函数与数据进行调用时,存在特定的调用方法。
听起来有点复杂,所以让我们看一个类的例子。
让我们创建一个名为"person"的类。这时候我们想,对于一个"person"而言,其应该具有什么"属性"与"能力"。
假设一个person可以做的事情如下:
在这里插入图片描述
person的属性对应着类的数据,person的能力对应着类的函数
我们可以得到一个类如下:

class Person
{
	private:
        string name;
        int age;
        int sex;
        int monthlyIncome;
    public:
    	void setParameter(String nameIn,int ageIn,
    		int sexIn,int monthlyIncomeIn);
        void selfIntroduction();
 		void mining();
 		void catchMouse();
};

在private里的,都是类的数据;在public里的,都是类的方法(函数)。一般而言我们通过函数来访问与修改类的数据,这样可以对类形成一定的封装,从而保护数据不会被胡乱修改与破坏。
这句话的重点是: 我们可以使用函数来访问类中的数据。
这也是C++类的封装特性的具体表现。
至此,我们就已经完成了类中数据与函数的"声明",“声明"代表了"存在”,但是如何具体实现对应着"定义"

2、类的定义

类的定义方式有很多,我们可以将类的声明和定义放在同一个文件里,也可以将二者分开,接下来我将分别讲述两种方法的实现。

2.1、声明与定义在同一文件

该例子将类的声明与定义放在同一文件中,同时在main函数中调用类。

#include<iostream>
#include <string>

using namespace std;

class Person
{
private:
	string name;
	int age;
	string sex;
	int monthlyIncome;
public:
	void setParameter(string nameIn, int ageIn,
		string sexIn, int monthlyIncomeIn);
	void selfIntroduction();
	void mining();
	void catchMouse();
};

void Person::setParameter(string nameIn, int ageIn,
	string sexIn, int monthlyIncomeIn)
{
	//ToDo: finish this function
	name = nameIn;
	age = ageIn;
	sex = sexIn;
	monthlyIncome = monthlyIncomeIn;
}

void Person::selfIntroduction()
{
	cout << "My name is " << name << ", I am " << age << " years old, I am a " << sex << " , my monthlyIncome is " << monthlyIncome <<"."<< endl;
}

void Person::mining()
{
	cout << "I am mining."<<endl;
}

void Person::catchMouse()
{
	cout << "I am the best, I caught a mouse" << endl;
}

int main() {
	Person person;
	person.setParameter("Tom", 20, "girl", 10000);
	person.selfIntroduction();
	person.mining();
	person.catchMouse();
	system("pause");
	return 0;
}

调用结果为:

My name is Tom, I am 20 years old, I am a girl , my monthlyIncome is 10000.
I am mining.
I am the best, I caught a mouse
请按任意键继续. . .

2.2、声明与定义在不同文件

C++中许多库都将类的声明放在头文件.h中,将类的定义放在源文件.cpp中,而调用则发生在另一个有main函数的源文件中,这样做相比于所有函数都放在一个文件里,更加清晰便于阅读,当然其优点不仅仅如此。
person.h文件:

#pragma once
#include<iostream>
#include <string>

using namespace std;

class Person
{
private:
	string name;
	int age;
	string sex;
	int monthlyIncome;
public:
	void setParameter(string nameIn, int ageIn,
		string sexIn, int monthlyIncomeIn);
	void selfIntroduction();
	void mining();
	void catchMouse();
};

person.cpp文件,需要注意的是,该文件需要载入头函数#include “person.h”:

#include<iostream>
#include <string>
#include "person.h"
using namespace std;

void Person::setParameter(string nameIn, int ageIn,
	string sexIn, int monthlyIncomeIn)
{
	//ToDo: finish this function
	name = nameIn;
	age = ageIn;
	sex = sexIn;
	monthlyIncome = monthlyIncomeIn;
}

void Person::selfIntroduction()
{
	cout << "My name is " << name << ", I am " << age << " years old, I am a " << sex << " , my monthlyIncome is " << monthlyIncome << "." << endl;
}

void Person::mining()
{
	cout << "I am mining." << endl;
}

void Person::catchMouse()
{
	cout << "I am the best, I caught a mouse" << endl;
}

main.cpp调用上述库:

#include<iostream>
#include <string>
#include "person.h"
using namespace std;

int main() {
	Person person;
	person.setParameter("Tom", 20, "girl", 10000);
	person.selfIntroduction();
	person.mining();
	person.catchMouse();
	system("pause");
	return 0;
}

调用结果同上。

2.3、C++类的创建和使用中的约定

1、需要使用命名空间classname ::来表示函数是类定义的一部分。

void Person::catchMouse()
{
	cout << "I am the best, I caught a mouse" << endl;
}

2、使用 ‘getVariableName’ 来访问数据,使用 ‘setVariableName’ 来修改数据。(这个我上述为了方便没有遵守)

class Person
{
	......
    public:
        void setName(string nameIn);
        void setSex(int idIn);
		......
        string getName();
        int getSex();
        ......
};

3、大写类名的第一个字母。

class Person
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bubbliiiing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值