C++使用类的基本知识

首先,以前一直把C++的代码,整巴巴的放在一个cpp文件中,巨长无比,看都不想看。看了C++primerplus的相关章节,给出以下建议。

假定创建了一个Apple类。

main()函数是程序的入口,主要负责使用已经定义好的类、函数等。所以单独拿出来放一个cpp文件。命名为useapple.cpp

类的声明包括了成员变量、成员函数、构造函数、析构函数等的声明。声明只是给出了名字,没有给出定义。于是,将声明和定义部分分开。声明部分作为头文件apple.h放入Header Files。

实际上,定义这些函数,则使用另一个cpp文件,apple.cpp

下面看一个例子apple。

首先是类的声明部分。

//apple.h --This is created for display the part of declaration
#ifndef APPLE_H_     //#ifndef #endif must occur in pair, ifndef represent for "if not define"
#define APPLE_H_     //本部分是在main所在的cpp文件中,#include后,使用的,"#ifndef"作用防止多次包含同样的头文件,减少重复编译;APPLE_H_即class的声明部分
class Apple
{
private:			//成员变量一般都是私有的,才能体现C++的封装性
	int m_id;		//成员变量前加m,避免与成员函数的参数混淆,比如m_id, 一个函数setID(int id)
	char m_color[20];
	double m_weight;

public:
	Apple();			//default constructor 默认构造函数,如果自己定义了另外的构造函数,则默认构造函数一定要写出;若无自己的构造函数,则编译器在编译时会自动添加
	Apple(int id, const char * color, double weight);  //自己定义的构造函数
	~Apple();			//deconstructor
	void setID(int id);
	int getID();
	void display();
};						//声明永远是一条语句,用;结尾

#endif

#ifndef #endif p264

接着是类的定义部分。

//apple.cpp --this is created for illustrating the  definition of the apple's related functions
#include <iostream>
#include "apple.h"

Apple::Apple()
{
	std::cout << "default constructor called !" << std::endl;
	m_id = 0;
	std::strcpy(m_color, "no color");
	m_weight = 0;
}

Apple::Apple(int id, const char * color, double weight)
{
	std::cout << "my constructor called !" << std::endl;
	m_id = id;
	std::strncpy(m_color, color, 19);
	m_color[20] = '\0';
	m_weight = weight;
}

Apple::~Apple()
{
	std::cout << "just tell you, now, deconstructor called" << std::endl;
}

void Apple::setID(int id)
{
	m_id = id;
}
int Apple::getID()
{
	return m_id;
}

void Apple::display()
{
	using std::cout;
	using std::endl;
	cout << "ID: " << m_id << "	Color: "<< m_color << "	Weight: " << m_weight << endl;
}
最后是使用部分,即main()函数部分。

//useapp.cpp --This part is created to use the defined class
#include <iostream>
#include "apple.h"

using namespace std;
int main()
{
	Apple iphone(1, "black", 100.00);		//隐式调用构造函数
	iphone.display();

	Apple ipod = Apple(2, "white", 50.00);  //显式调用构造函数
	ipod.display();

	Apple *ipad = new Apple(3, "silver", 500.00);//使用new,显式调用(Apple *ipad = new Apple;这就是隐式调用)
	ipad->display();
	(*ipad).display();
	delete ipad;

	cout << "This is for temp objet" << endl;
	Apple ipad2 = Apple(4, "gold", 500.00);		//创建一个临时对象(没有对象名),赋给ipad2
	ipad2.display();

	cout << "using setID() getID()" << endl;
	iphone.setID(7);
	int id = iphone.getID();
	cout << "this is the new id" << endl;
	iphone.display();

	//默认构造函数的调用
	Apple itouch;				//隐式调用默认构造函数,不用圆括号
	itouch.display();

	return 0;
}

1、定义对象的方式有两种,new和非new。使用new则手动分配空间,划分出一块堆空间stack,得到的是指向对象的指针,如Apple *a = new Apple(1,'blue',100); 使用非new,则系统自动划分一块栈空间,如Apple b = Apple(2,'red',100);

2、关于隐式和显式。p311

(A)初始化:显式初始化,调用(自定义)构造函数即手动给出相应参数值,如Apple b = Apple(2,'red',100);  隐式初始化调用(默认构造函数),即由构造函数自动给相应参数赋值,如Apple c; Apple d = Apple();               [区别:是否出现手动赋值]

(B)构造函数调用:显式调用,如Apple e = Apple();隐式调用,如Apple f; Apple g(4,'yellow',300); Apple *h = new Apple;          区别:显式调用,一般一函数的调用形式体现出来,如function(attr),一般都在"="右边;隐式则如f g h 没有函数的形式,通常在“=”的左边进行相关操作,在调用默认构造函数时,甚至连“()”都没有      只是个人看法,未必正确

3、默认构造函数

(A)若无构造函数,则编译器编译时自动添加;若有构造函数,则必须手动写出默认构造函数

(B)默认构造函数:无参数或不接受参数的构造函数,用于隐式初始化。一种如Apple(){} 函数体内是可以写东西的(函数重载);一种如Apple (int id = 0,const char * color = "red", weight){}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值