관리 메뉴

A seeker after truth

sololearn C++ More On Classes 배운 내용 메모 본문

Programming Language/C++

sololearn C++ More On Classes 배운 내용 메모

dr.meteor 2019. 11. 18. 20:42

1. separate files for a class

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

MyClass.h

#ifndef MYCLASS_H
#define MYCLASS_H

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

#endif // MYCLASS_H

MyClass.cpp

#include "MyClass.h"

MyClass::MyClass()
{
   //ctor
}

 

- 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' member functions, which have already been declared. Remember that we defined the constructor prototype in the header file.

To use our classes in our main, we need to include the header file.

#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.

 

 

2. Destructors

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. Destructors can be very useful for releasing resources before coming out of the program. This can include closing files, releasing memory, and so on.

MyClass.h

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

MyClass.cpp:

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

MyClass::MyClass()
{
  cout<<"Constructor"<<endl;
}

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

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

 

 

3. Selection Operator

1)

- ifndef stands for "if not defined". The first pair of statements tells the program to define the MyClass header file if it has not been defined already.
- endif ends the condition.

 

2) member func

MyClass.h

class MyClass
{
  public:
   MyClass();
   void myPrint();
};

MyClass.cpp

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

MyClass::MyClass() {
}

void MyClass::myPrint() {
  cout <<"Hello"<<endl;
}

Since myPrint() is a regular member function, it's necessary to specify its return type in both the declaration and the definition.

 

3) The arrow member selection operator (->) is used to access an object's members with a pointer.

MyClass obj;
MyClass *ptr = &obj;
ptr->myPrint();

When working with an object, use the dot member selection operator (.).
When working with a pointer to the object, use the arrow member selection operator (->).

 

 

4. const objects

- Once a const class object has been initialized via the constructor, you cannot modify the object's member variables.

When you've used const to declare an object, you can't change its data members during the object's lifetime.

- Only non-const objects can call non-const functions.

- For const member functions that are defined outside of the class definition, the const keyword must be used on both the function prototype and definition. For example:
MyClass.h

class MyClass
{
  public:
    void myPrint() const;
};

MyClass.cpp

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

void MyClass::myPrint() const {
  cout <<"Hello"<<endl;
}

Now the myPrint() function is a constant member function. As such, it can be called by our constant object:

int main() {
  const MyClass obj;
  obj.myPrint();
}
// Outputs "Hello"

- constants are variables that cannot be changed, and that all const variables must be initialized at time of creation. C++ provides a handy syntax for initializing members of the class called the member initializer list (also called a constructor initializer).

class MyClass {
  public:
   MyClass(int a, int b) {
    regVar = a;
    constVar = b;
   }
  private:
    int regVar;
    const int constVar;
};

const에 b값을 대입하는 부분 때문에 위 코드는 에러가 난다. 아래와 같이 생성자를 초기화하는 문법을 사용하는 것이 맞다.

class MyClass {
 public:
  MyClass(int a, int b)
  : regVar(a), constVar(b)
  {
  }
 private:
  int regVar;
  const int constVar;
};

Note that in the syntax, the initialization list follows the constructor parameters. The list begins with a colon (:), and then lists each variable to be initialized, along with the value for that variable, with a comma to separate them. Also, the initialization list does not end with a semicolon.

 

 

5. Friend functions

Normally, private members of a class cannot be accessed from outside of that class.
However, declaring a non-member function as a friend of a class allows it to access the class' private members. This is accomplished by including a declaration of this external function within the class, and preceding it with the keyword friend.
In the example below, someFunc(), which is not a member function of the class, is a friend of MyClass and can access its private members.

class MyClass {
 public:
  MyClass() {
   regVar = 0;
  }
 private:
  int regVar;
    
  friend void someFunc(MyClass &obj);
};

Note that when passing an object to the function, we need to pass it by reference, using the & operator.

The function someFunc() is defined as a regular function outside the class. It takes an object of type MyClass as its parameter, and is able to access the private data members of that object.

To make its members accessible, the class has to declare the function as a friend in its definition. You cannot "make" a function a friend to a class without the class "giving away" its friendship to that function.

class MyClass {
 public:
  MyClass() {
   regVar = 0;
  }
 private:
  int regVar;
    
 friend void someFunc(MyClass &obj);
};

void someFunc(MyClass &obj) {
  obj.regVar = 42;
  cout << obj.regVar;
}

 

 

6. The This keyword

Every object in C++ has access to its own address through an important pointer called the this pointer. Inside a member function this may be used to refer to the invoking object. 

Note that only member functions have a this pointer.

 

You may be wondering why it's necessary to use the this keyword, when you have the option of directly specifying the variable.
The this keyword has an important role in operator overloading, which will be covered in the following lesson.

 

7. Operator Overloading

 

class MyClass {
 public:
  int var;
  MyClass() {}
  MyClass(int a)
  : var(a) { }

  MyClass operator+(MyClass &obj) {
  }
};