Subject: Object-Oriented Programming with C++
For: Diploma in Computer Engineering (2nd Year)
Based on: Official Question Bank (Units 1–3)
OOP = Object-Oriented Programming. It uses objects (instances of classes) to model real-world entities. Focuses on data security, reusability, and modularity.
Feature | OOP | POP |
---|---|---|
Approach | Bottom-up | Top-down |
Data & Functions | Combined (in objects) | Separate |
Inheritance | Yes | No |
Access Control | (public/private) | No |
Examples | C++, Java | C, BASIC |
Specifier | Access |
---|---|
public |
Anywhere |
private |
Only in class |
protected |
In class & derived classes |
private
members cannot be accessed in main()
.
Used to:
int x = 10;
int main() {
int x = 20;
cout << ::x; // prints 10
}
Function expanded at compile time for speed. Use inline
keyword.
inline int cube(int a) {
return a * a * a;
}
Use for short, frequently called functions.
Same name, different parameters (number or type). Not by return type.
class Cal {
public:
int add(int a, int b) { return a+b; }
int add(int a, int b, int c) { return a+b+c; }
};
Non-member function that can access private
members using friend
.
class Test {
int a;
public:
friend void show(Test t);
};
void show(Test t) {
cout << t.a; // OK!
}
Blueprint (class Student {};
)
Instance (Student s1;
)
An instance of a class = object created at runtime.
Special function with same name as class, called automatically when object is created. No return type.
class Student {
public:
int id;
string name;
Student(int i, string n) {
id = i;
name = n;
}
};
// Usage: Student s(101, "Rahul");
#include <iostream>
using namespace std;
class Student {
public:
int id;
string name;
float salary;
Student(int i, string n, float s) {
id = i;
name = n;
salary = s;
}
void display() {
cout << id << " " << name << " " << salary << endl;
}
};
int main() {
Student s1(101, "Rahul", 50000);
s1.display();
return 0;
}
Write this in exam — covers Q25, Q27, and constructor use.
Underline keywords: private
,
friend
,
inline
,
::
For comparisons (OOP vs POP, access specifiers) to organize information clearly.
Even 4 lines with comments = full marks. Focus on key concepts.
Spend 1 min per 2 marks — don't over-write answers.
If blank, write something — definition, syntax, or example.
Focus on A PIE (Abstraction, Polymorphism, Inheritance, Encapsulation).