C++ Syllabus
C++ Syllabus
CO-PSO Mapping:
CO / PSO PSO1 PSO2
CO1 2
CO2 2
CO3 2
CO4 2
CO5 2
Practice Modules:
1. Fundamental Concepts of C++
Recursion: Write a program to find the factorial of a number using recursion.
Call by Value and Call by Reference: Develop a C++ program to demonstrate both.
2. Scope Resolution and Namespaces
Use global vs local variables, scope resolution operator, custom namespaces.
Program to demonstrate scope resolution and use of namespaces.
3. Inline Functions
Definition, syntax, benefits, when not to use.
Create a program illustrating inline functions.
4. Classes, Objects, and Encapsulation
Model a Bank Account with data members and functions (deposit, withdraw, display).
Access Specifiers: Public vs Private – create a sample program.
this Pointer: Use to resolve naming conflicts.
5. Function Overloading
Define multiple functions with same name but different parameters.
Use of default arguments.
Friend Function to access private data.
6. Constructors and Destructors
Create a class demonstrating default constructor and destructor.
Constructor overloading and copy constructor with examples.
7. Operator Overloading
Unary and binary operator overloading using member functions and friend functions.
8. Inheritance Types
Single, multiple, multilevel, hierarchical, hybrid inheritance – one program each.
9. Constructor & Destructor Execution Order
Demonstrate chaining of constructors and destructors in inheritance.
10. Pointers and Virtual Base Class
Pointers to objects and accessing members.
Resolve diamond problem using virtual base class.
11. Virtual Functions & Runtime Polymorphism
Demonstrate function overriding and dynamic dispatch with base class pointers.
12. Templates
Function templates and class templates – create programs for both.
13. Exception Handling
Try, catch, throw mechanisms.
Multiple catch blocks to handle different exceptions.
14. STL Containers
List and Vector: insertion, deletion, traversal.
Deque: implement and demonstrate basic operations.
Map: perform insertion, deletion, access, and searching.
III. Functions
A. Introduction to Functions
1. Purpose: Code reusability, modularity.
2. Function Declaration/Prototype.
3. Function Definition.
4. Function Call.
B. Function Parameters
1. Pass by Value.
2. Pass by Reference.
3. Pass by Pointer.
C. Return Values.
D. Function Overloading.
E. Inline Functions.
F. Default Arguments.
G. Recursion.
🔹 1. Recursion
✅ What is Recursion?
Recursion is a process in which a function calls itself.
It is used to solve problems that can be broken into smaller sub-problems.
✅ Syntax:
returnType functionName(parameters) {
if (base condition)
return result;
else
return recursive call;
}
✅ Example: Factorial using Recursion
int main() {
int number;
return 0;
}
📘 Explanation:
🔁 How recursion works (for input 5):
factorial(5)
= 5 * factorial(4)
= 5 * 4 * factorial(3)
= 5 * 4 * 3 * factorial(2)
= 5 * 4 * 3 * 2 * factorial(1)
= 5 * 4 * 3 * 2 * 1
= 120
The function keeps calling itself until it reaches the base case (n == 1).
Then it returns back through the call stack, multiplying values step by step.
🎯 Sample Output:
Enter a positive integer: 5
Factorial of 5 is: 120
✅ Call by Value
🔸 Definition:
A copy of the variable is passed to the function.
Changes made inside the function do not affect the original value.
🔸 Syntax & Example:
void modify(int x) {
x = x + 5;
}
int main() {
int a = 10;
modify(a);
cout << a; // Output: 10 (no change)
}
✅ Call by Reference
🔸 Definition:
The original variable is passed using reference (&).
Changes made inside the function affect the original variable.
🔸 Syntax & Example:
void modify(int &x) {
x = x + 5;
}
int main() {
int a = 10;
modify(a);
cout << a; // Output: 15 (value changed)
}
🧠 Comparison Table:
Feature Call by Value Call by Reference
What is passed? Copy of the value Address/reference
Changes affect main? ❌ No ✅ Yes
Syntax void func(int x) void func(int &x)
int main() {
int a = 5;
int b = 5;
return 0;
}
📘 Explanation:
🔹 Call by Value
Only a copy of the variable is passed to the function.
Changes inside the function do not affect the original variable.
🔹 Call by Reference
The actual memory address (reference) is passed to the function.
Changes inside the function do affect the original variable.
🎯 Sample Output:
Before callByValue, a = 5
Inside callByValue function, x = 15
After callByValue, a = 5
Before callByReference, b = 5
Inside callByReference function, y = 15
After callByReference, b = 15
int main() {
int x = 20; // local variable
std::cout << "Local x: " << x << std::endl;
std::cout << "Global x: " << ::x << std::endl; // scope resolution to access global x
return 0;
}
2. Function Defined Outside the Class:
#include <iostream>
class Test {
public:
void display();
};
void Test::display() {
std::cout << "Hello from Test class!" << std::endl;
}
int main() {
Test t;
t.display(); // Call the member function
return 0;
}
Namespaces
A namespace is used to organize code and avoid naming conflicts in large programs.
Syntax:
namespace MyNamespace {
int value = 42;
void show() {
std::cout << "Value: " << value << std::endl;
}
}
Accessing Namespaces:
1. Using Scope Resolution:
#include <iostream>
void show() {
std::cout << "Value: " << value << std::endl;
}
}
int main() {
std::cout << MyNamespace::value << std::endl;
MyNamespace::show();
return 0;
}
2. Using using Keyword:
#include <iostream>
void show() {
std::cout << "Value from show(): " << value << std::endl;
}
}
int main() {
std::cout << value << std::endl; // Now value is in scope
show(); // Now show() is in scope
return 0;
}
Summary Table:
Feature Description
:: Operator Resolves scope to access global/class/namespace members
Namespace Groups identifiers to avoid name collisions
using namespace Brings all identifiers in a namespace into current scope
std:: Accesses standard library features like cout, endl, etc.
2. (1) Use global vs local variables, scope resolution operator, custom namespaces.
Here's a complete C++ program that demonstrates:
Global vs Local Variables
Scope Resolution Operator (::)
Custom Namespaces
// Global variable
int value = 100;
// Custom namespace
namespace MyNamespace {
int value = 200;
void show() {
std::cout << "Namespace value: " << value << std::endl;
}
}
int main() {
int value = 300; // Local variable
std::cout << "Local value: " << value << std::endl; // Local variable
std::cout << "Global value: " << ::value << std::endl; // Scope resolution to
global
std::cout << "Namespace value: " << MyNamespace::value << std::endl; // Access
from namespace
return 0;
}
🧠 Output Explanation:
If you run the above code, you’ll get:
Local value: 300
Global value: 100
Namespace value: 200
Namespace value: 200
Namespace
MyNamespace::value Prevents name conflict
variable
#include <iostream>
// Global variable
int num = 10;
// Custom namespace
namespace MyMath {
int num = 20;
void display() {
std::cout << "Inside namespace MyMath, num = " << num << std::endl;
}
}
int main() {
int num = 30; // Local variable
return 0;
}
🧾 Output:
Local num = 30
Global num = 10
Namespace num = 20
Inside namespace MyMath, num = 20
📘 Explanation:
Code Meaning
int num = 30; in main() Local variable
::num Scope resolution to access global variable
MyMath::num Accesses variable inside custom namespace
MyMath::display() Calls function defined inside namespace
🔹 3. Inline Functions
✅ Definition:
An inline function is a function where the compiler replaces the function call with the
actual function code (body) during compilation, to reduce the overhead of function
calls.
✅ Syntax:
inline return_type function_name(parameters) {
// function body
}
Example:
inline int square(int x) {
return x * x;
}
int main() {
int num;
cout << "Square of " << num << " is: " << square(num) << endl;
cout << "Cube of " << num << " is: " << cube(num) << endl;
return 0;
}
✅ Output (Sample):
Enter a number: 4
Square of 4 is: 16
Cube of 4 is: 64
Experiment 4:
4. Classes, Objects, and Encapsulation
Model a Bank Account with data members and functions (deposit, withdraw, display).
Access Specifiers: Public vs Private – create a sample program.
this Pointer: Use to resolve naming conflicts.
public:
void setDetails(int r, string n) {
rollNo = r;
name = n;
}
void display() {
cout << "Roll No: " << rollNo << ", Name: " << name << endl;
}
};
🔹 2. Encapsulation – Concept
🔸 Definition:
Encapsulation means binding data and related functions into a single unit (class). It also
protects data from unauthorized access.
🔸 Achieved Using:
private → for data members
public → for access functions
class BankAccount {
private:
int accountNumber;
string holderName;
double balance;
public:
void createAccount(int accNo, string name, double bal) {
accountNumber = accNo;
holderName = name;
balance = bal;
}
void display() {
cout << "\n--- Account Info ---" << endl;
cout << "Account No: " << accountNumber << endl;
cout << "Holder Name: " << holderName << endl;
cout << "Balance: " << balance << endl;
}
};
int main() {
BankAccount acc;
acc.createAccount(1001, "P. Mohan", 5000.0);
acc.deposit(1500);
acc.withdraw(2000);
acc.display();
return 0;
}
public:
void setData(int id, string name) {
this->id = id; // resolves conflict
this->name = name;
}
void display() {
cout << "ID: " << id << ", Name: " << name << endl;
}
};
🔸 Syntax:
this->data_member = parameter;
✅ Summary Table
Topic Syntax/Usage Example
Class Declaration class ClassName { ... };
Object Creation ClassName obj;
Access Specifiers private:, public:
Encapsulation Keep data private, use public methods to access
this Pointer this->x = x; to avoid name conflict
5. Function Overloading
Define multiple functions with same name but different parameters.
Use of default arguments.
Friend Function to access private data.
✅ 5. Function Overloading, Default Arguments & Friend Function
🔹 1. Function Overloading
✅ Definition:
Function Overloading means defining multiple functions with the same name but
different number or types of parameters.
✅ Syntax:
return_type function_name(parameter_list);
✅ Example:
#include <iostream>
using namespace std;
class Print {
public:
void show(int x) {
cout << "Integer: " << x << endl;
}
void show(double x) {
cout << "Double: " << x << endl;
}
void show(string x) {
cout << "String: " << x << endl;
}
};
int main() {
Print p;
p.show(5); // Calls int version
p.show(5.5); // Calls double version
p.show("Hello"); // Calls string version
return 0;
}
🔹 2. Default Arguments
✅ Definition:
Default Arguments allow a function to be called with fewer arguments than it is defined
with.
Missing values are filled with default values.
✅ Syntax:
return_type function_name(type1 a = val1, type2 b = val2);
✅ Example:
#include <iostream>
using namespace std;
int main() {
greet(); // Uses default argument "Guest"
greet("Pattabhi"); // Uses provided argument
return 0;
}
🔹 3. Friend Function
✅ Definition:
A friend function is not a member of the class, but it can access private and protected
members of that class.
✅ Syntax:
class ClassName {
friend return_type function_name(arguments);
};
✅ Example:
#include <iostream>
using namespace std;
class Box {
private:
int length;
public:
Box() {
length = 10;
}
int main() {
Box b1;
displayLength(b1); // Friend function call
return 0;
}
✅ Summary Table
Concept Purpose Syntax/Usage
Function Same function name, different void show(int); void
Overloading parameters show(double);
Provide default value to function void greet(string name =
Default Arguments
parameters "Guest");
Non-member function accessing friend void displayLength(Box
Friend Function
private data b);
🔹 1. Default Constructor
✅ Definition:
A default constructor is a constructor that takes no parameters. It is automatically called
when an object is created.
✅ Syntax:
class ClassName {
public:
ClassName(); // Default constructor
};
✅ Example:
#include <iostream>
using namespace std;
class Student {
public:
Student() { // Default Constructor
cout << "Default Constructor Called!" << endl;
}
};
int main() {
Student s1; // Constructor is called automatically
return 0;
}
🔹 2. Destructor
✅ Definition:
A destructor is a special member function used to release resources when the object is
destroyed.
✅ Syntax:
~ClassName(); // tilde (~) symbol
✅ Example:
#include <iostream>
using namespace std;
class Student {
public:
Student() {
cout << "Constructor called." << endl;
}
~Student() {
cout << "Destructor called." << endl;
}
};
int main() {
Student s1;
return 0; // Destructor called automatically at end
}
🔹 3. Constructor Overloading
✅ Definition:
Constructor overloading means multiple constructors with the same name (class name),
but different parameter lists.
✅ Example:
#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
string name;
public:
// Default constructor
Student() {
rollNo = 0;
name = "Unknown";
}
// Parameterized constructor
Student(int r, string n) {
rollNo = r;
name = n;
}
void display() {
cout << "Roll No: " << rollNo << ", Name: " << name << endl;
}
};
int main() {
Student s1; // Calls default constructor
Student s2(101, "Ravi"); // Calls parameterized constructor
s1.display();
s2.display();
return 0;
}
🔹 4. Copy Constructor
✅ Definition:
A copy constructor creates a new object by copying the values from an existing object.
✅ Syntax:
ClassName(const ClassName &obj);
✅ Example:
#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
public:
// Parameterized constructor
Student(int r) {
rollNo = r;
}
// Copy constructor
Student(const Student &s) {
rollNo = s.rollNo;
}
void display() {
cout << "Roll No: " << rollNo << endl;
}
};
int main() {
Student s1(101);
Student s2 = s1; // Copy constructor called
s1.display();
s2.display();
return 0;
}
✅ Summary Table
Concept Description Syntax Example
Default Constructor Constructor with no parameters Student() { }
Destructor Destroys object and frees resources ~Student() { }
Constructor Multiple constructors with different Student(); Student(int,
Overloading parameters string);
Copy Constructor Creates a new object from another object Student(const Student &s)