Unit 1: Principles of OOP
Object Oriented Programming using C++ notes · PTU syllabus (UGSEC2503)
On this page
Unit summary
Object-oriented programming (OOP) organises a program around objects — bundles of data and the functions that work on that data — instead of around a sequence of procedures. C++ adds OOP features to C. This unit introduces the principles of OOP, the structure of a C++ program and how it differs from procedural programming.
After this unit you can
- Explain the basic features of OOP
- Write, compile and run a simple C++ program
- Use cin, cout and namespaces
- Compare procedural and object-oriented programming
PTU syllabus topics
- Introduction to OOP and its basic features
- C++ program structure
- compiling and executing a C++ program
- procedural vs object-oriented languages
Topic 1
Introduction to OOP
In procedural programming (C), a program is a list of functions working on shared data. As programs grow, any function can change any data, which makes them hard to maintain. OOP solves this by combining data and functions into objects and controlling access to the data.
Class and object
A class is a blueprint; an object is an instance
Encapsulation
Data and functions wrapped in one unit
Abstraction
Showing only essential details
Inheritance
A new class reuses an existing class
Polymorphism
One name, many forms
Dynamic binding and message passing
Call resolved at run time; objects communicate by calling functions
Example
A real-world object as a class: class Student — data members: roll, name, marks; member functions: input(), display(), result(). Each student in the class is an object, an instance with its own values: s1 (101, Asha, 82) and s2 (102, Ravi, 67). The data and the functions that work on it are bundled together (encapsulation), and marks can be made private so only result() can read them (data hiding).
Topic 2
Benefits of OOP
- Reusability through inheritance — write once, extend many times.
- Data security through encapsulation and data hiding.
- Easier maintenance and modelling of real-world entities (Student, Account, Car).
- Extensibility — new features can be added with little change to existing code.
Topic 3
Structure of a C++ program
cpp#include <iostream> // header for input/output
using namespace std; // use names like cout directly
class Greeting { // class definition
public:
void show() { cout << "Welcome to SBS" << endl; }
};
int main() { // execution starts here
Greeting g; // object
g.show();
return 0;
}cout <<(insertion operator) prints;cin >>(extraction operator) reads input.endlor"\n"ends a line.- A typical program has: include files, class declarations, member function definitions, and the main function.
Example
Tracing input and output: int a, b; cin >> a >> b; cout << "Sum = " << a + b << endl; With the input 7 5 (separated by a space or Enter), the program prints Sum = 12. The >> operator extracts values from the input stream and the << operator inserts values into the output stream, and both can be chained.
Exam tip
A one-line way to separate the two languages: C++ is C plus classes — everything valid in C (almost) still works, and cout/cin replace printf/scanf without needing format specifiers like %d.
Topic 4
Compiling and executing a C++ program
A C++ program is written as source code, translated into machine code by a compiler, joined with library code by a linker, and then run.
- 1Write source
prog.cpp
- 2Compile
g++ prog.cpp -o prog
- 3Link
Libraries joined to make an executable
- 4Run
./prog
- 5Debug
Fix compile-time and run-time errors
Example
From file to output: save the code as hello.cpp. The command g++ hello.cpp -o hello compiles and links it into an executable named hello. The command ./hello runs it. A missing semicolon stops the compile step (compile-time error); dividing by zero fails while running (run-time error).
Exam tip
Syntax errors are caught by the compiler. Logical errors compile and run but give the wrong answer.
Topic 5
Procedural vs object-oriented languages
Procedural programming organises a program as a set of functions that act on data. Object-oriented programming organises it as objects that hold both data and the functions that work on that data.
Focus
Functions and steps
Objects: data plus behaviour
Data
Often global and shared freely
Hidden inside classes
Approach
Top-down
Bottom-up
Reuse
Copying functions
Inheritance
Security
Less
More through access specifiers
Examples
C, Pascal
C++, Java, Python
Example
A bank account both ways: in C you write a variable balance and separate functions deposit(balance, amt) and withdraw(balance, amt); any function can change the balance. In C++ you write a class Account with a private balance and public deposit() and withdraw(); the balance can change only through those functions.
Key terms
- Object
- An instance of a class with its own data
- Class
- A user-defined blueprint describing data and functions
- Encapsulation
- Binding data and functions into one unit
- Abstraction
- Exposing only essential details
- Polymorphism
- The ability of one name to take many forms
Quick revision
- OOP features: class/object, encapsulation, abstraction, inheritance, polymorphism, dynamic binding.
- cout << prints; cin >> reads.
- Procedural is top-down; OOP is bottom-up.
- C++ = C + classes and OOP features.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Define object-oriented programming.
- Q2.Differentiate between a class and an object.
- Q3.What is encapsulation?
- Q4.What are cin and cout?
- Q5.Differentiate between procedural and object-oriented programming.
- Q6.List three benefits of OOP.
Long-answer questions
- Q1.Explain the basic features of object-oriented programming with examples.
- Q2.Explain the structure of a C++ program with a sample program.
- Q3.Compare procedural and object-oriented programming languages in detail.
Stuck on this unit?
Message SBS on WhatsApp for help with Object Oriented Programming using C++, or to ask about studying BCA at Synetic.
