Professional Documents
Culture Documents
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.
17
23
40
47
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.
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.
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;
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
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.
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
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;
}
};
Page
void main()
{
MyClass my_class;
SecondClass sec_class;
my_class.printMember();
sec_class.change( my_class, 5 );
my_class.printMember();
}
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
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
};
Page
int main(void)
{
Box Box1(3.3, 1.2, 1.5);
// Declare box1
// Declare box2
return 0;
}
When the above code is compiled and executed, it produces the following result:
Constructor called.
Constructor called.
Total objects: 2
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
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
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;
Page
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
Output:
Enter the Roll no: 100
90
80
: 260
Average: 86.66
Simple Program for VIRTUAL FUNCTIONS
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
};
void main()
{
clrscr();
base obj1;
base *p;
cout<<"\n\t P points to base:\n" ;
p=&obj1;
p->display();
p->show();
Page
P points to Drive
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
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()
{
Page
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
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();
}
Page