본문 바로가기

College Study/C++ (ENG)23

[C++] Static Keyword (ENG) static and extern static keyword makes global variables with limited scope. Scope of the file, the namespace, the class, and the function. extern keyword allows access to global variables in other files. // ExternTest.h extern int globalValue; void IncreaseValue(); // ExternTest.c #include "ExternTest.h" int globalValue = 2; // if this line contains static keyword, the main functions cause linke.. 2022. 1. 2.
[C++] Inline Functions (ENG) Inline Functions Creating new functions is not always good. It is slow, not optimized for the CPU cache. Sometimes, functions are good for readability. But if the calculations are simple, the overheads of calling the functions are burdensome. So that's why you should use inline functions. // Member Inline Function // Animal.h class Animal { public: Animal(int age); inline int GetAge() const; } i.. 2022. 1. 2.
[C++] Casting (ENG) C-style Casting C-style Casting is an implicit casting. int score = (int)someVariable; static_cast For Values: static_cast converts two numeric types. It tries to keep the same value between before and after the conversion. So the binary representations can be different. int number1 = 3; // 0000 0000 0000 0011 short number2 = static_cast(number1); // 0000 0011 float number1 = 3.f; // 0100 0000 0.. 2022. 1. 2.
[C++] Object-Oriented Programming: Interface (ENG) Multiple Inheritance // Faculty.h class Faculty { }; // Student.h class Student { }; // TA.h class TA : public Student, public Faculty { }; // Main.cpp TA* myTA = new TA(); The constructors are called in the order of the parent class in the child class declaration. (Regardless of the order of the initialiation list.) problem 1: Compiler doesn't know which function is supposed to be called. // Ma.. 2022. 1. 2.
[C++] Object-Oriented Programming: Polymorphism (ENG) Memory of the Member Function Member functions are allocated to the memory only once while compliling. Function Overriding // Animal.h class Animal { public: void Speak(); } // Animal.cpp void Animal::Speak() { std::cout 2022. 1. 2.
[C++] Object-Oriented Programming: Inheritance (ENG) Difference between Java and C++ // Java public class Cat extends Animal { private String Name; public Cat(int age, String name) { super(age); // calls Animal(int age) Name = name; } } // C++ class Cat : public Animal { public: Cat(int age, const char* name); private: char* mName; }; Cat::Cat(int age, const char* name) : Animal(age) { size_t size = string(name) +1; mName = new char[size]; strcpy(.. 2022. 1. 2.
[C++] Object-Oriented Programming : Overloading (ENG) Copy Constructor How to write a copy constructor: ( const & ); // Vector.h class Vector { public: Vector(const Vector& other); // copy constructor private: int mX; int mY; }; // Vector.cpp Vector::Vector(const Vector& other) : mX(other.mX) , mY(other.mY) { } // Main.cpp Vector a; // call the constructor with no parameter Vector b(a); // call the copy oconstructor There is also an implicit copy c.. 2022. 1. 2.
[C++] Object-Oriented Programming : Class (ENG) 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 m.. 2022. 1. 2.
[C++] File I/O (ENG) File Stream ifstream : input file ofstream : output file fstream : input and output file We can use operator and a manipulator in a file stream. // read only open ifstream fin; // an object fin.open("helloWorld.txt"); // write only open ofstream fout; fout.open("helloWorld.txt"); // read and write fstream fs; fs.open("helloWorld.txt"); open( ) Every stream has open( ) method. fin.open("HelloWorl.. 2022. 1. 2.