Unit 4
Unit 4
This document is confidential and intended solely for the educational purpose of RMK
Group of Educational Institutions. If you have received this document through email in
error, please notify the system manager. This document contains proprietary information
and is intended only to the respective group / learning community as intended. If you
are not the addressee you should not disseminate, distribute or copy through e-mail.
Please notify the sender immediately by e-mail if you have received this document by
mistake and delete this document from your system. If you are not the intended
recipient you are notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.
22CS101
Problem Solving using C++
1 Contents
2 Course Objectives
3 Prerequisites
4 Syllabus
5 Course Outcomes
6 CO-PO Mapping
7 Lecture Plan
9 Lecture Notes
10 Assignments
12 Part-B Questions
16 Assessment Schedule
4.3 Inheritance
4.15 Polymorphism
2. Course Objectives
❖ To learn problem solving and programming fundamentals.
using exceptions.
3. Prerequisites
22CS101
Problem Solving Using C++
Fundamentals of
Programming
Logical Thinking
Fundamentals of
Mathematics
4. Syllabus
PROBLEM SOLVING USING C++ L T P C
22CS101 (Lab Integrated) 3 0 2 4
OBJECTIVES:
To learn programming fundamentals in C.
To gain knowledge on pointers and functions.
To apply the principles of classes and objects
To develop a C++ application with object oriented concepts.
To use the functionalities of I/O operations, files build C++ programs using exceptions.
UNIT I PROGRAMMING FUNDAMENTALS 15
Computational thinking for Problem solving – Algorithmic thinking for problem Solving- Building
Blocks - Problem Solving and Decomposition –Dealing with Error – Evaluation. Overview of C –
Data types – Identifiers – Variables – Storage Class Specifiers – Constants – Operators -
Expressions – Statements – Arrays and Strings – Single-Dimensional – Two-
Dimensional Arrays – Arrays of Strings – Multidimensional Arrays.
Pointers -Variables – Operators – Expressions – Pointers and Arrays – Functions - Scope Rules –
Function Arguments – return Statement – Recursion – Structures – Unions – Enumerations.
Concepts of Object Oriented Programming – Benefits of OOP – Simple C++ program - Classes and
Objects - Member functions - Nesting of member functions - Private member functions - Memory
Allocation for Objects - Static Data Members - Static Member functions - Array of Objects - Objects
as function arguments - Returning objects - friend functions – Const Member functions -
Constructors – Destructors
C++ Streams – Unformatted I/O - Formatted Console I/O – Opening and Closing File – File
modes - File pointers and their manipulations – Templates – Class Templates – Function Templates
- Exception handling.
Lab Exercises
REFERENCES:
1. Nell Dale, Chip Weems, “Programming and Problem Solving with C++”, 5th Edition,
Jones and Barklett Publishers, 2010.
2. John Hubbard, “Schaum's Outline of Programming with C++”, MH, 2016.
3. Yashavant P. Kanetkar, “Let us C++”, BPB Publications, 2020
4. ISRD Group, “Introduction to Object-oriented Programming and C++”, Tata McGraw-
Hill Publishing Company Ltd., 2007.
5. D. S. Malik, “C++ Programming: From Problem Analysis to Program Design”, Third
Edition, Thomson Course Technology, 2007.
6. https://infyspringboard.onwingspan.com/web/en/aptoclex_auth_01297200240671948
837_shared/overview
5. Course Outcomes
PO PO PO PO PO PO PO PO PO PO PO PO PS PS PS
COs 1 2 3 4 5 6 7 8 9 10 11 12 O1 O2 O3
CO1 3 3 3 3 3 2 1
CO2 3 3 3 3 3 3 3 1
CO3 3 3 3 3 3 3 3 3 3 3 3 3 1
CO4 3 3 3 3 3 3 3 1
CO5 3 3 3 3 3 3 3 1
7. Lecture Plan - Unit IV
No.
S. of Proposed Actual Pertaining Taxonomy Mode of
Topic
No. Period Date Lecture CO Level Delivery
s Date
Operator
1 Chalk &
Overloading 1 CO4 K2
Talk
Overloading Using
2 1 CO4 Chalk &
Friend functions K3
Talk
3
Inheritance 1 CO4
K2
Chalk &
Talk
Types of
4 1 CO4 Chalk &
inheritance K2
Talk
15 Polymorphism 1 CO4
K2
Chalk &
Talk
8. Activity Based Learning
Learning Method Activity
Output
Sum of Two Numbers is : 30
Sum of Three Numbers is : 55
4.1 Operator Overloading
Operator Over Loading
In C++, we can change the way operators work for user-defined types like objects
and structures. This is known as operator overloading. For example,
Suppose we have created three objects c1, c2 and result from a class named
Complex that represents complex numbers.
Since operator overloading allows us to change how operators work, we can
redefine how the + operator works and use it to add the complex numbers of c1 and c2
by writing the following code:
result = c1 + c2;
instead of using
result = c1.addNumbers(c2);
This makes our code intuitive and easy to understand.
Syntax for C++ Operator Overloading
To overload an operator, we use a special operator function. We define the
function inside the class or structure whose objects/variables we want the overloaded
operator to work with.
class className
{ ... .. ...
public returnType operator symbol (arguments)
{ ... .. ...
}
... .. ...
};
Here,
returnType is the return type of the function.
operator is a keyword.
symbol is the operator we want to overload. Like: +, <, -, ++, etc.
arguments is the arguments passed to the function.
4.1 Operator Overloading
Program Example
#include <iostream>
using namespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex()
{
real =0;
imag = 0;
}
void input()
{
cout << "Enter Real part : ";
cin >> real;
cout << "\nEnter Imaginary part : ";
cin >> imag;
}
// Operator overloading
Complex operator + (Complex c2)
{
Complex temp;
temp.real = real + c2.real;
temp.imag = imag + c2.imag;
return temp;
}
void output()
{
if(imag < 0)
cout << "Output Complex number : "<< real << imag << "i";
else
cout << "Output Complex number : " << real << " + " << imag <<
"i";
}
};
4.1 Operator Overloading
Program Example Contd…
int main()
{
Complex c1, c2, result;
cout<<"Enter first complex number : \n";
c1.input();
cout << endl;
cout<<"Enter second complex number : \n";
c2.input();
result = c1 + c2;
result.output();
return 0;
}
Output
class Complex
{
private:
float real;
float imag;
public:
Complex()
{
real =0;
imag = 0;
}
void input()
{
cout << "Enter Real part : ";
cin >> real;
cout << "\nEnter Imaginary part : ";
cin >> imag;
}
int main()
{
Complex c1, c2, result;
result = c1 + c2;
result.output();
return 0;
}
4.2 Overloading Using Friend functions
Output
Enter first complex number :
Enter Real part : 9
Enter Imaginary part : 9
Enter second complex number :
Enter Real part : 10
Enter Imaginary part : 10
Output Complex number : 19 + 19i
Points to Remember While Overloading Operator using Friend Function:
❖ The Friend functions are not a member of the class and hence they do
not have ‘this’ pointer.
❖ The friend function in C++ can access the private data members of a
class directly.
Multiple inheritance is the process of deriving a new class that inherits the
attributes from two or more base classes.
In the above diagram, there are two-parent classes: Base Class 1 and
Base Class 2, whereas there is only one Child Class labelled “Derived Class”.
The Child Class acquires all features from both Base class 1 and Base class 2.
Therefore, we termed the type of Inheritance as Multiple Inheritance.
Syntax
class BaseClass1
{
// code of class BaseClass1
}
class BaseClass2
{
// code of class BaseClass2
}
class DerivedClass:AccessModifier BaseClass1,AccessModifier
BaseClass2
{
// code of class DerivedClass
}
4.4 Types of inheritance
Output
Your Salary is:900
Your Bonus is:100
Your Total Salary is: 1000
4.4 Types of inheritance
Multiple inheritance is the process of deriving a new class that inherits the
attributes from two or more base classes.
Example Program
#include <iostream>
using namespace std;
class Payroll // Base Class 1
{
public:
float basicPay = 900;
};
};
};
int main()
{
DerivedClass2 Obj;
Obj.print();
return 0;
}
Output
This is an example of Multilevel Inheritance
4.4 Types of inheritance
Hierarchical inheritance is defined as the process of deriving more than
one derived class from a single base class.
Example Program
#include <iostream>
using namespace std;
class Base //single base class
{
public:
int x, y;
void getdata()
{
cout << "\nEnter value of x and y:\n"; cin >> x >> y;
}
};
class Derived1 : public Base
{
public:
void product()
{
cout << "\nProduct= " << x * y;
}
};
class Derived2 : public Base
{
public:
void sum()
{
cout << "\nSum= " << x + y;
}
};
int main()
{
Derived1 obj1; //object of derived class B
Derived2 obj2; //object of derived class C
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}
4.4 Types of inheritance
Output
Enter value of x and y:
10
10
Product= 100
Enter value of x and y:
20
20
Sum= 40
4.4 Types of inheritance
Hybrid inheritance is a combination of more than one type of inheritance.
Example Program
#include <iostream>
using namespace std;
class World
{
public:
World()
{
cout << "This is World!\n";
}
};
// Here is Single Inheritance.
class Continent: public World {
public:
Continent()
{
cout << "This is Continent\n";
}};
class Country {
public:
Country()
{
cout << "This is the Country\n";
}
};
// Here is multiple Inheritance.
class India: public Continent, public Country {
public:
India()
{
cout << "This is India!";
}
};
int main()
{
India myworld;
return 0;
}
4.4 Types of inheritance
Output
This is World!
This is Continent
This is the Country
This is India!
Create class A
Declare the variable “int number” inside class A.
Class C and Class B get the “int number” variable from class A.
Class D is getting the “int number” from both class B, and Class C. This is not
good practice.
4.5 Virtual Base Class
We must avoid this redundancy and never inherit the same member for
example int number from multiple parent classes.
The solution to solve the above-mentioned problem is to make the parent
base class a virtual class.
Here, Class D is getting the “int number” directly from the parent base
virtual class.
4.5 Virtual Base Class
4.6 Abstract Class
An abstract class is a class that is designed to be specifically used as a base class. An
abstract class contains at least one pure virtual function. We declare a pure virtual
function by using a pure specifier (= 0) in the declaration of a virtual member function in
the class declaration. In other words, a function that has no definition. These classes
cannot be instantiated.
The classes inheriting the abstract class must provide a definition for the pure virtual
function; otherwise, the subclass would become an abstract class itself.
C++ Pure Virtual Functions
Pure virtual Functions are virtual functions with no definition. They start with virtual
keyword and ends with = 0. Here is the syntax for a pure virtual function,
virtual void functionname() = 0;
Note: The = 0 syntax doesn't mean we are assigning 0 to the function. It's just the way
we define pure virtual functions.
Pure virtual functions are used
❖ if a function doesn't have any use in the base class
❖ but the function must be implemented by all its derived classes
Let's take an example,
Suppose, we have derived Triangle, Square and Circle classes from the Shape class,
and we want to calculate the area of all these shapes. In this case, we can create a pure
virtual function named calculateArea() in the Shape. Since it's a pure virtual function, all
derived classes Triangle, Square and Circle must include the calculateArea() function with
implementation. A pure virtual function doesn't have the function body and it must end
with = 0. For example,
class Shape
{
public:
// creating a pure virtual function
virtual void calculateArea() = 0;
};
Example Program 4.6 Abstract Class
#include <iostream>
using namespace std;
// Abstract class
class Shape
{
protected:
float dimension;
public:
void getDimension()
{
cin >> dimension;
}
// pure virtual Function
virtual float calculateArea() = 0;
};
// Derived class
class Square : public Shape
{
public:
float calculateArea()
{
return dimension * dimension;
}
};
// Derived class
class Circle : public Shape
{
public:
float calculateArea()
{
return 3.14 * dimension * dimension;
}
};
int main()
{
Square square;
Circle circle;
cout << "Enter the length of the square: ";
square.getDimension();
cout << "Area of square: " << square.calculateArea() << endl;
cout << "\nEnter radius of the circle: ";
circle.getDimension();
cout << "Area of circle: " << circle.calculateArea() << endl;
return 0;
}
4.6 Abstract Class
Output
Enter the length of the square: 10
Area of square: 100
Example Program :
#include <iostream>
using namespace std;
// Base Class
class Parent
{
public:
// Base Class Constructor
Parent()
{
cout << "Inside Base Class" << endl;
}
};
// Child Class
class Child : public Parent
{
public:
// Clild Class Constructor
Child()
{
cout << "Inside Child Class" << endl;
}
};
// main() function
int main()
{
// Creating object of Child Class
Child object;
return 0;
}
Output:
Inside Base Class
Inside Child Class
4.8 Member Class
Class
A class in C++ is a user-defined type or data structure declared with keyword
class that has data and functions (also called member variables and member
functions) as its members whose access is governed by the three access specifiers
private, protected or public. By default access to members of a C++ class is private
Nested Class
A declaration of a class may appear within another class. Such declaration
declares a nested class.
Explanation
A nested class is a class which is declared in another enclosing class. A nested
class is a member and as such has the same access rights as any other member.
Member functions of a nested class follow regular access rules and have no special
access privileges to members of their enclosing classes. Member functions of the
enclosing class have no special access to members of a nested class.
The scope of a nested class is valid only within the scope of the enclosing
class. This increases the use of encapsulation and creates more readable and
maintainable code
Note: Nested class in C++ can be accessed outside the enclosing class
using scope resolution operator(::) . If nested class is declared after public access
specifiers inside the enclosing class then you must add scope resolution (::) during
creating its object inside main function.
Syntax:
class EnclosingClass
{
class NestedClass
{
};
};
4.9 Nesting of Classes
Example Program
#include<iostream>
using namespace std;
class EnclosingClass
{
public:
//Declaring Nested Class
class NestedClass
{
private:
int number;
public:
void setData(int num1)
{
number = num1;
}
void showData()
{
cout << "The number is: " << number;
}
};
};
int main()
{
// :: used to create object of nested class
EnclosingClass :: NestedClass obj1;
obj1.setData(100);
obj1.showData();
return 0;
}
Output
}
void showNumber();
};
void MyClass::showNumber()
{
cout << "The Number is "<<num << "\n";
}
int main()
{
MyClass object, *p; // an object is declared and a pointer to
it
Output
The Number is 100
The Number is 100
4.10 Pointer to Objects
Program 2
#include<iostream>
using namespace std;
class Complex
{
int real, imaginary;
public:
void get_Data()
{
cout << "The real part is “ << real << endl;
cout << "The imaginary part is “ << imaginary << endl;
}
int main(){
Complex *ptr = new Complex;
ptr->set_Data(1, 54);
ptr->get_Data();
// Array of Objects
Complex *ptr1 = new Complex[4];
ptr1->set_Data(1, 4);
ptr1->get_Data();
return 0;
}
Output
The real part is 1
The imaginary part is 54
The real part is 1
The imaginary part is 4
4.11 this Pointer
Every object in C++ has access to its own address through an
important pointer called this pointer. The this pointer is an implicit
parameter to all member functions. Therefore, inside a member function,
this may be used to refer to the invoking object.
this pointer in C++ stores the address of the class instance, which is
called from the member function that enables functions to access the correct
object data members.
Friend functions do not have a this pointer, because friends are not
members of a class. Only member functions have a this pointer.
The this pointer is a pointer accessible only within the nonstatic
member functions of a class, struct, or union type. It points to the object for
which the member function is called. Static member functions don't have a
this pointer.
Syntax
this
this->member_identifier
#include<iostream>
using namespace std;
class Employee
{
public:
int id;
string name;
float salary;
Employee(int id,string name, float salary)
{
this->id = id;
this->name = name;
this->salary = salary;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void)
{
Employee e1 = Employee(101,"Sonoo",890000);
Employee e2 = Employee(102,"Nakul",59000);
e1.display();
e2.display();
return 0;
}
Output
class Base
{
public:
virtual void print()
{
cout << "Printed from Base Class print() Function" << endl;
}
};
int main() {
Derived derived1;
return 0;
}
Output
Printed from derived Class print() Function
4.13 Virtual Function
Here, we have declared the print() function of Base as virtual.
So, this function is overridden even when we use a pointer of Base type that
points to the Derived object derived1.
Working of Virtual Function in C++
public:
void getDimension()
{
cin >> dimension;
}
// pure virtual Function
virtual float calculateArea() = 0; // Pure Virtual Function
};
// Derived class
class Square : public Shape
{
public:
float calculateArea()
{
return dimension * dimension;
}
};
// Derived class
class Circle : public Shape
{
public:
float calculateArea()
{
return 3.14 * dimension * dimension;
}
};
int main()
{
Square square;
Circle circle;
cout << "Enter the length of the square: ";
square.getDimension();
cout << "Area of square: " << square.calculateArea() << endl;
cout << "\nEnter radius of the circle: ";
circle.getDimension();
cout << "Area of circle: " << circle.calculateArea() << endl;
return 0;
}
4.14 Pure Virtual Function
Output
Enter the length of the square: 10
Area of square: 100
If the derived class will not If the derived class does not
redefine the virtual function of define the pure virtual function; it
5 the base class, then there will will not throw any error but the
be no effect on the derived class becomes an abstract
compilation. class.
College
It is achieved by function
It is achieved by virtual
4 overloading and operator
functions and pointers.
overloading.
return 0;
}
Output
Sum of Two Integers using sum() = 11
Sum of Two Double using sum() = 12.1
Sum of Three Integers using sum() = 18
4.15 Polymorphism
Operator overloading - Compile Time Polymorphism
we can overload an operator as long as we are operating on
user-defined types like objects or structures. We cannot use operator
overloading for basic types such as int, double, etc. Operator overloading is
basically function overloading, where different operator functions have the
same symbol but different operands. And, depending on the operands,
different operator functions are executed.
Program
##include <iostream>
using namespace std;
class Count
{
private:
int number;
public:
// Constructor to initialize count to 5
Count()
{
number = 5;
}
// Overload ++ when used as prefix
void operator ++()
{
number = number + 1;
}
void display()
{
cout << "Incremented value of member \"number\"
of object Count using overloaded operator ++ is :
" << number << endl;
}
};
4.15 Polymorphism
int main()
{
Count count1;
// Call the "void operator ++()" function
++count1;
count1.display();
return 0;
}
Output
class Shape
{
protected:
int width, height;
public:
Shape( int a = 0, int b = 0)
{
width = a;
height = b;
}
virtual int area()
{
cout << "Parent class area :" << width * height << endl;
return width * height;
}
};
class Rectangle: public Shape
{
public:
Rectangle( int a = 0, int b = 0)
{
width = a;
height = b;
}
int area ()
{
cout << "Rectangle class area : " << width * height << endl;
return (width * height);
}
};
4.15 Polymorphism
Program Contd…
class Triangle: public Shape
{
public:
Triangle( int a = 0, int b = 0)
{
width = a;
height = b;
}
int area ()
{
cout << "Triangle class area : " << (width * height)/2 <<
endl;
return (width * height / 2);
}
};
return 0;
}
Output
Rectangle class area : 70
Triangle class area : 25
10. Assignment Questions
S.No K-
Write Programs for the following COs
. Level
Overload the binary operator - to find the
1 K3 CO4
difference between two complex numbers
Create a class named Vehicle with two data
member named mileage and price. Create its
two subclasses
❖ Car with data members to store ownership
cost, warranty (by years), seating capacity
and fuel type (diesel or petrol).
❖ Bike with data members to store the
number of cylinders, number of gears, cooling
type(air, liquid or oil), wheel type(alloys or
spokes) and fuel tank size(in inches)
2 ❖ Make another two subclasses Audi and Ford K3 CO4
of Car, each having a data member to store
the model type.
❖ Next, make two subclasses Bajaj and TVS,
each having a data member to store the
make-type.
Now, store and print the information of an
Audi and a Ford car (i.e. model type,
ownership cost, warranty, seating capacity,
fuel type, mileage and price.)
❖ Do the same for a Bajaj and a TVS bike.
Create a class named Shape with a function
that prints "This is a shape". Create another
class named Polygon inheriting the Shape
class with the same function that prints
"Polygon is a shape". Create two other classes
named Rectangle and Triangle having the
3 same function which prints "Rectangle is a K3 CO4
polygon" and "Triangle is a polygon"
respectively. Again, make another class
named Square having the same function
which prints "Square is a rectangle".
Now, try calling the function by the object of
each of these classes
11. Part A
Question & Answer
1.
Part A
Define Over Loading (CO4, K1)
An overloaded declaration is a declaration that is declared with the same name
as a previously declared declaration in the same scope, except that both declarations
have different arguments and obviously different definition (implementation).
❖ Function overloading
❖ Operator overloading
we can change the way operators work for user-defined types like objects and
structures. This is known as operator overloading. we can redefine how the +
operator works and use it to add the complex numbers of c1 and c2
Part A
6. How to Overload an operator in C++ (CO4, K3)
To overload an operator, we use a special operator function. We define the
function inside the class or structure whose objects/variables we want the overloaded
operator to work with.
class className
{ ... .. ...
public returnType operator symbol (arguments)
{ ... .. ...
}
... .. ...
};
7. Define friend function (CO4, K1)
A friend function can access the private and protected data of a class. We declare
a friend function using the friend keyword inside the body of the class.
Syntax
class className
{
... .. ...
friend returnType functionName(arguments);
... .. ...
}
9. List out the Characteristics of friend Function. (CO4, K1)
❖ The function is not in the scope of the class to which it has been declared as a friend.
❖ It cannot be called using the object as it is not in the scope of that class.
The Friend function in C++ using operator overloading offers better flexibility
to the class.
The friend function in C++ can access the private data members of a class directly.
Part A
10. Define Inheritance (CO4, K1)
Inheritance is one of the key features of Object-oriented programming in C++. It
allows us to create a new class (Derived Class) from an existing class (Base Class).
Derived Class object acquires all the properties and behaviors of its Base Class object
automatically.
Single inheritance
Multiple inheritance
Multilevel inheritance
Hierarchical inheritance
Hybrid inheritance
13. Give the Syntax for Single Inheritance. (CO4, K2)
Syntax
class BaseClass
{
// code of class BaseClass
}
class DerivedClass : AccessModifier BaseClass
{
// code of class DerivedClass
}
14. Give the Syntax for Multiple Inheritance. (CO4, K2)
Syntax
class BaseClass1
{
// code of class BaseClass1
}
class BaseClass2
{
// code of class BaseClass2
}
class DerivedClass:AccessModifier BaseClass1,AccessModifier
BaseClass2
{
// code of class DerivedClass }
Part A
15. Give the Syntax for Multi Level Inheritance. (CO4, K2)
Syntax
class BaseClass1
{
// code of class BaseClass1
}
class DerivedClass1 : AccessModifier BaseClass1
{
// code of class DerivedClass1
}
class DerivedClass2 : AccessModifier DerivedClass1
{
// code of class DerivedClass2
}
16. Give the Syntax for Hierarchical Inheritance. (CO4, K2)
Syntax
class BaseClass
{
// code of class BaseClass
}
class DerivedClass1 : AccessModifier BaseClass
{
// code of class DerivedClass1
}
class DerivedClass2 : AccessModifier BaseClass
{
// code of class DerivedClass2
}
class DerivedClass3 : AccessModifier BaseClass
{
// code of class DerivedClass3
17. Demonstrate the need for Virtual Class (CO4, K1)
Virtual classes are primarily used during multiple inheritance. To avoid, multiple
instances of the same class being taken to the same class which later causes ambiguity,
virtual classes are used.
}
private:
int x_;
int y_;
};
21. Demonstrate the usage of Pointer to Object (CO4, K3)
A pointer to an object in C++ is used to store the address of an object. For creating a
pointer to an object in C++, we use the following
syntax
Classname * pointertoobject;
For storing the address of an object into a pointer in c++, we use the following
syntax
Pointertoobject = &objectname;
After storing the address in the pointer to the object, the member function can
be called using the pointer to the object with the help of an arrow operator [ ->].
Part A
22. Define tis pointer. (CO4, K1)
The this pointer is a pointer accessible only within the nonstatic member
functions of a class, struct, or union type. It points to the object for which the member
function is called. Static member functions don't have a this pointer.
Syntax
this
this->member_identifier
A virtual function is a member function which is declared within a base class and is
re-defined (overridden) by a derived class.
❖ Virtual functions ensure that the correct function is called for an object, regardless
26. Give the syntax for defining Pure Virtual Function. (CO4, K2)
Syntax 1
Note: The = 0 syntax doesn't mean we are assigning 0 to the function. It's just the
way we define pure virtual functions.
Syntax 2
It is achieved by function
It is achieved by virtual
3 overloading and operator
functions and pointers.
overloading.
❖ Function overloading
❖ Operator overloading
❖ Virtual functions
❖ Function overriding
12. Part B Questions
12.PART – B Questions
1. Explain Function Overloading with a C++ Program. (CO4, K3)
2. Write a C++ program to demonstrate function overloading. (CO4, K3)
3. Write a C++ program to demonstrate the overloading of a unary operator. (CO4, K3)
4. Write a C++ program to demonstrate the overloading of a binary operator. (CO4, K3)
5. Explain Operator Overloading with a C++ Program. (CO4, K3)
6. Write a program to add two complex numbers using object as arguments. (CO4, K3)
7. Write a program to add two distances. (CO4, K3)
8. Discuss the role of access specifiers in inheritance and show their visibility when they
are inherited as public, private and protected. (CO4. K2)
9. Write a C++ Program to Illustrate Friend Function. (CO4, K3)
10. Explain the significance of Inheritance in Object Oriented Programming. (CO4, K1)
11. C++ program to read and print employee information using multiple inheritance.
(CO4, K3)
12. C++ program to demonstrate example of hierarchical inheritance to get square and
cube of a number. (CO4, K3)
13. C++ program to read and print employee information with department and PF
information using hierarchical inheritance. (CO4, K3)
14. Write a C++ Program to Illustrate types of inheritance. (CO4, K3)
15. Illustrate the role of Virtual Base Class in C++ with a complete Program. (CO4, K3)
16. How overriding is different from the overloading.
17. Explain the concept of polymorphism by an example in C. (CO4. K2)
18. Compare and Contrast late binding and early binding. (CO4. K2)
19. Write a C++ Program to demonstrate Run time polymorphism. (CO4, K3)
20. Write a C++ Program to illustrate the use of pure virtual function in Polymorphism.
(CO4, K3)
21. Write a C++ Program to define an Abstract Class in Polymorphism. (CO4, K3)
22. Write a C++ Program to illustrates the use of Virtual base class. (CO4, K3)
23. Write a C++ Program to show an Example of Pointers to base class. (CO4, K3)
24. Write a C++ Program to illustrate Abstract Base Class. (CO4, K3)
25. Write a C++ Program to illustrate an example of Pure Virtual functions. (CO4, K3)
26. Write a C++ Program to maintain employee database using virtual class. (CO4, K3)
13. Supportive online courses
❖ https://nptel.ac.in/courses/106105151
❖ https://nptel.ac.in/courses/106101208
❖ https://www.codecademy.com/learn/learn-c-plus-plus
❖ https://www.udemy.com/topic/c-plus-plus/
❖ https://www.edx.org/learn/c-plus-plus
❖ https://in.coursera.org/specializations/hands-on-cpp
❖ https://www.simplilearn.com/free-course-to-learn-cpp-basics-skillup
❖ https://www.codespaces.com/best-c-plus-plus-courses-tutorials-certifications
.html
❖ https://hackr.io/blog/cpp-course
❖ https://www.udacity.com/course/c-for-programmers--ud210
❖ https://swayam.gov.in
❖ https://www.coursera.org
❖ https://www.udemy.com
❖ https://unacademy.com
❖ https://www.sololearn.com
❖ https://www.tutorialspoint.com
❖ https://www.w3schools.in
❖ https://www.geeksforgeeks.org
❖ https://www.programiz.com
14. Real Time Applications
Suppose a Car has two functions: accelerate and brake. Now these
functions have different values for different cars as every car has a
different acceleration rate and braking mechanisms. So let us take the
example of a Mercedes Benz S-Class which is a car. It inherits all the
definitions of the class Car. However its acceleration and braking
systems are unique.
class person
{
protected:
char name[20];
int code;
public:
void getdetail(void)
{
cout<<"\n\nEnter name :- ";
cin>>name;
cout<<"\nEnter code :- ";
cin>>code;
}
void dispdetail(void)
{
cout<<"\n\nNAME :- "<<name;
cout<<"\nCODE :- "<<code;
}
};
15. Content Beyond Syllabus
class account : virtual public person
{
protected:
float pay;
public:
void getpay(void)
{
cout<<"\nEnter Pay amount :- ";
cin>>pay;
}
void dispay(void)
{
cout<<"\nPAY :- "<<pay;
}
};
void display(void)
{
cout<<"\n\n=====DISPLAY DETAILS=====\n";
dispdetail();
dispay();
dispexpr();
}
void update(void)
{
cout<<"\n\n=====UPDATE DETAILS=====\n";
cout<<"\nChoose detail you want to update\n";
cout<<"1) NAME\n";
cout<<"2) CODE\n";
cout<<"3) EXPERIENCE\n";
cout<<"4) PAY\n";
cout<<"Enter your choice:- ";
int choice;
cin>>choice;
15. Content Beyond Syllabus
switch(choice)
{
case 1 : cout<<"\n\nEnter name : - ";
cin>>name;
break;
case 2 : cout<<"\n\nEnter code :- ";
cin>>code;
break;
case 3 : cout<<"\n\nEnter pay :- ";
cin>>pay;
break;
case 4 : cout<<"\n\nEnter Expereince :- ";
cin>>experience;
break;
default: cout<<"\n\nInvalid choice\n\n";
}
}
};
int main()
{
master ob1;
int choice;
while(1)
{
cout<<"\n\n=====EMPLOYE DATABASE=====\n\n";
cout<<"\nChoose Operation you want to
perform\n";
cout<<"1) Create Record\n";
cout<<"2) Display Record\n";
cout<<"3) Update Record\n";
cout<<"4) Exit\n";
cout<<"\nEnter your choice:- ";
cin>>choice;
15. Content Beyond Syllabus
switch(choice)
case 1 : ob1.create();
break;
case 2 : ob1.display();
break;
case 3 : ob1.update();
break;
case 4 : exit(1);
Again\n\n";
return 0;
}
15. Content Beyond Syllabus
=====EMPLOYE DATABASE=====
=====GETDATA IN=====
=====EMPLOYE DATABASE=====
NAME :- Vijayakumar
CODE :- 229
PAY :- 75000
EXPERIENCE:- 23
=====EMPLOYE DATABASE=====
Exited…..
16. Assessment Schedule
S. Name of the
Start Date End Date Portion
No. Assessment
Text Books:
Reference Books:
Disclaimer:
This document is confidential and intended solely for the educational purpose of RMK Group of
Educational Institutions. If you have received this document through email in error, please notify the
system manager. This document contains proprietary information and is intended only to the
respective group / learning community as intended. If you are not the addressee you should not
disseminate, distribute or copy through e-mail. Please notify the sender immediately by e-mail if you
have received this document by mistake and delete this document from your system. If you are not
the intended recipient you are notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.