C++中的类

private问题

案例分析

class Box
{
public:
	double length;         // 长度
	double breadth;        // 宽度
	double height;         // 高度
	double getVolume(void)
	{
		return length * breadth * height;
	}
private:
	double length;
};

注意:当把length声明成private时,在主函数中

int main()
{
	Box Box1;                // 声明 Box1,类型为 Box
	
	double volume = 0.0;     // 用于存储体积
	Box1.breadth = 1;
	Box1.height = 1;
	Box1.length = 1; //Error: 因为 length 是私有的
	
	// box 1 的体积
	volume = Box1.getVolume();
	cout << "Box1 的体积:" << volume << endl;
	return 0;
}

调用"Box1.length = 1;",会报错,因为:
private私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的只有自己的类和友元函数可以访问私有成员

用法点拨

1.默认情况下,类的所有成员都是私有的。
2.实际操作中,我们一般会在私有区域定义数据在公有区域定义相关的函数,以便在类的外部也可以调用这些函数。

protected问题

总述:保护成员变量或函数与私有成员十分相似,但有一点不同保护成员在派生类(即子类)中是可访问的

案例分析

#include <iostream>
using namespace std;
 
class Box
{
   protected:
      double width;
};
 
class SmallBox:Box // SmallBox 是派生类
{
   public:
      void setSmallWidth( double wid );
      double getSmallWidth( void );
};
 
// 子类的成员函数
double SmallBox::getSmallWidth(void)
{
    return width ;
}
 
void SmallBox::setSmallWidth( double wid )
{
    width = wid;
}
 
// 程序的主函数
int main( )
{
   SmallBox box;
 
   // 使用成员函数设置宽度
   box.setSmallWidth(5.0);
   cout << "Width of box : "<< box.getSmallWidth() << endl;
   return 0;
}

注:现在您可以看到上面的实例中,我们从父类 Box 派生了一个子类 smallBox。
下面的实例与前面的实例类似,在这里 width 成员可被派生类 smallBox 的任何成员函数访问
在这里要注意,是派生类的任何函数访问,而在主函数里使用"box.width=3;",
则会报错。

继承中的特点

有public, protected, private三种继承方式,它们相应地改变了基类成员的访问属性

1.public 继承

基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:public, protected, private

2.protected 继承

基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:protected, protected, private

3.private 继承

基类 public 成员,protected 成员,private 成员的访问属性在派生类中分别变成:private, private, private

注意:1.private 成员只能被本类成员(类内)和友元访问,不能被派生类访问
2.protected 成员可以被派生类访问

类的构造函数

类的构造函数类的一种特殊的成员函数,它会在每次创建类的新对象时执行。

构造函数的名称与类的名称是完全相同的并且不会返回任何类型,也不会返回 void。构造函数可用于为某些成员变量设置初始值

带参数的构造函数

实例讲解

#include <iostream>

using namespace std;

class Line
{
public:
	void setLength(double len);
	double getLength(void);
	Line(double len);  // 这是构造函数

private:
	double length;
	
};

// 成员函数定义,包括构造函数
Line::Line(double len)
{
	cout << "Object is being created, length = " << len << endl;
	length = len;
	
}

void Line::setLength(double len)
{
	length = len;
	
}

double Line::getLength(void)
{
	return length;
}
// 程序的主函数
int main()
{
	
	Line line(11.0);//在这里实现初始化

	// 获取默认设置的长度
	cout << "Length of line : " << line.getLength() << endl;
	
	//再次设置长度
	line.setLength(6.0);
	cout << "Length of line : " << line.getLength() << endl;

	return 0;
}

注意:在这里"Line line(11.0);"实现初始化,同时还需要在构造函数里声明参数。

使用初始化列表来初始化字段

使用初始化列表来初始化字段:

Line::Line( double len): length(len)
{
    cout << "Object is being created, length = " << len << endl;
}

上面的语法等同于如下语法:

Line::Line( double len)
{
    length = len;
    cout << "Object is being created, length = " << len << endl;
}

**注:**假设有一个类 C,具有多个成员变量X、Y、Z 等需要进行初始化,同理地,您可以使用上面的语法,只需要在不同的字段使用逗号进行分隔,如下所示:

C::C( double a, double b, double c): X(a), Y(b), Z(c)
{
  ....
}

代码实战

#include <iostream>

using namespace std;

class Line
{
public:
	void setLength(double len);
	double getLength(void);
	double getLength1(void);
	double getLength2(void);
	Line(double len, double len1, double len2);  // 这是构造函数

private:
	double length;
	double len1;
	double len2;
	
};

// 成员函数定义,包括构造函数
Line::Line(double len, double len1, double len2):length(len),len1(len1),len2(len2)
{
	cout << "Object is being created, length = " << len << endl;
	length = len;
	
}

void Line::setLength(double len)
{
	length = len;
	
}

double Line::getLength(void)
{
	return length;
}
double Line::getLength1(void)
{
	return len1;
}
double Line::getLength2(void)
{
	return len2;
}
// 程序的主函数
int main()
{
	
	Line line(11.0,2,3);

	// 获取默认设置的长度
	cout << "Length of line : " << line.getLength() << endl;
	cout << "Length of line : " << line.getLength1() << endl;
	cout << "Length of line : " << line.getLength2() << endl;
	//再次设置长度
	//line.setLength(6.0);
	//cout << "Length of line : " << line.getLength() << endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值