Skip to content

abir-js/cpp-notes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Objects and Classes

In C++, a class is a user-defined data type that groups data (variables) and functions (methods) together. An object is an instance of a class that uses its properties and methods.

Classes support encapsulation, abstraction, and form the base of OOP.

Example:

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    void show() {
        cout << "Name: " << name << endl;
    }
};

int main() {
    Student s1;      // object
    s1.name = "Abir";
    s1.show();
    return 0;
}

Output: Name: Abir


Arrays

An array in C++ is a collection of elements of the same data type, stored in contiguous memory locations. It allows easy access using index numbers (starting from 0).

Syntax:

dataType arrayName[size];

Example:

#include <iostream>
using namespace std;

int main() {
    int marks[5] = {90, 85, 88, 92, 80};

    for(int i = 0; i < 5; i++)
        cout << marks[i] << " ";
    return 0;
}

Output: 90 85 88 92 80


Constructors and Destructors

A constructor is a special function that is automatically called when an object is created — used to initialize data. A destructor is automatically called when an object is destroyed — used to release resources.

Rules:

  • Same name as class
  • No return type
  • Destructor name starts with ~

Example:

#include <iostream>
using namespace std;

class Demo {
public:
    Demo() { cout << "Constructor called\n"; }
    ~Demo() { cout << "Destructor called\n"; }
};

int main() {
    Demo obj;  // Constructor runs automatically
    return 0;  // Destructor runs automatically
}

Output:

Constructor called  
Destructor called

Methods and Member Functions

Methods (also called member functions) are functions defined inside a class that operate on its data members.

They define behavior of objects.

Example:

#include <iostream>
using namespace std;

class Student {
public:
    string name;

    void display() {        // method
        cout << "Name: " << name << endl;
    }
};

int main() {
    Student s1;
    s1.name = "Abir";
    s1.display();           // calling method
    return 0;
}

Output: Name: Abir


Constructor Overloading and Method Overloading

Constructor overloading means having multiple constructors in the same class with different parameter lists. It allows objects to be initialized in different ways.

Example:

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int age;

    Student() {  // default constructor
        name = "Unknown";
        age = 0;
    }

    Student(string n, int a) {  // parameterized constructor
        name = n;
        age = a;
    }

    void show() {
        cout << name << " " << age << endl;
    }
};

int main() {
    Student s1;           // calls default
    Student s2("Abir", 20); // calls parameterized
    s1.show();
    s2.show();
    return 0;
}

Output:

Unknown 0  
Abir 20

Method overloading means having multiple functions with the same name but different parameters (type or number). It increases code readability and flexibility.

Example:

#include <iostream>
using namespace std;

class Math {
public:
    void add(int a, int b) {
        cout << a + b << endl;
    }
    void add(double a, double b) {
        cout << a + b << endl;
    }
};

int main() {
    Math m;
    m.add(5, 3);       // calls int version
    m.add(2.5, 1.5);   // calls double version
    return 0;
}

Output:

8  
4

Inheritance

Inheritance is an OOP concept where one class (child/derived) acquires properties and methods of another class (parent/base). It promotes code reusability.

Syntax:

class Derived : access Base { ... };

Example:

#include <iostream>
using namespace std;

class Animal {
public:
    void eat() { cout << "Eating\n"; }
};

class Dog : public Animal {
public:
    void bark() { cout << "Barking\n"; }
};

int main() {
    Dog d;
    d.eat();   // inherited
    d.bark();  // own method
    return 0;
}

Output:

Eating  
Barking

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published