You are on page 1of 27

https://www.scribd.

com/doc/244495572/Lab-Manual-CPP

LAB MANUAL
C++ PROGRAMMING LAB
3CS10

INDEX
S. NO.
1.

TITLE
To write a simple program for understanding of C++
program structure without any CLASS declaration. Program
may be based on simple input output, understanding of

PAGE NO.
3

2.

3.
4.

keyword using.
Write a C++ program to demonstrate concept of declaration of class
with public & private member, constructors, object creation using
constructors, access restrictions, defining member functions within and
outside a class. Scope resolution operators, accessing an objects data
members and functions through different type of object handle name of
object, reference to object, pointer to object, assigning class objects to
each other.
Program involving multiple classes (without inheritance) to
accomplish a task. Demonstrate composition of class.
Demonstration Friend function friend classes and this

9
13

pointer.

5.
6.

7.
8.

Demonstration dynamic memory management using new


& delete & static class members.
Demonstration of restrictions an operator overloading,
operator functions as
member function and/ or friend function, overloading
stream insertion and stream extraction, operators,
overloading operators etc.
Demonstrator use of protected members, public & private
protected classes, multi-level inheritance etc.
Demonstrating multiple inheritance, virtual functions, virtual base

17
23

40
47

classes, abstract Classes.

Experiment No.-1
AIM: To write a simple program for understanding of C++ program structure without any
CLASS declaration. Program may be based on simple input output, understanding of keyword
using.
S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

ALGORITHM
1. Start the program.
2. No need to declare the CLASS.
3. Start with the use of using keyword.
4. Start the main block of the program and print Hello World! .
5. End of the program.
PROGRAM
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}

Experiment No.-2
AIM: Write a C++ program to demonstrate concept of declaration of class with public &
private member, constructors, object creation using constructors, access restrictions, defining
member functions within and outside a class. Scope resolution operators, accessing an objects
data members and functions through different type of object handle name of object, reference
to object, pointer to object, assigning class objects to each other.
S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

ALGORITHM

Constructors:
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle (int,int);
int area () {return (width*height);}
};
Rectangle::Rectangle (int a, int b) {
width = a;
height = b;
}
int main () {
Rectangle rect (3,4);
Rectangle rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
OUTPUT
rect area: 12
rectb area: 30
Scope resolution operators:
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area() {return width*height;}
};
void Rectangle::set_values (int x, int y) {
width = x;
height = y;
}
S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

int main () {
Rectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}

Experiment No.-3
AIM: Program involving multiple classes (without inheritance) to accomplish a
task. Demonstrate composition of class.

Experiment No.-4
AIM: Demonstration Friend function friend classes and this pointer.

To find the mean value of a given number using friend function


ALGORITHM:
1: Start the program.
2: Declare the class name as Base with data members and member functions.
3: The function get() is used to read the 2 inputs from the user.
4: Declare the friend function mean(base ob) inside the class.
5: Outside the class to define the friend function and do the following.
6: Return the mean value (ob.val1+ob.val2)/2 as a float.
7: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

class base
{
int val1,val2;
public:
void get()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float(ob.val1+ob.val2)/2;
}
void main()
{
clrscr();
base obj;
obj.get();
cout<<"\n Mean value is : "<<mean(obj);
getch();
}
Output:
Enter two values: 10, 20
Mean Value is: 15
Friend Functions
A C++ friend functions are special functions which can access the private members of a class.
They are considered to be a loophole in the Object Oriented Programming concepts, but
logical use of them can make them useful in certain cases. For instance: when it is not possible
to implement some function, without making private members accessible in them. This
situation arises mostly in case of operator overloading.

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

In the following example, the friend function print is a member of class TWO and accesses the
private data members a and b of class ONE.

#include <iostream>
using namespace std;

//Must be known to TWO


//before declaration of ONE.
class ONE;

class TWO
{
public:
void print(ONE& x);
};

class ONE
{
int a, b;
friend void TWO::print(ONE& x);
public:
ONE() : a(1), b(2) { }
};

void TWO::print(ONE& x)
{
S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

cout << "a is " << x.a << endl;


cout << "b is " << x.b << endl;
}
int main()
{
ONE xobj;
TWO yobj;
yobj.print(xobj);
}

Friend functions have the following properties:

1) Friend of the class can be member of some other class.

2) Friend of one class can be friend of another class or all the classes in one program,
such a friend is known as GLOBAL FRIEND.

3) Friend can access the private or protected members of the class in which they are
declared to be friend, but they can use the members for a specific object.

4) Friends are non-members hence do not get this pointer.

5) Friends, can be friend of more than one class, hence they can be used for message
passing between the classes.

6) Friend can be declared anywhere (in public, protected or private section) in the
class.

Friend Class
A class can also be declared to be the friend of some other class. When we create a friend class
then all the member functions of the friend class also become the friend of the other class. This
requires the condition that the friend becoming class must be first declared or defined
(forward declaration).
PROGRAM:
#include <iostream>
S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

using namespace std;

class MyClass
{
// Declare a friend class
friend class SecondClass;

public:
MyClass() : Secret(0){}
void printMember()
{
cout << Secret << endl;
}
private:
int Secret;
};

class SecondClass
{
public:
void change( MyClass& yourclass, int x )
{
yourclass.Secret = x;
}
};

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

void main()
{
MyClass my_class;
SecondClass sec_class;
my_class.printMember();
sec_class.change( my_class, 5 );
my_class.printMember();
}

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

Experiment No.-5
AIM: Demonstration dynamic memory management using new & delete & static class
members.
PROGRAM:
#include <iostream>
#include <new>
using namespace std;
int main()
{
int *p;
try
{
p = new int; //dynamic allocation of memory
}
catch (bad_alloc x)
{
cout << Memory allocation failed;
return 1;
}
*p = 100;
cout <<P has value<<*p;
delete p;
//free the dynamically allocated memory
return 0;
}
A static member is shared by all objects of the class. All static data is initialized to zero when
the first object is created, if no other initialization is present. We can't put it in the class
definition but it can be initialized outside the class as done in the following example by
redeclaring the static variable, using the scope resolution operator :: to identify which class it
belongs to.
Let us try the following example to understand the concept of static data members:
#include <iostream>
S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

using namespace std;

class Box
{
public:
static int objectCount;
// Constructor definition
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// Increase every time object is created
objectCount++;
}
double Volume()
{
return length * breadth * height;
}
private:
double length;

// Length of a box

double breadth;

// Breadth of a box

double height;

// Height of a box

};

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

// Initialize static member of class Box


int Box::objectCount = 0;

int main(void)
{
Box Box1(3.3, 1.2, 1.5);

// Declare box1

Box Box2(8.5, 6.0, 2.0);

// Declare box2

// Print total number of objects.


cout << "Total objects: " << Box::objectCount << endl;

return 0;
}
When the above code is compiled and executed, it produces the following result:
Constructor called.
Constructor called.
Total objects: 2

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

Experiment No.-6
AIM: Demonstration of restrictions an operator overloading, operator functions as member
function and/ or friend function, overloading stream insertion and stream extraction,
operators, overloading operators etc.
Experiment No.-7
AIM: Demonstrator use of protected members, public & private protected classes, multi-level
inheritance etc.
Program for MULTI-LEVEL INHERITANCE:
#include <iostream>
using namespace std;
class A
{
public:
void display()
{
cout<<"Base class content.";
}
};

class B : public A
{

};

class C : public B
{
S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

};

int main()
{
C c;
c.display();
return 0;
}

Experiment No.-8
AIM: Demonstrating multiple inheritance, virtual functions, virtual base classes, abstract
classes
S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

To find out the student details using MULTIPLE INHERITANCE


ALGORITHM:
1: Start the program.
2: Declare the base class student.
3: Declare and define the function get() to get the student details.
4: Declare the other class sports.
5: Declare and define the function getsm() to read the sports mark.
6: Create the class statement derived from student and sports.
7: Declare and define the function display() to find out the total and average.
8: Declare the derived class object,call the functions get(),getsm() and display().
9: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>

class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

cin>>m1>>m2;
}
};
class sports
{
protected:
int sm;

// sm = Sports mark

public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;

}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No
cout<<"\n\tAverage

: "<<rno<<"\n\tTotal

: "<<tot;

: "<<avg;

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
Output:
Enter the Roll no: 100

Enter two marks

90
80

Enter the Sports Mark: 90

Roll No: 100


Total

: 260

Average: 86.66
Simple Program for VIRTUAL FUNCTIONS

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

ALGORITHM:
1: Start the program.
2: Declare the base class base.
3: Declare and define the virtual function show().
4: Declare and define the function display().
5: Create the derived class from the base class.
6: Declare and define the functions display() and show().
7: Create the base class object and pointer variable.
8: Call the functions display() and show() using the base class object and pointer.
9: Create the derived class object and call the functions display() and show() using the
derived class object and pointer.
10: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void show()
{
cout<<"\n Base class show:";
}
void display()
{
cout<<"\n Base class display:" ;
}
S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

};

class drive:public base


{
public:
void display()
{
cout<<"\n Drive class display:";
}
void show()
{
cout<<"\n Drive class show:";
}
};

void main()
{
clrscr();
base obj1;
base *p;
cout<<"\n\t P points to base:\n" ;

p=&obj1;
p->display();
p->show();

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

cout<<"\n\n\t P points to drive:\n";


drive obj2;
p=&obj2;
p->display();
p->show();
getch();
}
Output:
P points to Base

Base class display


Base class show

P points to Drive

Base class Display


Drive class Show
To calculate the total mark of a student using the concept of VIRTUAL BASE CLASS
ALGORITHM:
1: Start the program.
2: Declare the base class student.
3: Declare and define the functions getnumber() and putnumber().
4: Create the derived class test virtually derived from the base class student.
5: Declare and define the function getmarks() and putmarks().

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

6: Create the derived class sports virtually derived from the base class student.
7: Declare and define the function getscore() and putscore().
8: Create the derived class result derived from the class test and sports.
9: Declare and define the function display() to calculate the total.
10: Create the derived class object obj.
11: Call the function get number(),getmarks(),getscore() and display().
12: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
void putnumber()
{
cout<<"\n\n\tRoll No:"<<rno<<"\n";
}
};
class test:virtual public student

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

{
public:
int part1,part2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Part1:";
cin>>part1;
cout<<"Part2:";
cin>>part2;
}
void putmarks()
{
cout<<"\tMarks Obtained\n";
cout<<"\n\tPart1:"<<part1;
cout<<"\n\tPart2:"<<part2;
}
};
class sports:public virtual student
{

public:
int score;
void getscore()
{

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

cout<<"Enter Sports Score:";


cin>>score;
}
void putscore()
{
cout<<"\n\tSports Score is:"<<score;
}
};

class result:public test,public sports


{
int total;
public:
void display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\tTotal Score:"<<total;
}
};
void main()
{
result obj;

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}
Output:
Enter Roll No: 200

Enter Marks

Part1: 90
Part2: 80
Enter Sports Score: 80

Roll No: 200


Marks Obtained
Part1: 90
Part2: 80
Sports Score is: 80
Total Score is: 250

Demonstration of ABSTRACT CLASS:

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

#include <iostream.h>
#include <conio.h>
const int max = 80;
class first
{
protected:
char name[max];
char cls [max] ;
public:
virtual void insert()=0;
virtual void show()=0;
};
class second: public first
{
protected:
int fees;
public:
void insert()
{
cout<<"Name ";
cin>>name;
cout<<"Class ";
S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

cin>>cls;
cout<<"Fees ";
cin>>fees;
}
void show()
{
cout<<"\nName : "<<name<<"\n";
cout<<"Class: "<<cls<<"\n";
cout<<"Fees : "<<fees<<"\n";
}
};
void main()
{
clrscr();
second s1;
s1.insert();
s1.show();
getch();
}

S.I.E.T/CSE/ C++ PROGRAMMING LAB-3CS10

Page

You might also like