Unit 3
Unit 3
Unit 3
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++
Department : Science and Humanities
Date :
1. Table of Contents
SLIDE
S.NO. CONTENTS
NO.
1 CONTENTS 5
2 COURSE OBJECTIVES 6
3 PRE REQUISITES (COURSE NAMES WITH CODE) 7
4 SYLLABUS (WITH SUBJECT CODE, NAME, LTPC DETAILS) 8
5 COURSE OUTCOMES
12
15 ASSESSMENT SCHEDULE 73
OBJECTIVES:
12. Write C++ programs that illustrate how the following forms of inheritance are
supported:
a) Single inheritance b)Multiple inheritance c)Multi level inheritance
d)Hierarchical inheritance
13. Program to demonstrate pure virtual function implementation.
14. Count the number of account holders whose balance is less than the minimum
balance using sequential access file.
13. Write a Program to Demonstrate the Catching of all
Exceptions.
TEXT BOOKS:
1. Herbert Schildt, “The Complete Reference C++”, 4th edition,
MH, 2015. (Unit 1 & 2)
2. E Balagurusamy,”Object Oriented Programming with C++”, 4th
Edition, Tata McGraw- Hill Education, 2008. (Unit 3, 4 & 5)
REFERENCES:
1. Karl Beecher,”Computational Thinking: A beginner's guide to
problem-solving and programming”, BCS Learning &
Development Ltd, 2017. (Unit 1)
2. Nell Dale, Chip Weems, “Programming and Problem Solving
with C++”, 5th Edition, Jones and Barklett Publishers, 2010.
3. John Hubbard, “Schaum's Outline of Programming with C++”,
MH, 2016.
4. Yashavant P. Kanetkar, “Let us C++”, BPB Publications, 2020
5. ISRD Group, “Introduction to Object-oriented Programming
and C++”, Tata McGraw- Hill Publishing Company Ltd., 2007.
6. D. S. Malik, “C++ Programming: From Problem Analysis to
Program Design”, Third Edition, Thomson Course Technology,
2007
5. COURSE OUTCOME
At the end of this course, the students will be able to:
PO10
PO11
PO12
PSO1
PSO2
PSO3
PO1
PO2
PO3
PO4
PO5
PO6
PO7
PO8
PO9
COs
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
Lecture Plan
UNIT III
Unit III
Member functions
4 Nesting of member 1 Chalk &
functions, Private CO3 K1 Talk/PPT
member functions
UNIT III
Activity Based Learning
UNIT III
Output
5
The number is a prime number
6
The number is not a prime number
//PROGRAM TO DISPLAY ALL FACTORS OF A NUMBER
#include<iostream>
using namespace std;
int main()
{
int n, i;
cin>>n;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
cout<<i<<“ “;
}
}
return 0;
}
Output
60
1 2 3 4 5 6 10 12 15 20 30 60
//PROGRAM TO CHECK VOWEL OR A CONSONANT MANUALLY
#include<iostream>
using namespace std;
int main()
{
char c;
bool islower, isupper;
cin>>c;
islower=(c==‘a’ || c==‘e’ || c==‘I’ || c==‘o’ || c==‘u’);
isupper=(c==‘A’ || c==‘E’ || c==‘I’ || c==‘O’ ||c==‘U’);
if(!isalpha(c))
cout<<“Not a alphabet”;
else
if(islower || isupper)
cout<< c<<“is a vowel”;
else
cout<<c<<“is a consonant.”;
return 0;
}
Output
Enter an alphabet: u
u is a vowel
3.4 CLASS AND OBJECT
3.4.1 CLASS
• Classes are the blueprint for the objects.
• It is a user-defined datatype, which holds its own data members and
member functions, which can be accessed and used by creating an
instance of that class.
• The data and functions within a class are called members of the class.
• For example, Consider the Class of Cars. There may be many cars with
different names and brand but all of them will share some common
properties like 4 wheels, Speed limit, Mileage range etc.,
• So here, Car is the Class and wheels, speed limits, mileage are
their properties.
DEFINING A CLASS
• A Class is defined using keyword class followed by the name of the
class.
• The body of the class is defined inside the curly brackets and
terminated by the semicolon at the end.
EXAMPLE
class Car // The Class
{
Public: // Access specifier
string brand;
string model; //Attributes
int year;
};
3.4.2 Object
• An Object is an instance of a class.
• Objects are the basic run-time entities in an object-oriented system.
• They may represent a person, a place, a bank account, a table of
data or any item that the program has to handle.
• Objects take up space in the memory and have an associated
address like a record in Pascal or a structure in C.
• When a program is executed, the objects interact by sending
messages to one another.
• For example, if “customer” and “account” are two objects in a
program, then the customer object may send a message to the
account object requesting for the bank balance.
Declaring Object
• When Class is defined no memory is allocated but when it is
instantiated memory is allocated.
• The object is created to uses the data and access functions defined
in the class.
SYNTAX
ClassName ObjectName;
EXAMPLE
#include<iostream>
using namespace std;
class Car
{
Public:
string brand;
string model;
int year;
};
int main()
{
Car obj1; // Creating an object of class car
obj1.brand = “BMW”;
obj1.model = “X5”;
obj1.year = 1999;
cout << obj1.brand << ” “ << obj1.model << “ “ << obj1.year;
}
O/P
BMW X5 1999
3.5 Data Members and Member Functions
Data members are the data variables and member functions
are the functions used to manipulate these variables and
together defines the properties and behavior of the objects
in the class.
For example,
Car is the class, the speed limit, mileage are the data
members and apply brakes, increase speed are the member
functions.
Example
#include<iostream>
using namespace std;
class Car
{
public:
int speed_limit, mileage; //Data member
void apply_break() // Member function
{
cout<< “Applybreak” ;
}
void increase_speed() // Member function
{
cout<< “Increase speed” ;
}
}
int main()
{
Car obj1; // Creating object for the class
Obj1.apply_break(); // Accessing member function
increase_speed(); // Accessing member function
}
Output
Apply break
Increase speed
3.5.1 Accessing Data Member and Member Functions
• The data members and member functions of class can be accessed using
dot (.) operator with the object.
• The public data members can be accessed directly by the object whereas
the private data member cannot be accessed directly by the object.
Example
#include<iostream>
using namespace std;
class Car
{
public:
string model //Data member
void printcarmodel() // Member function
{
cout<< “ Car model name is ” <<model;
}
}
int main()
{
Car obj1; // Creating object for the class
obj1.model = “BMW X5”; // Accessing data member
obj1.printcarmodel(); // Accessing member function
}
O/P
Car model name is BMW X5
3.5.2 Defining Member Functions in Classes
• Member functions can be defined in two places:
i) Outside the class definition
ii) Inside the class definition
EXAMPLE
#include<iostream>
using namespace std;
class set
{
int m, n;
public: Output
void input(void): Input values of m and n
void display(void); 23 13
int largest(void); Largest value = 23
};
int set :: largest(void)
{
if(m>=n)
return (m);
else
return(n);
}
void set :: input(void)
{
cout << “Input values of m and n” << “\n”;
cin >> m >> n;
}
void set :: display(void)
{
cout<< “Largest value =” << largest() << “\n”;
}
int main()
{
set A;
A.input();
A.display();
return 0;
}
3.7 Private Member Function
Example
For example, there is a class named “student” which has the following
private data members and public member functions:
• For instance, the three objects, namely, book1, book2 and book3 of
the class book have individual copies of the data members title and
price.
Syntax:
• In the above syntax, static keyword is used. The data_type is the C++ data
type such as int, float etc.
• The data_member_name is the name provided to the data member.
Example:
#include <iostream>
#include<string.h>
using namespace std;
class Student
{
private:
int rollNo;
char name[10];
int marks;
public:
static int objectCount;
Student()
{
objectCount++;
}
void getdata()
{
cout << "Enter roll number: "<<endl;
cin >> rollNo;
cout << "Enter name: "<<endl;
cin >> name;
cout << "Enter marks: "<<endl;
cin >> marks;
}
void putdata()
{
cout<<"Roll Number = "<< rollNo <<endl;
cout<<"Name = "<< name <<endl;
cout<<"Marks = "<< marks <<endl;
cout<<endl;
}
};
OUTPUT:
int Student::objectCount = 0;
Enter roll number: 1
int main(void)
Enter name: Mark
{
Enter marks: 78
Student s1;
Roll Number = 1
s1.getdata();
Name = Mark
s1.putdata();
Marks = 78
Student s2;
s2.getdata();
Enter roll number: 2
s2.putdata();
Enter name: Nancy
Student s3;
Enter marks: 55
s3.getdata();
Roll Number = 2
s3.putdata();
Name = Nancy
cout << "Total objects created = “
Marks = 55
<< Student::objectCount;
return 0; Enter roll number: 3
} Enter name: Susan
Enter marks: 90
Roll Number = 3
Name = Susan
Marks = 90
Total objects created = 3
3.10 Static Member Function
• The static member functions are special functions used to access the
static data members or other static member functions.
• A member function is defined using the static keyword.
• A static member function shares the single copy of the member
function to any number of the class objects.
• We can access the static member function using the class name or
class objects.
• If the static member function accesses any non-static data member or
non-static member function, it throws an error.
SYNTAX:
class_name::function_name(parameter);
EXAMPLE:
//create program to access the static member function using
the class name
#include <iostream>
using namespace std;
class Note
{
static int num; //Declare a static data member
public:
static int fun() // Create static member function
{
return num;
}
};
/*Initialize the static data member using the class name and the scope
resolution operator*/
int Note :: num =5; int main()
{
/*Access static member function using the class name and scope
resolution operator*/
cout<<“The value of the num is: “<<Note :: fun() <<endl;
return 0;
}
Output:
The value of the num is: 5
3.11 Arrays of Objects
• We know that an array can be of any data type including struct.
• Similarly, we can also have arrays of variables that are of the type class.
• Such variables are called arrays of objects.
Consider the following class definition:
class employee
{
char name[30];
float age;
public:
void getdata(void);
void putdata(void);
};
The identifier employee is a user-defined data type and can be used to
create objects that relate to different categories of the employees.
Example:
employee manager[3]; //array of manager
employee foreman[15]; //array of foreman
employee worker[75]; //array of worker
Example
#include<iostream>
using namespace std;
class construct
{
public:
int a, b;
construct()
{
a=10;
b=20;
}
};
int main()
{
construct c;
cout<< “a: ”<< c.a<<endl<<“b: ”<<c.b;
return 1;
}
Output:
a: 10
b: 20
3.16.1.2 Parameterized Constructor
• The parameterized constructors can take arguments to initialize an object
when it is created.
• Parameters are added to a parameterized constructor just like they are
added to a normal function.
• When object is declared in a parameterized constructor, the initial values
have to be passed as arguments to the constructor function.
• The parameterized constructors can be called implicitly or explicitly.
UNIT III
Assignment Questions
8. What is a destructor?
• A Destructor is a special class function which destroys the object as soon as
the scope of object ends. The destructor is called automatically by the
compiler when the object goes out of scope.
• The syntax for destructor is same as that for the constructor, the class
name is used for the name of destructor, with a tilde ~ sign as prefix to it.
Class Structure
13. What is array of objects? Write the syntax for declaring array of
objects.
A. The Array of Objects stores objects. We can have arrays of variables that
are of the type class.
Syntax is
ClassName ObjectName[number of objects];
14. What are const member functions?
The const member functions are the functions which are declared as
constant in the program. The object called by these functions cannot be
modified. A const member function can be called by any type of object.
Constructor Destructor
The constructor’s name must be the The destructor has the same as that of
same as that of the class the class prefixed by the tilde
symbol(~)
It is possible to overload the It is not possible to overload the
constructor function destructor function
A constructor can have a list of A destructor can’t have a list of
parameter parameter
It is not possible to inherit a It is not possible to inherit a destructor
constructor. But the constructor of the
base class can be considered a derived
class
When the object is created, the When the control reaches the end of
destructor is automatically executed the class scope, the destructor is
executed automatically to destroy the
object
Memory space reserved for the It destroys the object.
objects,
• A static member function can be called even if no objects of the class exist
and the static functions are accessed using only the class name and the
scope resolution operator ::
• A static member function can only access static data member, other static
member functions and any other functions from outside the class.
UNIT III
Real time Applications
1. Operating Systems
C++ is a fast and strongly-typed programming language
which makes it an ideal choice for developing operating
systems. Mac OS X has large amounts written in C++. Most of
the software from Microsoft like Windows, Microsoft Office, IDE
Visual Studio, and Internet Explorer are also written in C++.
2. Games
Since C++ is closer to hardware, game development
companies use it as their primary choice to develop gaming
systems. It can easily manipulate resources and can override
the complexities of 3D games and multiplayer networking.
3. GUI Based Applications
C++ is also used to develop GUI-based and desktop
applications. Most of the applications from Adobe such as
Photoshop, Illustrator, etc. are developed using C++.
4. Web Browsers
Web browsers need to be fast in execution as people do not
like to wait for their web pages to be loaded. This is why most
browsers are developed in C++ for rendering purposes. Mozilla
Firefox is completely developed from C++. Google applications
like Chrome and Google File System are partly written in C++
5. Embedded Systems
Various embedded systems that require the program to be
closer to hardware such as smartwatches, medical equipment
systems, etc., are developed in C++. It can provide a lot of low-
level function calls, unlike other high-level programming
languages.
6. Banking Applications
Since banking applications require concurrency, multi-
threading, concurrency, and high performance, C++ is the
default choice of programming language. Infosys Finacle is a
popular banking application developed using C++.
7. Compilers
The compilers of many programming languages are
developed in C and C++. This is because they are relatively
lower-level when compared to other higher-level languages and
are closer to the hardware.
8. Database Management Software
C++ is also used to write database management software.
The world’s most popular open-source database, MySQL, is
written in C++.
9. Cloud/Distributed Systems
Cloud storage systems that are used extensively need to
work closer to the hardware. This makes C++ the default
choice for implementing cloud storage systems. These systems
also require multithreading support to build concurrent
applications that support load tolerance, which C++ provides.
Bloomberg is a distributed RDBMS application that is primarily
written in C, but its development environment and set of
libraries are all written with C++.
10. Libraries
Libraries require very high-level mathematical computations,
performance, and speed. Hence C++ is the core programming
language used by most libraries. Tensorflow, one of the most
popularly used Machine Learning libraries uses C++ as
its backend programming language.
Assessment Schedule
UNIT III
Assessment Schedule
UNIT III
Text books & References
TEXT BOOKS:
1. Herbert Schildt, “The Complete Reference C++”, 4th
edition, MH, 2015. (Unit 1 & 2)
2. E Balagurusamy,”Object Oriented Programming with C++”,
4th Edition, Tata McGraw- Hill Education, 2008. (Unit 3, 4 & 5)
REFERENCES:
1. Karl Beecher,”Computational Thinking: A beginner's guide
to problem-solving and programming”, BCS Learning &
Development Ltd, 2017. (Unit 1)
2. Nell Dale, Chip Weems, “Programming and Problem Solving
with C++”, 5th Edition, Jones and Barklett Publishers, 2010.
3. John Hubbard, “Schaum's Outline of Programming with
C++”, MH, 2016.
4. Yashavant P. Kanetkar, “Let us C++”, BPB Publications,
2020
5. ISRD Group, “Introduction to Object-oriented Programming
and C++”, Tata McGraw- Hill Publishing Company Ltd., 2007.
6. D. S. Malik, “C++ Programming: From Problem Analysis to
Program Design”, Third Edition, Thomson Course Technology,
2007
Mini Project Suggestions
UNIT III
Mini Project Suggestions
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.