C++类

C++类&对象

C++在C语言的基础上增加了面向对象程序设计。
类是C++的核心特性,通常被称为用户定义的类型。

  • 类用于指定对象的形式,包含了数据表示法和用于处理数据的方法。
  • 类中的数据和方法被称为类的成员。
  • 函数在一个类被称为类的成员。
class dog
{
	public:
	char color;
	int old;
};

public确定了类成员的访问属性。在类对象作用域内,公共成员在类的外部是可访问的。

类的声明

dog dog1;    //声明dog,类型为dog1

类的对象的公共数据可以使用直接成员访问运算符(.)来访问。

#include<iostream>
using namespace std;
class Box
{
	public:
		double length;
		double breadth;
		double height;
};

int main()
{
	Box box1;
	Box box2;
	double volume;
	
	box1.height=5.0;
	box1.length=6.0;
	box1.breadth=7.0; 
	
	box2.height=7.0;
	box2.length=8.0;
	box2.breadth=9.0; 
	
	volume =box1.breadth*box1.height*box1.length;
	cout<<"The volume of box1:\t"<<volume<<endl;
	
	volume =box2.breadth*box2.height*box2.length;
	cout<<"The volume of box2:\t"<<volume<<endl;
	return 0;
	 
}

运行结果
在这里插入图片描述
类的成员函数
定义和原型写在类定义内部的函数,如同类定义的其它变量一样。
类成员函数是类的一个成员,可以操作类的任意对象,可以访问对象中所有成员。

class Box
{
	public:
		double length;
		double breadth;
		double height;

		double getVolume(void)
		{
			return length*breadth*height;
		}
}; 

也可以在类的外部使用解析运算符::定义函数(注意:在::运算符之前必须使用类名。调用成员函数是在对象上使用点运算符(.))

double Box::getVolume(void)
{
	return length*breadth*height;
}

Box box1;
box1.getVolume();
	

举例

#include<iostream>
using namespace std;
class Box
{
	public:
		double length;
		double breadth;
		double height;
		
		double getVolume(void);
		void setLength(double len);
		void setBreadth(double bre);
		void setHeight(double hei);
};

double Box::getVolume()
{
	return length*breadth*height;
}
void Box::setLength(double len)
{
	length=len;
}
void Box::setBreadth(double bre)
{
	breadth=bre;
}
void Box::setHeight(double hei)
{
	height=hei;
}
int main()
{
	Box box1;
	Box box2;
	double volume;
	
	box1.setBreadth(12.0);
	box1.setHeight(20.0);
	box1.setLength(15.0);
	
	box2.setBreadth(13.0);
	box2.setHeight(22.0);
	box2.setLength(16.0);
	
	volume =box1.getVolume();
	cout<<"The volume of box1\t"<<volume<<endl;
	
	volume =box2.getVolume();
	cout<<"The volume of box2\t"<<volume<<endl;
	return 0;
	
	
}

运行结果
在这里插入图片描述
类访问修饰符
数据封装是面向对象编程的一个重要特点,防止函数直接访问类类型的内部成员。
类成员访问限制是通过在类主体内部对各个区域标记public、private、protected来指定的。
访问修饰符(public、private、protected)

class Box
{
	public:
	//公有成员
	protected:
	//被保护成员
	private:
	//私有成员
};

public(共有成员)在程序中类的外部是可访问的,可以不使用任何成员函数来设置和获取变量的值

private(私有成员)在函数的外部是不可访问,甚至是不可查看的,只有类和有元函数可以访问私有成员

#include<iostream>
using namespace std;
class box
{
	public:
		double length;
		void setWidth(double wid);
		double getWidth(void);
	private:
		double width;
}; 
void box::setWidth(double wid)
{
	width=wid;
}
double box::getWidth()
{
	return width;
}
int main()
{
	box box1;
	box1.length=10.0;
	cout<<"The length of box1:\t"<<box1.length<<endl;
	
	box1.setWidth(10.0);
	cout<<"The width of box1:\t" <<box1.getWidth()<<endl;
	
	return 0;
	
}

在这里插入图片描述

protected(保护成员)保护成员变量或函数与私有成员十分相似,注意:保护成员在派生类(子类)中可访问的

#include <iostream>
using namespace std;
class Box
{
	protected:
		double width;
};
class SmallBox:Box
{
	public: 
	void setSmallWidth(double wid);
	double getSmallwidth(void); 
};

double SmallBox::getSmallwidth(void)
{
	return width;
}

void SmallBox::setSmallWidth(double wid)
{
	width=wid;
}
int main()
{
	SmallBox box3;
	
	box3.setSmallWidth(5.0);
	cout<<"Width of box\t"<<box3.getSmallwidth() <<endl;
	return 0;
}

运行结果
在这里插入图片描述
类构造函数&析构函数
构造函数:名称和类的名称完全相同的,并且不会访问任何类型,也不会返回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(10.0);
	cout << "Length of line : " << line.getLength() <<endl;
	
	 line.setLength(6.0); 
     cout << "Length of line : " << line.getLength() <<endl;
     return 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;
}

析构函数:一种特殊的成员函数,会在每次删除所创建的对象时执行
析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,不会返回任何值,也不能带任何参数。析构函数有利于跳出程序前释放资源。

#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line();   // 这是构造函数声明
      ~Line();  // 这是析构函数声明
 
   private:
      double length;
};
 
// 成员函数定义,包括构造函数
Line::Line(void)
{
    cout << "Object is being created" << endl;
}
Line::~Line(void)
{
    cout << "Object is being deleted" << endl;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
double Line::getLength( void )
{
    return length;
}
// 程序的主函数
int main( )
{
   Line line;
 
   // 设置长度
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}

拷贝构造函数
一种特殊的构造函数,在创建对象时,是使用同一类中的之前创建的对象来初始化新创建的对象。

  • 通过使用另一个同类型的对象来初始化新创建的对象
  • 复制对象把它作为参数传递给函数
  • 复制对象,并从函数返回这个对象

如果类中没有定义拷贝构造函数,编译器会自行定义一个。如果带有指针变量,并有动态分配,则它必须有一个拷贝构造函数,

classname(const classname &obj)
{
	//构造函数主体
}
#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      int getLength( void );
      Line( int len );             // 简单的构造函数
      Line( const Line &obj);      // 拷贝构造函数
      ~Line();                     // 析构函数
 
   private:
      int *ptr;
};
 
// 成员函数定义,包括构造函数
Line::Line(int len)
{
    cout << "调用构造函数" << endl;
    // 为指针分配内存
    ptr = new int;
    *ptr = len;
}
 
Line::Line(const Line &obj)
{
    cout << "调用拷贝构造函数并为指针 ptr 分配内存" << endl;
    ptr = new int;
    *ptr = *obj.ptr; // 拷贝值
}
 
Line::~Line(void)
{
    cout << "释放内存" << endl;
    delete ptr;
}
int Line::getLength( void )
{
    return *ptr;
}
 
void display(Line obj)
{
   cout << "line 大小 : " << obj.getLength() <<endl;
}
 
// 程序的主函数
int main( )
{
   Line line1(10);
 
   Line line2 = line1; // 这里也调用了拷贝构造函数
 
   display(line1);
   display(line2);
 
   return 0;
}

在这里插入图片描述
友元函数
定义在类外部,但有权访问类的所有私有成员和保护成员,尽管有元函数的原型在类的定义中出现过,但是友元函数不是成员函数

友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类,在这种情况下,整个类及其所有成员都是友元。
使用关键字friend

class Box
{
   double width;
  public:
   double length;
   friend void printWidth( Box box );
   void setWidth( double wid );
};

声明类ClassTwo的所有成员函数作为类ClassOne的友元,需要在类ClassOne的定义中放置如下声明

friend class ClassTwo;
  • 38
    点赞
  • 109
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值