0% found this document useful (0 votes)
68 views

C - Notes (Data Planet)

The document discusses various C++ concepts like functions, classes, operator overloading, file handling, I/O manipulators, and exception handling. It provides examples and homework problems related to these topics for students to practice and learn. The instructor encourages students to write their own code and not copy solutions directly.

Uploaded by

Akash Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

C - Notes (Data Planet)

The document discusses various C++ concepts like functions, classes, operator overloading, file handling, I/O manipulators, and exception handling. It provides examples and homework problems related to these topics for students to practice and learn. The instructor encourages students to write their own code and not copy solutions directly.

Uploaded by

Akash Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 142

main

function1 function2 function3

function4 function5 function6


Program for addition of two integers in C & C++
What Is Inline Function? Explain with Example.
WAP to do following task through function
overloading.
1. swap values of two integer variables
2. swap values of two float variables
3. swap values of two character variables

Write Program after understanding from lecture. In lecture I have


developed two cases & third case is to be developed by you while
writing program in notebook

Kedar Kabra Sir


20-08-2020

WAP to define a class MaxMin having following members


Data Members
Two integers
Member Functions
1. To read values of integers
2. To find maximum
3. To find minimum

Write program on your own after watching lecture. If you are stuck then check
video and start writing again but do not copy paste.

Kedar Kabra Sir

WAP to define a class MaxMin having following members


Data Members
Three integers
Member Functions
1. To read values of integers
2. To find maximum
3. To find minimum

Home Work

Kedar Kabra Sir


20-08-2020

WAP to define a class Student having following members


Data Members
Roll number
Name of Student
Marks in 3 subjects
Member Functions
1. To read information
2. To compute total & average
3. To print information
Write program on your own after watching lecture. If you are stuck then check
video and start writing again but do not copy paste.

Kedar Kabra Sir

WAP to define a class Employee having following


members
Data Members
Employee number
Name of Employee
Gender
Salary
Member Functions
1. To read information
2. To print information
Home Work
Kedar Kabra Sir
20-08-2020

WAP to define a class arithmetic to demonstrate


arithmetic operations on data members.
(Note: Use nesting of member functions)

If a member function of class is called by another


member function of same class then
it is nesting of member functions.

Write program on your own after watching lecture. If you are stuck then check
video and start writing again but do not copy paste.

Kedar Kabra Sir


Example -1

Example-2
Answer for
what is friend
function?
Explain it’s
properties
This is for
additional
knowledge,
do not
include in
answer.
Write C++ program for addition of complex number
Method-1: Through member without returning object
Method-2: Through member by returning object
Method-3: Through friend function
Write C++ program for addition of time objects
Method-1: Through member without returning object

Method-2: Through member by returning object (HOME – WORK)


Method-3: Through friend function (HOME – WORK)
Write Output
Write Output
WAP to overload Unary minus operator.
Write C++ program for addition of complex number through operator
overloading
Write C++ program for addition of time objects through operator
overloading
Homework
Write C++ program for addition of matrix objects through operator
overloading.
Take 3 X 3 Matrices
Method – 1: Member function
Method – 2: Friend function
Home Work
Write C++ program for Concatenation of strings through operator
overloading.
Method – 1: Member function

Method – 2: Friend function


Home Work
Write C++ program for comparison of two-time objects
through operator overloading.
Method – 1: Member function
Method – 2: Friend function (Home Work)
Write C++ program for comparison of two string objects
through operator overloading.
Method – 1: Member function
Method – 2: Friend function (Home Work)
Fill
members
in every
example
Program is Home-work
Output:
Derived class display
Member class

Member of Member
Member of Member
7.
Write output on your
own
For ex: increment of integer pointer

For ex: increment of float pointer

For ex: adding integer n to pointer


Dynamic memory allocation for array within class
Dynamic memory allocation for matrix within class

Try to modify above program for memory allocation through parametrized


constructor.
Develop program for addition of matrices by asking dimensions to user
and then creating objects accordingly.
If you are confident enough, try to develop program for multiplication.
Also include logic of checking conformability of multiplication for given
dimensions.

“Always believe in yourself and always stretch yourself beyond your


limits. Your life is worth a lot more than you think because you are
capable of accomplishing more than you know. You have more potential
than you think, but you will never know your full potential unless you
keep challenging yourself and pushing beyond your own self-imposed
limits.”
― Roy T. Bennett
Write C++ program to create a file and store
following information in file.
Your name
Your college
Your branch
Write C++ program to read name and phone number from user and store
records in file
WAP to copy contents of one file to another
WAP to concatenate contents of two files in third file
(Read name of two source files and one target file from
user) – Home Work

WAP to count number of words in file– Home Work

WAP to count number of lines in files (do not use


getline() function, use get() function) – Home Work
I/O manipulators
 C++ provides built in function which can be included in
input/output statements to format the input/output, such
standard functions are known as manipulators.”
 These manipulators are included in iomanip.h header file.

Some of the commonly used manipulators are discussed below.

endl (end line)


 It terminates output line and moves cursor on next line.
 Syntax -
cout<<endl;

setw() (set width)


 It is used to set column width for printing a value in
output.
 Syntax -
cout<<setw(n)<<x;

Where, n is number of columns to be used while printing x

setfill()
 It is used to set fill character for unused columns while
printing a value. Default is space (Blank)
 Syntax -
cout<<setfill('*')<<x;

Unused columns will be filled by symbol * while printing x

KEDAR KABRA SIR - 9420213481 1


setprecision()
 It is used to set precision level for printing real numbers

cout<<setprecision(3)<<x;
if x is a float value, it will be truncated at 3rd digit.

setiosflags()
 It is used to set various flags of ios class which formats
output in specific manner.
 Syntax -
cout<<setiosflags(flagbit)

Some of the flag bit and their actions are listed below
flag bit actions
ios::left Value is printed left justified
ios::right Value is printed right justified(default)
ios::dec Integer is printed in decimal format(default)
ios::oct Integer is printed in octal format
ios::hex Integer is printed in hexa-decimal format
ios::uppercase Upper case in Hexa-decimal number
ios::showbase Show base in output (0 for octal and 0x for hex)
ios::showpos show positive (it shows sign + while printing +ve number)
ios::scientific Print float value in exponential notation

6. resetiosflags()
 It is used to reset flags to default
 Syntax -
cout<<resetiosflags(flagbit)

 Multiple flags can be set/reset by using symbol '|'

Member functions
 C++ also provides some standard functions in stream classes
which can format output.
 Such functions are not directly included in output statement,
instead they are called through ‘cout' object.

KEDAR KABRA SIR - 9420213481 2


Following table shows manipulators and equivalent functions

manipulator function
setw() width()
setfill() fill()
setprecision() precision()
setiosflags() setf()
resetiosflags() resetf()

for ex -
cout<<setw(5)<<x;

above statement is equivalent to following two statements

cout.width(5);
cout<<x;

User-defined manipulator

 We can create our own manipulators through a special


function.
 In such manipulator we can apply more than one formatting
through more simplified statement.
 Syntax -
ostream& <manipulator_name>(ostream &)
{
//set of instructions

Write example programs from lecture or create your own program.

KEDAR KABRA SIR - 9420213481 3


Exception handling
 Exceptions are run-time anomalies or unusual conditions that
a program may encounter. (Unexpected behaviour during
execution)
 For ex. If denominator of the division is zero, access to
the array outside its bound.
 C++ supports exception handling mechanism.
 It is built on three keywords- try, catch and throw.
 Format of exception handling is shown below.
try
{
______
______
throw(parameter)
______
______
}
catch(thrown parameter)
{
______
______

}
 Keyword try is used to preface or enclose the block of code
where exception may occur.
 When exception occurs, it is thrown by using keyword throw
in try block.
 Keyword catch, catches the exception and handles it
appropriately.
 The try and catch keywords come in pairs
 Multiple catch blocks are allowed.

KEDAR KABRA SIR - 9420213481 4


Catch all / Catch any
1. We can use catch(...) to catch any type of exception.
2. If exact type match is not found for thrown parameter then
catch any block catches the exception.
3. If catch any block is not present then program terminates
abnormally.

Program

#include<iostream>
using namespace std;
int main()
{
int x,y;
float d;

cout<<”\nEnter two integers”;


cin>>x>>y;

try
{
if(y==0)
throw(“\nDenominator can not be zero”);

d = float(x)/y;
cout<<”\nDivision =”<<d;
}
catch(char const *msg)
{
cout<<msg;
}

return 0;
}

KEDAR KABRA SIR - 9420213481 5


Templates
 Templates are a mechanism that make it possible to use one
function or class to handle many different data types.
 By using templates, we can design a single class/function
that operates on data of many types, instead of having to
create a separate class/function for each type.
 Syntax -
template<class T>
where, T is name of template

Advantages of templates.
1) Use of template eliminates unnecessary duplication of code.
2) Size of the program is reduced.
3) Errors are minimized.
4) We can create generic storage classes.

Types of templates
There are two types of templates
1) Function template –
If template is used as a data type of parameters of function then
it is referred as function template.

2) class template –
If template is used as data type of data member of class then it
is referred as class template.

Program-1 (Function template)


#include<iostream>
using namespace std;
template<class t>
T max(T a, T b)
{
if(a>b)

KEDAR KABRA SIR - 9420213481 6


return a;
else
return b;
}
int main()
{
cout<<”\nMaximum between two integers =”<<max(34,65);
cout<<”\nMaximum between two characters =”<<max(‘r’, ‘e’);
cout<<”\nMaximum between two floats =”<<max(4.5,2.3);

return 0;
}

Execute above program and check output.

Program-2 (class template)

#include<iostream>
using namespace std;
template<class T>
class Array
{
T a[10];

public:
void read()
{
int i;
cout<<”\nEnter elements”;
for(i=0;i<10;i++)
cin>>a[i];
}
void print()
{

KEDAR KABRA SIR - 9420213481 7


int i;
for(i=0;i<10;i++)
cout<<a[i]<<”\t”;
}
};
int main()
{
Array<int> a1;
Array<float> a2;

a1.read();
a2.read();

cout<<”\nArray-1”;
a1.print();
cout<<”\nArray-2”;
a2.print();

return 0;
}

KEDAR KABRA SIR - 9420213481 8

You might also like