CPP06 More On Classes

Separate Files for Classes

Creating a New Class

It is generally a good practice to define your new classes in separate files. This makes maintaining and reading the code easier.
Click File->New->Class…
Give your new class a name, uncheck “Has destructor” and check “Header and implementation file shall be in same folder”, then click the “Create” button.
在这里插入图片描述
Note that two files have been added to your project:
在这里插入图片描述

The new files act as templates for our new class.
- MyClass.h is the header file.
- MyClass.cpp is the source file.

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

The new files act as templates for our new class.
- MyClass.h is the header file.
- MyClass.cpp is the source file.

Source & Header

The header file(.h) holds the function declarations (prototypes) and variable declarations. It includes a template for our new MyClass, with one default constructor MyClass.h

#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass
{
   
  public:
    MyClass();
  protected:
  private:
};

#endif // MYCLASS_H

The implementation of the class and its methods go into the source file (.cpp).
Currently it includes just an empty constructor.
MyClass.cpp

#include "MyClass.h"
MyClass::MyClass()
{
   
//ctor
}

The #ifndef and #define statements in the header file will be discussed in the upcoming lessons.

Scope Resolution Operator

The double colon in the source file (.cpp) is called the scope resolution operator, and it’s used for the constructor definition:

#include "MyClass.h"
MyClass::MyClass()
{
   
//ctor
}

The scope resolution operator is used to define a particular class’ memeber functions, which have alreadly been declared. Remember that we defined the constructor protype in the header file.
So, basically, MyClass::MyClass() refers to the MyClass() member function - or, in this case, constructor - of the MyClass class.
To use our classes in our main, we need to include the header file.
For example, to use our newly created MyClass in main:

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

int main(){
   
	MyClass obj;
}

The header declares “what” a class (or whatever is being implemented) will do, while the cpp source file defines “how” it will perform those features.

Destructors

Remember constructors? They’re special member functions that are automatically called when an object is created.
Destructors are special functions, as well. They’re called when an object is destroyed or deleted.

Objects are destroyed when they go out of scope, or whenever the delete expression is applied to a pointer directed at an object of a class.

The name of a destructor will be exactly the same as the class, only prefixed with a tilde (~). A destructor can’t return a value or take any parameters.

class MyClass {
   
  public: 
    ~MyClass() {
   
     // some code
    }
};

Destructors can be very useful for releasing resources before coming out of the program. This can include closing files, releasing memory, and so on.
For example, let’s declare a destructor for our MyClass class, in its header file MyClass.h:

#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass
{
   
public:
    MyClass();
    ~MyClass();
};

#endif // MYCLASS_H

Declare a destructor for our MyClass class.

After declaring the destructor in the header file, we can write the implementation in the source file MyClass.cpp:

#include "MyClass.h"
#include<iostream>
using namespace std;
MyClass::MyClass()
{
   
cout<<"Constructor"<<endl;
}

MyClass::~MyClass(){
   
    cout<<"Destructor"<<endl;
}

Note that we included the header, so that we can use cout.

Since destructors can’t take parameters, they also can’t be overloaded.
Each class will have just one destructor.

Defining a destructor is not mandatory; if you don’t need one, you don’t have to define one.

Let’s return to our main.

// Constructor;Destructor;
#include<iostream>
#include<MyClass.h>
using namespace std;

int main(){
   
    MyClass obj;

    return 0;
}

We included the class’ header file and then created an object of that type.
This returns the following output:

Constructor
Destructor

When the program runs, it first creates the object and calls the constructor. The object is deleted and the destructor is called when the program’s execution is completed.

Remember that we printed “Constructor” from the constructor and “Desructor” from the destructor.

Selection Operator

#ifdef & #define

We created seprate header and source files for our class, which resulted in this header life.

#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass{
   
public:
MyClass();
protected:
private:
};
#endif //MYCLASS_H

ifndef stands for “if not defined”. The first pair of staments tells the program to define the MyClass header file if it has not been defined already.
endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值