Access Modifier
public : accessible to anyone
protected : accessible from child classes
private : accessible only from the class (not the object)
The default for access permissions is private.
class SomeClass
{
public:
int PublicMember;
protected:
int mProtectedMember; // m for member variables
private:
int mPrivateMember1;
int mPrivateMember2;
};
Create and Delete an Object
// create in stack memory (fast)
Vector a;
// create in heap memory (slow)
Vector* b = new Vector(); // bad example (RAII - Resource Acquisition Is Initialization)
Object array:
// create 10 Vector objects in heap
Vector* list = new Vector[10];
// create 10 pointers in heap
Vector** list = new Vector*[10];
Vector* a = new Vector;
Vector* list = new Vector[10];
// ...
delete a; // released immediately
a = NULL; // not required
delete[] list;
list = NULL; // not required
It is essential to use delete after using new to prevent memory leaks.
Initialize Member Variables
class Vector
{
private:
int mX;
int mY;
};
Vector a;
Vector* b = new Vector();
When an object is created, the values of the member variables are not initialized, but the values that were stored in memory space before are used, for performance.
Constructor
class Vector
{
public:
// constructor without a parameter
Vector()
{
// assignment
mX = 0;
mY = 0;
}
private:
int mX;
int mY;
};
Initializer List
class Vector
{
public:
Vector()
: mX(0) // : start an initializer list
, mY(0)
{
}
private:
int mX;
int mY;
};
Member variables are initialized without assignment, so constants and reference variables can also be initialized.
class X
{
const int mNameSize;
AnotherClass& mAnother;
X(AnotherClass& another)
: mNameSize(20)
, mAnother(another)
{
}
};
Better Vector class:
// vector.h
class Vector // define a class
{
public:
Vector();
Vector(int x, int y);
private:
int mX;
int mY;
};
// vector.cpp
Vector::Vector() // implement a class
: mX(0)
, mY(0)
{
}
Vector::Vector(int x, int y)
: mX(x)
, mY(y)
{
}
Default Constructor
A default constructor takes no parameters. If the class does not have a constructor, the compiler automatically creates a default constructor. This constructor does not initialize member variables. However, it calls the constructor for all embedded objects.
What the Compiler Does
case 1: Vector class does not have a constructor
// Vector.h
class Vector
{
private:
int mX;
int mY;
};
// Vector.obj
class Vector
{
public:
Vector() {} // what the compiler created
private:
int mX;
int mY;
};
case 2: Vector class has a constructor
// Vector.h
class Vector
{
public:
Vector(int x, int y);
private:
int mX;
int mY;
};
// Vector.obj
class Vector
{
public:
Vector(int x, int y); // the compiler did not create anything
private:
int mX;
int mY;
};
Constructor Overloading
It's possible to create multiple constructors. They have the same names, but the number and type of their arguments are different.
Destructor
// vector.h
class Vector
{
public:
~Vector();
private:
int mX;
int mY;
};
// vector.cpp
Vector::~Vector()
{
}
The destructor is called when the object is cleared. C ++ classes can dynamically allocate memory within them, so in such cases the destructor must release the memory directly.
*Virtual Destructor
Dynamic memory allocation within a class:
// MyString.h
class MyString
{
public:
MyString();
~MyString();
private:
char* mChars;
int mLength;
int mCapacity;
};
// MyString.cpp
MyString::MyString()
: mLength(0)
, mCapacity(15)
{
mChars = new char[mCapacity + 1];
}
MyString::~MyString()
{
delete[] mChars;
// mCapacity = 0;
// mChars = NULL;
}
// Main.cpp
void Foo()
{
MyString name;
// ...
} // name disappears, destructor is called
Const Member Function(Method)
// Vector.h
class Vector
{
public:
void SetX(int x);
void SetY(int y);
int GetX() const;// const method. nothing in the object is changed
int GetY() const;
void Add(const Vector& other);
private:
int mX;
int mY;
};
It prevents member variables from changing.
int GetX() const
{
return mX; // OK
}
void AddConst(const Vector& other) const
{
mX = mX + other.mX; // compile error
mY = mY + other.mY; // compile error
}
Struct vs. Class
Default access permission of a struct is public. Of a class is private.
Standard
In c ++, structures can be written as classes, but structures must be written in c style.
Structures should be data only.
- Do not write a user-defined constructor or destructor.
- Do not create private / protected member variables that are not static.
- Do not include a virtual function.
Structures are memory-copyable. You can use memcpy( ) to copy a struct as char[ ] or vice versa.
'College Study > C++ (ENG)' 카테고리의 다른 글
[C++] Object-Oriented Programming: Inheritance (ENG) (0) | 2022.01.02 |
---|---|
[C++] Object-Oriented Programming : Overloading (ENG) (0) | 2022.01.02 |
[C++] File I/O (ENG) (0) | 2022.01.02 |
[C++] File I/O (ENG) (0) | 2022.01.02 |
[C++] String (ENG) (0) | 2021.12.05 |
댓글