Unit 1 of 4 · B.Sc IT Sem 2

Unit 1: Introduction to OOP and classes

OOPs using C++ notes · PTU syllabus (BSIT203/BSBC203)

3 min read7 topics10 exam questions
On this page
  1. Unit summary
  2. The object-oriented programming approach
  3. Characteristics and benefits of OOP languages
  4. Bridging C and C++
  5. Structure of a C++ program
  6. Structures and unions in C++
  7. Class declaration: data members and member functions
  8. Private and public members, data hiding and encapsulation
  9. Key terms
  10. Quick revision
  11. 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
ClassificationFour pillars of OOP
Object-oriented programming
  • Encapsulation

    Data and functions bundled in a class

  • Abstraction

    Show what, hide how

  • Inheritance

    Reuse a base class

  • Polymorphism

    One interface, many forms

1

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.

ClassificationBasic features of OOP
OOP
  • 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

2

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.
3

Topic 3

Bridging C and C++

ComparisonC vs C++
C
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.
4

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.
  • endl or "\n" ends a line.
  • A typical program has: include files, class declarations, member function definitions, and the main function.
5

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"; }
};
6

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();.

7

Topic 7

Private and public members, data hiding and encapsulation

SpecifierAccessible from
privateOnly member functions of the same class (default in a class)
publicAnywhere the object is visible
protectedThe 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

  1. Q1.State four characteristics of OOP.
  2. Q2.Distinguish C and C++.
  3. Q3.Distinguish a struct and a class in C++.
  4. Q4.What is encapsulation?
  5. Q5.What is data hiding?
  6. Q6.Write the syntax of a class declaration.

Long-answer questions

  1. Q1.Explain the object-oriented approach and its characteristics.
  2. Q2.Compare C and C++ and explain the structure of a C++ program.
  3. Q3.Explain structures and unions in C++.
  4. 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.

WhatsApp us