0% found this document useful (0 votes)
24 views24 pages

C M M Lab Manual

Uploaded by

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

C M M Lab Manual

Uploaded by

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

M.S.P.

Mandal’s
Shri Shivaji Institute of Engineering & Management Studies,
Vasmat Road, Parbhani – 431 401 (M.S.)

C++ PROGRAMMING LAB (BTCOC305(B))


LAB MANUAL
For the Academic Year 2024 - 2025 [R18]
II B.Tech / III Semester

DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 1


C++ PROGRAMMING LAB (CS305PC)
LAB MANUAL II B.Tech - III Semester [R18]

Shri Shivaji Institute of Engineering &


Management Studies,
Vasmat Road, Parbhani – 431 401 (M.S.)

Department of Computer Science & Engineering

Institute Vision and Mission

Vision:

“Our vision is to become a beacon of hope and empowerment in the backward region,
transforming lives through technical education, innovation, and sustainable
development.”

Mission:

We aspire to provide unparalleled access to quality education in the technical field


for socially and economically backward classes, ensuring that no deserving student is
left behind due to financial constraints.

We are committed to fostering the educational and cultural development of the


rural population, enriching communities with knowledge and opportunities for
growth.

We aim to set the standard for educational facilities by providing top-tier


amenities, including state-of-the-art hostel accommodation, comprehensive physical
education programs, and value-based education that nurtures character and
integrity.

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 2


Through education, we seek to catalyze social transformation, uplifting the common people

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 3


and empowering them to shape their own destinies.

We are dedicated to promoting intellectual, ethical, and cultural values among our students,
while equipping them with the technical and professional skills needed to thrive in today’s
competitive job market, thereby contributing to economic development.

We envision creating a wide-reaching technical education network that encourages mass


participation in education, breaking down barriers and opening doors to a brighter future
for.

Program Outcomes (PO’s):

P01:-

The graduates will possess the knowledge of various discrete mathematical systems.

PO2:-

The graduates will have an ability to apply mathematical formalism of Finite Automata and Probability.
in modeling and analysis of systems.

PO3:-

The graduates will have knowledge of core programming paradigms such as database orientation, object
orientation, and agent orientation and concepts essential to implement software based system.

PO4:-

The graduates will have an ability to analyze problem, specify algorithmic solutions to them and to
evaluate alternative solutions.

P05:-

The graduate will have broad understanding of the impact of a computer based solutions in economic,
environmental and social context and will demonstrate use of analytical tools in gathering requirements.
and distilling relevant information to provide computer based solutions.

PO6:-

The graduates will demonstrate the ability to build human centric interfaces to computers.

PO7:-

The graduates will posses the knowledge of advanced and emerging

topics PO8:-

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 4


In the fields of operating systems, databases and computer networks. The graduates will posses skills
necessary to communicate design

PO9:-

Engineering ideas. The skills set include verbal, written and listening skills. The graduates will
understand ethical issues in providing computer based solutions also they will have an ability and.
attitude to address the ethical issues.

PO10:-

The graduates will understand the role of system software such as operating systems, database.
management systems, compilers, middle-ware and internet protocols in realizing distributed information
environment.

1. LAB OBJECTIVE

• Introduces object-oriented programming concepts using the C++ language.

• Introduces the principles of data abstraction, inheritance and polymorphism;

• Introduces the principles of virtual functions and polymorphism

• Introduces handling formatted I/O and unformatted I/O

• Introduces exception handling

2. LAB OUTCOME

Upon successful completion of this Lab the student will be able to:

• Ability to develop applications for a range of problems using object-oriented


programming techniques

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 5


List of Lab Experiments as per DBATU Syllabus

1. Programs on operators, Arithmetic promotion, Method calling.

2. Programs on dealing with array.

3. Programs on Classes: String and Math

4. Programs on inheritance and polymorphism.

5. Programs on Dynamic polymorphism.

6. Programs on Garbage collection, packaging, access modifiers as well as static and


abstract modifiers.

7. Programs on interfaces block initializers, final modifiers as well as static and


dynamic binding.

8. Programs on file handling and stream manipulation.

9. Programs on Dynamic memory management.

10. Programs on Exception handling.

11. Programs on generic Programming using templates.

12. Programs on STL containers and iterators.

Reference Books :
1. The C++ Programming Language, 3rd Edition, B. Strattera, Pearson Education.
2. OOP in C++, 3rd Edition, T. Gaddis, J. Walters and G. Muganda, Wiley Dream Tech Press.

3. Object Oriented Programming in C++, 6th Edition E Balagurusamy.

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 6


1. Programs on operators, Arithmetic promotion, Method calling.
#include <iostream>
using namespace
std; class math
{
public:
int i,j;
float cal;
void get()
{
cout<<”Enter two numbers”<<endl;
cin>>i>>j;
}
void sum()
{
cal=i+j;
cout<<”sum of two numbers is”<<cal<<endl;
}
void sub()
{
cal=i-j;
cout<<”sub of two numbers is”<<cal<<endl;
}
void multi()
{
cal=i*j;
cout<<”multi of two number is”<<cal<<endl;
}
void div()
{
cal=i/j;
cout<<”div of two numbers is”<<cal<<endl;
}
void mod()
{

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 7


cal=i%j;
cout<<”mod of two numbers is”<<cal<<endl;
}
};

int main() {
math obj ;
obj.get();
obj.sum();
obj.sub();
obj.multi();
obj.div();
obj.mod();
return 0;
}

Output:-
Enter two numbers 5 7
sum of two numbers is12
sub of two numbers is-2
multi of two number is35
div of two numbers is0
mod of two numbers is5

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 8


2. Programs on dealing with array.
#include <iostream>
using namespace
std; class Employee
{
public:
int id;
char name[30];
void getdata()
{
cout<<”Enter id”<<endl;
cin>>id;
cout<<”Enter name”<<endl;
cin>>name;
}
void putdata()
{
cout<<”id “<<id<<endl;
cout<<”name “<<name<<endl;
}
};
int main() {
Employee emp[30];
int n,i;
cout<<”Enter no. of employee”<<endl;
cin>>n;
for(i=0;i<=n;i++)
{
emp[i].getdata();
}
for(i=0;i<=n;i++)
{
emp[i].putdata();
}
return 0;}
Output:-
Enter no. of employee 1
Enter id 123
Enter name Vaishnavi
Enter id 345
Enter name Rohini
id 123
name Vaishnavi
id 345

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 9


name Rohini

3. Programs on Classes: String and Math


1] string

#include <iostream>
#include<cstring>
#include <string>
using namespace std;
int main(){
string myadd="Learn coding";
cout<<"Original address is:"<<myadd<<endl;
myadd.push_back('A');
cout<<"modified address is:"<<myadd<<endl;
myadd.pop_back();
cout<<"modified address is:"<<myadd;
return 0;
}
Output:-
Original address is:Learn coding
modified address is:Learn
codingA modified address
is:Learn coding

2] string

#include <iostream>
#include<cstring>
#include <string>
using namespace std;
int main(){
char str[]="Vaishnavi";
string strl="ABC";
int l=strlen(str);
cout<<l<<endl;
string reverse(strl.rbegin(),strl.rend());
cout<<reverse<<endl;
char str2[10];
char str3[]=" Udawant";
char str4[]="Vaishu";
strcat(str,str3);
cout<<str<<endl;
int value=strcmp(str,str4);
if (value==0)
{

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 10


cout<<"string are equal"<<endl;
}
else
{
cout<<"strings are not equal"<<endl;
}
return 0;
}
Output:-
9
CBA
Vaishnavi Udawant
strings are not equal

3] string

#include <iostream>
#include<string>
using namespace std;
int main() {
char myname[20];
cout<<"Enter your name:-";
cin>>myname ;
cout<<"My name is:-"<<myname<<endl;
string myaddress;
cout<<"Enter your address:-";
cin>>myaddress;
cout<<"full address is:"<<myaddress;
return 0;
}
Output:-
Enter your name:-vgudawant
My name is:-vgudawant
Enter your address:-khanapurNagarPbn
full address is:khanapurNagarPbn

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 11


4] Math

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
double x = 2.3;
cout << "Sine value of x=2.3 : " << sin(x) << endl;
cout << "Cosine value of x=2.3 : " << cos(x) << endl;
cout << "Tangent value of x=2.3 : " << tan(x) <<
endl;

double y = 0.25;
cout << "Square root value of y=0.25 : " << sqrt(y)
<< endl;

int z = -10;
cout << "Absolute value of z=-10 : " << abs(z) << endl;
cout << "Power value: x^y = (2.3^0.25) : " << pow(x, y)
<< endl;

x = 3.0;
y = 4.0;
cout << "Hypotenuse having other two sides as x=3.0 and"
<< " y=4.0 : " << hypot(x, y) << endl;

x = 4.56;
cout << "Floor value of x=4.56 is : " << floor(x)
<< endl;

x = -4.57;
cout << "Absolute value of x=-4.57 is : " << fabs(x)
<< endl;

x = 1.0;
cout << "Arc Cosine value of x=1.0 : " << acos(x)
<< endl;

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 12


cout << "Arc Sine value of x=1.0 : " << asin(x) <<
endl; cout << "Arc Tangent value of x=1.0 : " <<
atan(x)
<< endl;

y = 12.3;
cout << "Ceiling value of y=12.3 : " << ceil(y) << endl;

x = 57.3; // in radians
cout << "Hyperbolic Cosine of x=57.3 : " << cosh(x)
<< endl;
cout << "Hyperbolic tangent of x=57.3 : " << tanh(x)
<< endl;

y = 100.0;
// Natural base with 'e'
cout << "Log value of y=100.0 is : " << log(y) << endl;

return 0;
}

Output:-
Sine value of x=2.3 : 0.745705
Cosine value of x=2.3 : -
0.666276 Tangent value of x=2.3
: -1.11921 Square root value of
y=0.25 : 0.5 Absolute value of
z=-10 : 10
Power value: x^y = (2.3^0.25) : 1.23149
Hypotenuse having other two sides as x=3.0 and y=4.0 : 5
Floor value of x=4.56 is : 4
Absolute value of x=-4.57 is : 4.57
Arc Cosine value of x=1.0 : 0
Arc Sine value of x=1.0 : 1.5708
Arc Tangent value of x=1.0 :
0.785398 Ceiling value of y=12.3 : 13
Hyperbolic Cosine of x=57.3 :
3.83746e+24 Hyperbolic tangent of x=57.3
:1
Log value of y=100.0 is : 4.60517

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 13


4. Programs on inheritance and polymorphism.
1] Basic Inheritance

#include <iostream>
using namespace
std; class student
{
public:
void display()
{
cout<<"This is student call"<<endl;
}
};
class Teacher : public student
{
public:
void show()
{
cout<<"This is teacher call";
}
};
int main() {
Teacher Tech;
Tech.display();
Tech.show();
return 0;
}
Output:-
This is student
call This is
teacher call

2] Hierarchical Inheritance

#include<iostream>

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 14


using namespace std;
class student
{
public:
void show_student()
{
cout<<"This is a student call"<<endl;
}
};
class Engineer : public student
{
public:
void show_Eng()
{
cout<<"This is Engineer call"<<endl;
}
};
class Medical : public student
{
public:
void show_Med()
{
cout<<"This is Medical call"<<endl;
}
};
int main(){
Engineer Eg;
Medical Md;
Eg.show_Eng();
Md.show_Med();
Eg.show_student();
Md.show_student();
return 0;
}
Output:-
This is Engineer call
This is Medical call
This is a student call
This is a student call

3] Hybrid Inheritance

#include<iostream>
using namespace std;

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 15


class student
{
public:
int rollNo;
void get_rollNo()
{
cout<<"Enter roll
no:"; cin>>rollNo;
}
void show_rollNo()
{
cout<<"The roll no.is:"<<rollNo<<endl;
}
};
class Test : public student
{
public:
int sub1, sub2;
void get_sub()
{
cout<<"Enter sub1 and
sub2:"; cin>>sub1>>sub2;
}
void show_sub()
{
cout<<"The marks of sub1 and sub2 are:"<<sub1<<"and"<<sub2<<endl;
}
};
class sports
{
public:
int sportMarks;
void get_spm()
{
cout<<"Enter sportmarks:";
cin>>sportMarks;
}
void show_spm()
{
cout<<"The sport marks is:"<<sportMarks<<endl;
}
};
class result : public Test, public sports
{

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 16


public:
int total;
void display()
{
total=sub1+sub2+sportMarks;
cout<<"The total marks is="<<total;
}
};
int main(){
result R;
R.get_rollNo();
R.show_rollNo();
R.get_sub();
R.show_sub();
R.get_spm();
R.show_spm();
R.display();
return 0;
}
Output:-
Enter roll no:1
The roll
no.is:1
Enter sub1 and sub2:60 80
The marks of sub1 and sub2
are:60and80 Enter sportmarks:70
The sport marks is:70
The total marks is=210

4] Multiple Inheritance

#include <iostream>
using namespace
std; class A
{
public:
int a;
void get_a()
{
cout<<"Enter value of a:";
cin>>a;
}
void show_a()
{

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 17


cout<<"The value of a is:"<<a<<endl;
}
};
class B
{
public:
int b;
void get_b()
{
cout<<"Enter value of b:";
cin>>b;
}
void show_b()
{
cout<<"value of b is:"<<b<<endl;
}
};
class C :public A, public B
{
public:
int c;
void sum()
{
c = a+b;
cout<<"sum of a and b is ="<<c<<endl;
}
};
int main() {
C obj;
obj.get_a();
obj.show_a();
obj.get_b();
obj.show_b();
obj.sum();
return 0;
}
Output :-
Enter value of a:5
The value of a
is:5 Enter value of
b:8 value of b is:8
sum of a and b is =13

5] Multilevel Inheritance

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 18


#include<iostream>
using namespace std;
class A
{
public:
int rollNo;
void get_rollNo()
{
cout<<"Enter roll
no:"; cin>>rollNo;
}
void show_rollNo()
{
cout<<"The roll no.is:"<<rollNo<<endl;
}
};
class B
{
public:
int sub1, sub2;
void get_sub()
{
cout<<"Enter sub1 and
sub2:"; cin>>sub1>>sub2;
}
void show_marks()
{
cout<<"The marks of sub1 and sub2 are:"<<sub1<<sub2<<endl;
}
};
class C : public A, public B
{
public:
int sportMarks,
Total; void
get_spm()
{
cout<<"Enter sportmarks:";
cin>>sportMarks;
}
void show_spm()
{
cout<<"The sport marks is:"<<sportMarks<<endl;
}

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 19


void sum()
{
Total=sub1+sub2+sportMarks;
cout<<"The total marks
is="<<Total;
}
};
int main(){
C obj;
obj.get_rollNo();
obj.show_rollNo();
obj.get_sub();
obj.show_marks();
obj.get_spm();
obj.show_spm();
obj.sum();
return 0;
}
Output:-
Enter roll no:4
The roll
no.is:4
Enter sub1 and sub2:70 60
The marks of sub1 and sub2 are:70 60
Enter sportmarks:80
The sport marks is:80
The total marks is=210

6] Private Inheritance

#include <iostream>
using namespace
std; class B{
int a;
public:
int b;
void get_ab(){
cout << "Enter a & b:
"; cin >> a >> b;
}
int get_a() {
return a;
}
void show_a() {
cout << "Value of a is " << a << endl;
}
C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 20
};

class D : private B {
int c;
public:
void mul() {
get_ab();
c = b * get_a();
}
void display() {
show_a();
cout << "b = " << b << "\nc = " << c << endl; }};
int main() {
D d;
d.mul();
d.display();
return 0;}

Output:-
Enter a & b: 4
6 Value of a is
4b=6
c = 24

7] Public Inheritance

#include <iostream>
using namespace
std; class B{
int a;
public:
int b;
void getab(){
cout << "Enter a and b:
"; cin >> a >> b;
}
int geta()
{
return a;
}
void showa()
{
cout << "Value of a is: " << a << endl;
}

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 21


};
class D : public B
{ int c;
public:
void mul() {
c = b * geta();
}
void display()
{ showa();
cout << "b: " << b << "\n" << "c: " << c << endl;
}
};
int main() {
D d;
d.getab();
d.mul();
d.display();
d.b = 10;
return 0;
}
Output:-
Enter a and b: 8
9 Value of a is: 8
b: 9
c: 72

5. Programs on Dynamic polymorphism.


#include <iostream>
using namespace
std; class shape
{
public:
virtual void calculate_area()
{
cout<<"area of shape ";
}
};
class Rectangle : public shape
{
public:
int l,b;
float area;
void calculate_area()

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 22


{
cout<<"Enter l and
b:"; cin>>l>>b;
area=l*b;
cout<<"area of rectangle is:"<<area<<endl;
}
};
class square : public shape
{
public:
int s;
float area;
void calculate_area()
{
cout<<"Enter s:";
cin>>s;
area=s*s;
cout<<"area of square is:"<<area<<endl;
}
};

int main()
{
shape *ptr;
shape obj1;
Rectangle obj2;
square obj3;
ptr=&obj2;
ptr->calculate_area();
ptr=&obj3;
ptr->calculate_area();
ptr=&obj1;
ptr->calculate_area();
return 0;
}
Output:-
Enter l and b:6 5

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 23


area of rectangle
is:30 Enter s:8
area of square is:64
area of shape

C++ Programming Lab Manual / II-III SEM / 2024-2025 Page 24

You might also like