C++ Tutorial menu

C++ Classes and Objects

Classes let you group data and the functions that work on it into one type. Objects are the real values made from a class.

A Simple Class

Example

cpp
#include <iostream>
#include <string>
using namespace std;

class Phone {
public:
  string brand;
  int price;

  void show() {
    cout << brand << " costs Rs " << price << "\n";
  }
};

int main() {
  Phone p1;
  p1.brand = "Infinix";
  p1.price = 32000;

  Phone p2;
  p2.brand = "Samsung";
  p2.price = 65000;

  p1.show();
  p2.show();
  return 0;
}
Output
Infinix costs Rs 32000
Samsung costs Rs 65000
TermMeaning
ClassA blueprint, like Phone
ObjectA real item made from the class, like p1
Member variableData inside the class, like brand
Member functionA function inside the class, like show()

Note the semicolon after the closing brace of the class. It is easy to forget.

Constructors

A constructor runs automatically when an object is created. It has the class name and no return type:

Example

cpp
#include <iostream>
#include <string>
using namespace std;

class Student {
public:
  string name;
  int marks;

  Student(string n, int m) {
    name = n;
    marks = m;
  }

  string result() {
    return marks >= 50 ? "Pass" : "Fail";
  }
};

int main() {
  Student a("Hina", 78);
  Student b("Kamran", 42);
  cout << a.name << ": " << a.result() << "\n";
  cout << b.name << ": " << b.result() << "\n";
  return 0;
}
Output
Hina: Pass
Kamran: Fail

private and public

  • public members can be used from anywhere
  • private members can only be used inside the class

Keeping data private and giving access through functions is called encapsulation. It protects the data from wrong values:

Example

cpp
#include <iostream>
using namespace std;

class Wallet {
private:
  int balance = 0;

public:
  void add(int amount) {
    if (amount > 0) balance += amount;
  }
  bool pay(int amount) {
    if (amount > balance) return false;
    balance -= amount;
    return true;
  }
  int getBalance() const { return balance; }
};

int main() {
  Wallet w;
  w.add(2000);
  w.pay(750);
  cout << boolalpha << w.pay(5000) << "\n";
  cout << w.getBalance() << "\n";
  return 0;
}
Output
false
1250

In a class, members are private unless you write public:. In a struct, they are public by default. Otherwise the two are almost the same.

Inheritance

A class can inherit from another class and reuse its members:

Example

cpp
#include <iostream>
#include <string>
using namespace std;

class Vehicle {
public:
  string name;
  void start() { cout << name << " started\n"; }
};

class Rickshaw : public Vehicle {
public:
  int wheels = 3;
};

int main() {
  Rickshaw r;
  r.name = "Qingqi";
  r.start();
  cout << "Wheels: " << r.wheels << "\n";
  return 0;
}
Output
Qingqi started
Wheels: 3

Rickshaw gets name and start() from Vehicle, and adds its own wheels.

Exercise

Exercise

In a C++ class, what is the default access level of members if you do not write public or private?

Learn C++ with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp