Unit 1: Introduction to OOP and classes
OOPs using C++ notes · PTU syllabus (BSIT203/BSBC203)
On this page
- Unit summary
- The object-oriented programming approach
- Characteristics and benefits of OOP languages
- Bridging C and C++
- Structure of a C++ program
- Structures and unions in C++
- Class declaration: data members and member functions
- Private and public members, data hiding and encapsulation
- Key terms
- Quick revision
- Important questions
Unit summary
Object-oriented programming models software as interacting objects, and C++ adds this power to C. This unit covers the object-oriented approach, characteristics of OOP languages, bridging C and C++, structures and unions in C++, and class declaration — data members, member functions, private and public members, data hiding and encapsulation.
After this unit you can
- Explain the object-oriented approach and characteristics of OOP
- Compare C and C++
- Use structures and unions in C++
- Declare classes with data hiding and encapsulation
PTU syllabus topics
- Object-oriented programming approach
- characteristics of OOP languages
- bridging C and C++
- structures and unions in C++
- class declaration — data members
- member functions
- private/public members
- data hiding and encapsulation
Encapsulation
Data and functions bundled in a class
Abstraction
Show what, hide how
Inheritance
Reuse a base class
Polymorphism
One interface, many forms
Topic 1
The object-oriented programming approach
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
Topic 2
Characteristics and benefits of OOP languages
- 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
Bridging C and C++
Paradigm
Procedural
Multi-paradigm — procedural and object-oriented
Data security
Data freely accessible
Data hiding through access specifiers
I/O
printf, scanf
cin, cout (streams)
Features
Functions, structures
Classes, inheritance, polymorphism, overloading, templates, exceptions, namespaces
Memory
malloc, free
new, delete
- C++ is largely a superset of C — most C programs compile as C++ — and adds references, function overloading, default arguments and bool.
Topic 4
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.
Topic 5
Structures and unions in C++
- In C++ a struct can contain member functions and access specifiers; members are public by default (in a class they are private by default).
- Unions share memory among members; C++ unions can have member functions; anonymous unions allow direct member access.
cppstruct Point {
int x, y;
void show() { std::cout << "(" << x << ", " << y << ")\n"; }
};Topic 6
Class declaration: data members and member functions
A class has data members (variables) and member functions (methods). Member functions can be defined inside the class or outside using the scope resolution operator ::.
cppclass Account {
private:
int number;
double balance;
public:
void deposit(double amt); // declared here
void show() { cout << number << " " << balance; }
};
void Account::deposit(double amt) { // defined outside
balance += amt;
}Objects are declared like variables: Account a1, a2; and members are accessed with the dot operator: a1.show();.
Topic 7
Private and public members, data hiding and encapsulation
| Specifier | Accessible from |
|---|---|
| private | Only member functions of the same class (default in a class) |
| public | Anywhere the object is visible |
| protected | The class and its derived classes |
Keeping data private and providing public functions to use it is called data hiding.
Key terms
- Object
- Instance of a class with state and behaviour
- Class
- Blueprint defining data and functions
- Encapsulation
- Bundling data and functions and hiding details
- Data hiding
- Restricting direct access to data
- Access specifier
- Keyword controlling member access
Quick revision
- Objects, classes, encapsulation, abstraction, inheritance, polymorphism, dynamic binding, message passing.
- C vs C++; structure of a C++ program.
- struct members public by default; class members private by default; unions.
- Class declaration; member functions; private and public.
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.State four characteristics of OOP.
- Q2.Distinguish C and C++.
- Q3.Distinguish a struct and a class in C++.
- Q4.What is encapsulation?
- Q5.What is data hiding?
- Q6.Write the syntax of a class declaration.
Long-answer questions
- Q1.Explain the object-oriented approach and its characteristics.
- Q2.Compare C and C++ and explain the structure of a C++ program.
- Q3.Explain structures and unions in C++.
- Q4.Explain class declaration, data hiding and encapsulation with an example.
Stuck on this unit?
Message SBS on WhatsApp for help with OOPs using C++, or to ask about studying B.Sc IT at Synetic.
