C++类和对象的简要小结

C++

类和对象

三种类成员

  • public (公共)类可以由外部对象调用

    • 如:class a{ public: c;}; a b; b.c
  • private (私有)类 由类和友元函数访问,也就是由类对象中的public中的成员函数访问private,不能被子类(派生类)访问

  • protected (被保护)类,可以由子类的成员函数访问父类(派生类)的protected

友元函数

  • 友元函数可以访问private私有类以及protected被保护的类
    例如:
    #include
    using namespace std;
    class Box
    {
    double width;
    public:
    friend void printWidth( Box box );
    void setWidth( double wid );
    };
// 成员函数定义
void Box::setWidth( double wid )
{
	width = wid;
}

// 请注意:printWidth() 不是任何类的成员函数
void printWidth( Box box )
{
   /* 因为 printWidth() 是 Box 的友元,它可以直接访问该类的任何成员 */
   cout << "Width of box : " << box.width <<endl;
}
 
// 程序的主函数
int main( )
{
   Box box;
 
   // 使用成员函数设置宽度
   box.setWidth(10.0);
   
   // 使用友元函数输出宽度
   printWidth( box );
 
   return 0;
}

三个继承

this指针

  • 每个对象都有一个默认的this指针,this指针指向对象的地址,每一个 成员函数都有一个this指针
    例如:
    #include
    using namespace std;
    class Box
    {
    public:
    // 构造函数定义
    Box(double l=2.0, double b=2.0, double h=2.0)
    {
    cout <<“Constructor called.” << endl;
    length = l;
    breadth = b;
    height = h;
    }
    double Volume()
    {
    return length * breadth * height;
    }
    int compare(Box box)
    {
    return this->Volume() > box.Volume();
    }
    private:
    double length; // Length of a box
    double breadth; // Breadth of a box
    double height; // Height of a box
    };

访问权限

访问	    public	protected	private
同一个类	yes	    yes	        yes
派生类	    yes	    yes	        no
外部的类	yes	    no	        no

子类和派生类的区别

  • 个人理解,子类是类派生出的第一代,而派生类是多重的;
    例如:基类A,
    类B直接继承于A
    类C直接继承于B;

    那么依据上面的说法则有:

     类A派生类B,类A被类B继承;类B派生类C, 类B被类C继承。
     类B是类A的子类,类C是类B的子类;
     类B和类C都是类A的派生类。
    
  • class Rectangle: public Shape

new的作用

  • new的作用其实就相当于1.在堆上申请了空间,2.调用了对象的构造函数
    1. 分配空间: 调用函数 operator new 来实现。
    2. 调用构造函数: 调用 placement new 来实现。

成员函数

  • 用::运算符定义,如void Stock::update(double price),定义了update()函数是Stock类的成员
    例如:
//先在Box类中声明成员函数,再在类外定义成员函数
 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(void)
{
	return length * breadth * height;
}

构造函数与析构函数

* 简单的来说,构造函数就是给变量赋值初始值,也就是申请了一块空间;析构函数就是把通过构造函数申请来的空间给释放掉
 * 类每次创建对象时,都会调用构造函数,并且构造函数也是为了给某些变量初始化的
 * 类每次删除对象时,都会调用析构函数,并且可以释放空间
例如:
//构造函数
#include <iostream>
using namespace std;
class Line
{
   public:
	  void setLength( double len );
	  double getLength( void );
	  Line();  // 这是构造函数
 
   private:
	  double length;
};
 
// 成员函数定义,包括构造函数
Line::Line(void)
{
	cout << "Object is being created" << 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;
}

//析构函数
#include
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;
}

指向类的指针

* 个人感觉类似于结构体指针,类的指针可以指向类成员

类的静态成员

* 不论创建类的多少个对象,静态成员都只有一个,是共享的
*  static 关键字来定义静态成员
  //初始化
  int Box::objectCount = 0;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值