Unit 1: C++ fundamentals
Object Oriented Programming using C++ notes · PTU syllabus (PGCA1911)
On this page
- Unit summary
- Introduction to OOP
- Basic features of OOP
- Procedural vs object-oriented languages
- Structure of a C++ program
- Compiling and executing a C++ program
- Input/output and assignment statements
- Constants, variables and data types
- Operators
- Control structures: if-else, nested if and switch
- Loops: while, do-while and for
- Jump statements: break, continue and goto
- Key terms
- Quick revision
- 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)
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
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
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.
Topic 3
Procedural vs object-oriented languages
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
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
Compiling and executing a C++ program
- 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
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.
Topic 7
Constants, variables and data types
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.
Topic 8
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 ->
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");
}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
Topic 10
Loops: while, do-while and for
A loop repeats a block of statements while a condition holds.
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.
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
- Q1.State three features of OOP.
- Q2.Distinguish procedural and object-oriented languages.
- Q3.What is the role of the scope resolution operator?
- Q4.Distinguish const and #define.
- Q5.Distinguish while and do-while loops.
- Q6.What does continue do?
Long-answer questions
- Q1.Explain the basic features of OOP.
- Q2.Explain the structure of a C++ program and the compile–execute cycle.
- Q3.Explain data types, variables, constants and operators in C++.
- 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.
