728x90

Nekoder


Namespace


In C++, a namespace is a declarative region that provides a scope to the identifiers inside it.

 
C++ uses namespaces to organize code and prevent naming collisions, especially when integrating third-party libraries or large codebases.
 

Click here to view the example code .▼ 

더보기
#include <iostream>

namespace mytools {
    void greet() {
        std::cout << "Hello from mytools!" << std::endl;
    }
}

int main() {
    mytools::greet(); // fully qualified name
}

 
Using directives and declarations
 

Click here to view the example code .▼ 

더보기
using namespace std;

int main() {
    cout << "This works without std:: prefix." << endl;
}

While using namespace std; may simplify code in small examples, it is discouraged in headers or large projects due to potential ambiguity.
 
Key points

  • Prevents naming conflicts by scoping identifiers
  • Use :: to access elements within a namespace
  • Prefer qualified names or using declarations over using namespace globally

References

namespace : 🔗 https://en.cppreference.com/w/cpp/language/namespace

 
 
 
 


Reference


A reference is an alias for another object. Once initialized, it cannot be reseated.

 
 

A reference provides a second name for an existing object. It must be initialized at declaration and cannot be changed to refer to a different object afterward.
 

Click here to view the example code .▼ 

더보기
int a = 10;
int& ref = a;
ref = 20;
std::cout << a << std::endl; // Output: 20

The reference ref is simply another name for a.

 

Function parameters

References are often used in function parameters to avoid copying and allow direct modification of arguments.

void increment(int& n) {
    n += 1;
}

int main() {
    int value = 5;
    increment(value);
    std::cout << value << std::endl; // Output: 6
}

 

Const references

A reference to a const object allows read-only access to the referred value.

void printMessage(const std::string& msg) {
    std::cout << msg << std::endl;
}
  • const & is essential when passing large objects efficiently without allowing modification.
  • Also allows binding to rvalues (temporary values), which normal references do not.

References

reference : 🔗 https://en.cppreference.com/w/cpp/language/reference

 
 
 


Object-Oriented Programming


Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which contain data and methods to operate on that data.

 
OOP introduces a new way of thinking:

  • Procedural: "What will happen, and in what order?"
  • Object-Oriented: "Who will do what?"

Key Concepts:

  • Encapsulation: Bundling data and functions together.
  • Abstraction: Hiding internal complexity and exposing only necessary parts.
  • Inheritance: Deriving new classes from existing ones.
  • Polymorphism: Using one interface for different types.

Class and Object Basics

A class is a user-defined data type that acts as a blueprint for creating objects.
An object is an instance of a class.

Click here to view the example code .▼ 

더보기
class Animal {
private:
    int food;
    int weight;

public:
    void setAnimal(int f, int w);
    void increaseFood(int amount);
    void showStatus();
};
Animal dog;
dog.setAnimal(100, 30);
dog.increaseFood(10);
dog.showStatus();
 

 


Constructor, Destructor, and Overloading

 
Constructor

A constructor is a special function that gets called automatically when an object is created.

Animal::Animal(int f, int w) {
    food = f;
    weight = w;
}

 

Destructor

A destructor is called when an object is destroyed. Useful for memory cleanup.

Animal::~Animal() {
    // Clean up resources
}
 

Overloading

 
Function overloading allows multiple functions with the same name but different parameters.

void print(int x);
void print(std::string s);

 


Advanced Class Features

Initializer List

Animal::Animal(int f, int w) : food(f), weight(w) {}

Used to initialize members efficiently (especially const and reference members).
 
Static Members

class Counter {
public:
    static int count;
    Counter() { count++; }
};

Shared across all instances of the class.
 

 

this Pointer

this->weight += 1;

 
Inside member functions, this points to the current object.
 

 

Returning Reference

int& getWeight() { return weight; }

 
A function can return a reference to modify original data.
 

 

Const Member Function

int getWeight() const;

 
A member function declared with const cannot modify the object.
 
 
 
References
 

Class : 🔗  https://en.cppreference.com/w/cpp/language/classes
Constructor: 🔗  https://en.cppreference.com/w/cpp/language/constructor
Destructor:  🔗  https://en.cppreference.com/w/cpp/language/destructor
Function overloading:  🔗  https://en.cppreference.com/w/cpp/language/overload_resolution
Static members:  🔗  https://en.cppreference.com/w/cpp/language/static
this pointer:  🔗  https://en.cppreference.com/w/cpp/language/this
Reference initialization :  🔗  https://en.cppreference.com/w/cpp/language/reference_initialization
Const member function:  🔗  https://en.cppreference.com/w/cpp/language/cv
 

728x90

'( * )Engineering > 🌜C/C++' 카테고리의 다른 글

C++ : Standard I/O Library  (0) 2025.04.13
C++ : Operator Overloading  (0) 2025.04.01
C : const / break 없는 switch  (0) 2025.03.31
C : void / main / 동적 할당 / list / inline  (0) 2025.03.16
C : 포인터 / 컴파일  (0) 2025.03.13