Unit 1 of 4 · M.Sc IT Sem 2

Unit 1: C++ fundamentals

Object Oriented Programming using C++ notes · PTU syllabus (PGCA1911)

3 min read11 topics10 exam questions
On this page
  1. Unit summary
  2. Introduction to OOP
  3. Basic features of OOP
  4. Procedural vs object-oriented languages
  5. Structure of a C++ program
  6. Compiling and executing a C++ program
  7. Input/output and assignment statements
  8. Constants, variables and data types
  9. Operators
  10. Control structures: if-else, nested if and switch
  11. Loops: while, do-while and for
  12. Jump statements: break, continue and goto
  13. Key terms
  14. Quick revision
  15. Important questions

Unit summary

C++ extends C with classes and objects while keeping C's speed. This unit covers an introduction to OOP and its basic features, the structure of a C++ program, compiling and executing programs, procedural versus object-oriented languages, input/output and assignment statements, constants, variables, operators and data types, and control structures — if-else, nested if, while, do-while, for and jump statements.

After this unit you can

  • Explain OOP and compare it with procedural programming
  • Write, compile and run C++ programs
  • Use data types, variables, constants, operators and I/O
  • Write decision, loop and jump statements

PTU syllabus topics

  • OOP introduction and basic features
  • C++ program structure
  • compiling and executing programs
  • procedural vs object-oriented languages
  • I/O and assignment statements
  • constants
  • variables
  • operators
  • data types
  • control structures (if-else, nested if, while, do-while, for, jump statements)
ComparisonProcedural vs object-oriented programming
Procedural
Object-oriented

Focus

Functions and steps

Objects with data and behaviour

Data

Often global, shared

Encapsulated in classes

Reuse

Copy functions

Inheritance and polymorphism

Examples

C

C++, Java

1

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.

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

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

Topic 3

Procedural vs object-oriented languages

ComparisonProcedural vs object-oriented
Procedural (C)
Object-oriented (C++)

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

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

Compiling and executing a C++ program

ProcessFrom source to output
  1. 1Write source

    prog.cpp

  2. 2Compile

    g++ prog.cpp -o prog

  3. 3Link

    Libraries joined to make an executable

  4. 4Run

    ./prog

  5. 5Debug

    Fix compile-time and run-time errors

6

Topic 6

Input/output and assignment statements

cpp#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    string name; int age; double fee;
    cout << "Enter name, age and fee: ";
    cin >> name >> age >> fee;                     // extraction operator
    int total = age;                                // assignment
    total += 5;                                     // compound assignment
    cout << "Name: " << name << '\n'
         << "Fee: " << fixed << setprecision(2) << fee << endl;   // insertion operator, manipulators
    return 0;
}
  • getline(cin, line) reads a whole line with spaces; setw(n) sets field width.
7

Topic 7

Constants, variables and data types

ComparisonC++ data types
Size (typical)
Example

int

4 bytes

int roll = 21;

float and double

4 and 8 bytes

double cgpa = 8.75;

char

1 byte

char grade = 'A';

bool

1 byte

bool passed = true;

Modifiers

short, long, signed, unsigned

unsigned long population;

Derived and user-defined

Arrays, pointers, references, structures, classes, enums

enum Day { MON, TUE };

  • Constants: literal constants (25, 3.14, 'x', "SBS"), const variables (const double PI = 3.14159;), constexpr (compile-time), #define macros (C style), and enum constants.
  • Variables can be declared anywhere before use; auto lets the compiler deduce the type; references (int &r = x;) are aliases.
8

Topic 8

Operators

Key termsC++ operators
Arithmetic
+ − × / % (multiplication written with the asterisk symbol)
Relational
== != < > <= >=
Logical
&&, the double-bar OR, !
Assignment
= += −= and others
Increment and decrement
++ and −− (prefix and postfix)
Bitwise
& ^ ~ << >> and the bar for OR
Conditional
condition ? a : b
C++-specific
:: scope resolution, new and delete, insertion << and extraction >>, member access . and ->
9

Topic 9

Control structures: if-else, nested if and switch

  • if: runs a block only if the condition is true.
  • if-else: chooses between two blocks.
  • Nested if / else-if ladder: tests several conditions in order.
  • switch: selects one of many cases by the value of an integer or character expression; each case usually ends with break.
cint day = 3;
switch (day) {
    case 1: printf("Monday"); break;
    case 2: printf("Tuesday"); break;
    case 3: printf("Wednesday"); break;
    default: printf("Invalid day");
}
Comparisonif-else ladder vs switch
if-else ladder
switch

Tests

Any condition, including ranges

Equality with constant values only

Data types

Any

int or char

Readability

Harder for many cases

Cleaner for many fixed cases

Fall-through

Not possible

Happens if break is missing

10

Topic 10

Loops: while, do-while and for

A loop repeats a block of statements while a condition holds.

Comparisonwhile vs do-while vs for
Condition checked
Runs at least once?

while

Before the body (entry-controlled)

No

do-while

After the body (exit-controlled)

Yes

for

Before the body; initialisation and update in one line

No

c/* Sum of first n natural numbers */
int n = 10, sum = 0;
for (int i = 1; i <= n; i++) {
    sum += i;
}
printf("Sum = %d", sum);   /* 55 */

Example

A menu program uses do-while, because the menu must be shown at least once before the user chooses to exit.

11

Topic 11

Jump statements: break, continue and goto

  • break ends the loop (or switch) immediately and jumps to the statement after it.
  • continue skips the rest of the current iteration and moves to the next one.

Example

Printing 1 to 10 but skipping 5: inside the loop, if (i == 5) continue;. Stopping at the first negative number in input: if (x < 0) break;.

  • goto: transfers control to a labelled statement — legal but discouraged because it makes programs hard to follow; occasionally used to exit deeply nested loops.

Key terms

Object-oriented programming
Programming organised around objects that combine data and functions
cin and cout
Standard input and output stream objects
Manipulator
Function such as setw or endl that formats stream output
const
Qualifier making a variable read-only
Jump statement
Statement transferring control — break, continue, goto, return

Quick revision

  • OOP features: class, object, encapsulation, abstraction, inheritance, polymorphism.
  • Procedural vs OOP; program structure; compile and link.
  • cin, cout, getline, manipulators; assignment.
  • Data types, modifiers, const, constexpr, auto, references; operators including ::, new, delete.
  • if, nested if, switch, while, do-while, for, break, continue, goto.

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 three features of OOP.
  2. Q2.Distinguish procedural and object-oriented languages.
  3. Q3.What is the role of the scope resolution operator?
  4. Q4.Distinguish const and #define.
  5. Q5.Distinguish while and do-while loops.
  6. Q6.What does continue do?

Long-answer questions

  1. Q1.Explain the basic features of OOP.
  2. Q2.Explain the structure of a C++ program and the compile–execute cycle.
  3. Q3.Explain data types, variables, constants and operators in C++.
  4. Q4.Explain control structures in C++ with programs.

Stuck on this unit?

Message SBS on WhatsApp for help with Object Oriented Programming using C++, or to ask about studying M.Sc IT at Synetic.

WhatsApp us