0% found this document useful (0 votes)
11 views29 pages

C++ Lab r18

The document outlines a series of C++ programming lab experiments, including tasks such as creating classes, using structures, managing memory allocation, and implementing inheritance. Each experiment provides a specific aim along with example code and expected outputs to demonstrate the concepts. The topics cover various aspects of C++ programming, including console I/O operations, scope resolution, and employee salary calculations.

Uploaded by

Sree Latha
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)
11 views29 pages

C++ Lab r18

The document outlines a series of C++ programming lab experiments, including tasks such as creating classes, using structures, managing memory allocation, and implementing inheritance. Each experiment provides a specific aim along with example code and expected outputs to demonstrate the concepts. The topics cover various aspects of C++ programming, including console I/O operations, scope resolution, and employee salary calculations.

Uploaded by

Sree Latha
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/ 29

CS309PC: C++ PROGRAMMING LAB

LIST OF EXPERIMENTS
1. Write a C++ Program to display Names, Roll No., and grades of 3 students who have
appeared in the examination. Declare the class of name, Roll No. and grade. Create an array
of class objects. Read and display the contents of the array.

2. Write a C++ program to declare Strut. Initialize and display contents of member variables.

3. Write a C++ program to declare a class. Declare pointer to class. Initialize and display the
Contents of the class member.

4. Given that an EMPLOYEE class contains following members: data members: Employee
number, Employee name, Basic, DA, IT, Net Salary and print data members.

5. Write a C++ program to read the data of N employee and compute Net salary of each
employee (DA=52% of Basic and Income Tax (IT) =30% of the gross salary).

6. Write a C++ to illustrate the concepts of console I/O operations.

7. Write a C++ program to use scope resolution operator. Display the various values of the same
variables declared at different scope levels.

8. Write a C++ program to allocate memory using new operator.

9. Write a C++ program to create multilevel inheritance. (Hint: Classes A1, A2, A3)

10. Write a C++ program to create an array of pointers. Invoke functions using array objects.

11. Write a C++ program to use pointer for both base and derived classes and call the member

Function. Use Virtual keyword.


Aim: 1. Write a C++ Program to display Names, Roll No., and grades of 3 students who have
appeared in the examination. Declare the class of name, Roll No. and grade. Create an array of
class objects. Read and display the contents of the array

#include <iostream>

using namespace std;

struct student

char name[50];

int roll;

float marks;

} s[10];

int main()

cout << "Enter information of students: " << endl;

// storing information

for(int i = 0; i < 10; ++i)

s[i].roll = i+1;

cout << "For roll number" << s[i].roll << "," << endl;
cout << "Enter name: ";

cin >> s[i].name;

cout << "Enter marks: ";

cin >> s[i].marks;

cout << endl;

cout << "Displaying Information: " << endl;

// Displaying information

for(int i = 0; i < 10; ++i)

cout << "\nRoll number: " << i+1 << endl;

cout << "Name: " << s[i].name << endl;

cout << "Marks: " << s[i].marks << endl;

return 0;

}
Output

Enter information of students:

For roll number1,

Enter name: Tom

Enter marks: 98

For roll number2,

Enter name: Jerry

Enter marks: 89

Displaying Information:

Roll number: 1

Name: Tom

Marks: 98

.
Aim: 2. Write a C++ program to declare Strut. Initialize and display contents of member
variables.

#include <iostream>

using namespace std;

struct student

char name[50];

int roll;

float marks;

};

int main()

student s;

cout << "Enter information," << endl;

cout << "Enter name: ";

cin >> s.name;

cout << "Enter roll number: ";

cin >> s.roll;

cout << "Enter marks: ";

cin >> s.marks;

cout << "\nDisplaying Information," << endl;

cout << "Name: " << s.name << endl;


cout << "Roll: " << s.roll << endl;

cout << "Marks: " << s.marks << endl;

return 0;

Output

Enter information,

Enter name: Bill

Enter roll number: 4

Enter marks: 55.6

Displaying Information,

Name: Bill

Roll: 4

Marks: 55.6

Aim : 3. Write a C++ program to declare a class. Declare pointer to class. Initialize and display
the contents of the class member
#include <iostream>

using namespace std;

class Box {

public:

// 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;

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

};

int main(void) {

Box Box1(3.3, 1.2, 1.5); // Declare box1

Box Box2(8.5, 6.0, 2.0); // Declare box2

Box *ptrBox; // Declare pointer to a class.


// Save the address of first object

ptrBox = &Box1;

// Now try to access a member using member access operator

cout << "Volume of Box1: " << ptrBox->Volume() << endl;

// Save the address of second object

ptrBox = &Box2;

// Now try to access a member using member access operator

cout << "Volume of Box2: " << ptrBox->Volume() << endl;

return 0;

When the above code is compiled and executed, it produces the following result −

Constructor called.

Constructor called.

Volume of Box1: 5.94

Volume of Box2: 102

Aim : 4. Given that an EMPLOYEE class contains following members: data members: Employee

number, Employee name, Basic, DA, IT, Net Salary and print data members.
#include<iostream.h>

#include<conio.h>

class employee

int emp_num;

char emp_name[20];

float emp_basic;

float sal;

float emp_da;

float net_sal;

float emp_it;

public:

void get_details();

void find_net_sal();

void show_emp_details();

};

void employee :: get_details()

cout<<"\nEnter employee number:\n";

cin>>emp_num;
cout<<"\nEnter employee name:\n";

cin>>emp_name;

cout<<"\nEnter employee basic:\n";

cin>>emp_basic;

void employee :: find_net_sal()

emp_da=0.52*emp_basic;

emp_it=0.30*(emp_basic+emp_da);

net_sal=(emp_basic+emp_da)-emp_it;

void employee :: show_emp_details()

cout<<"\n\n\nDetails of : "<<emp_name;

cout<<"\n\nEmployee number: "<<emp_num;

cout<<"\nBasic salary : "<<emp_basic;

cout<<"\nEmployee DA : "<<emp_da;

cout<<"\nIncome Tax : "<<emp_it;

cout<<"\nNet Salary : "<<net_sal;

int main()
{

employee emp[10];

int i,num;

clrscr();

cout<<"\nEnter number of employee details\n";

cin>>num;

for(i=0;i<num;i++)

emp[i].get_details();

for(i=0;i<num;i++)

emp[i].find_net_sal();

for(i=0;i<num;i++)

emp[i].show_emp_details();

getch();

return 0;

Aim : 5 Write a C++ program to read the data of N employee and compute Net salary of each

employee (DA=52% of Basic and Income Tax (IT) =30% of the gross salary
#include<iostream.h>

#include<conio.h>

#define SIZE 5

class emp

float basic,da,it,netsal;

char name[20],num[10];

public:

void getdata();

void net_sal();

void dispdata();

};

void emp::getdata()

cout<<"\n Enter employee number: " ;

cin>>name;

cout<<" Enter employee name: " ;

cin>>num;

cout<<"Enter employee basic salary in Rs: " ;

cin>>basic;

}
void emp::net_sal()

da=((0.52)*basic );

float gsal=da+basic;

it=((0.3)*gsal);

netsal=gsal-it;

void emp::dispdata()

cout

<<"\n Employee number: "<<name

<<"\n Employee name: "<<num

<<"\n Employee netsalary: "<<netsal<<" Rs.";

void main()

clrscr();

emp ob[SIZE];

int n;

cout<<"\n\n***********************************"

<<"\nCalculation of Employee Net Salary"


<<"\n***********************************"

<<"\n Enter the number of employees";

cin>>n;

for(int i=0;i<n;i++)

ob[i].getdata();

ob[i].net_sal();

clrscr();

cout<<"\n-----------------"

<<"\nEmployee Detail::"

<<"\n-----------------";

for( i=0;i<n;i++)

cout<<"\n\n Employee:"<<i+1

<<"\n ----------";

ob[i].dispdata();

getch();

Out put:
Aim : 6 Write a C++ to illustrate the concepts of console I/O operations

In case of C++ it uses streams to perform input and output operations in standard input output
devices (keyboard and monitor). A stream is an object which can either insert or extract the
character from it.

The standard C++ library is iostream and standard input / output functions in C++ are:

Cin

cout

There are mainly two types of consol I/O operations form:


Unformatted consol input output

Formatted consol input output

1) Unformatted consol input output operations

These input / output operations are in unformatted mode. The following are operations of
unformatted consol input / output operations:

A) void get()

It is a method of cin object used to input a single character from keyboard. But its main
property is that it allows wide spaces and newline character.

Syntax:

char c=cin.get();

Example:

Copy

#include<iostream>

using namespace std;

int main()

char c=cin.get();

cout<<c<<endl;

return 0;

Output

B) void put()
It is a method of cout object and it is used to print the specified character on the screen or
monitor.

Syntax:

cout.put(variable / character);

Example:

Copy

#include<iostream>

using namespace std;

int main()

char c=cin.get();

cout.put(c); //Here it prints the value of variable c;

cout.put('c'); //Here it prints the character 'c';

return 0;

Output

Aim: 7 write a C++ program to use scope resolution operator. Display the various values of the

same variables declared at different scope levels


1) To access a global variable when there is a local variable with same name:

filter_none

edit

play_arrow

brightness_4

// C++ program to show that we can access a global variable

// using scope resolution operator :: when there is a local

// variable with same name

#include<iostream>

using namespace std;

int x; // Global x

int main()

int x = 10; // Local x

cout << "Value of global x is " << ::x;

cout << "\nValue of local x is " << x;

return 0;

Output:

Value of global x is 0

Value of local x is 10
2) To define a function outside a class.

filter_none

edit

play_arrow

brightness_4

// C++ program to show that scope resolution operator :: is used

// to define a function outside a class

#include<iostream>

using namespace std;

class A

public:

// Only declaration

void fun();

};

// Definition outside class using ::

void A::fun()

cout << "fun() called";

}
int main()

A a;

a.fun();

return 0;

Output:

fun() called

3) To access a class’s static variables.

filter_none

edit

play_arrow

brightness_4

// C++ program to show that :: can be used to access static

// members when there is a local variable with same name

#include<iostream>

using namespace std;

class Test
{

static int x;

public:

static int y;

// Local parameter 'a' hides class member

// 'a', but we can access it using ::

void func(int x)

// We can access class's static variable

// even if there is a local variable

cout << "Value of static x is " << Test::x;

cout << "\nValue of local x is " << x;

};

// In C++, static members must be explicitly defined

// like this

int Test::x = 1;

int Test::y = 2;

int main()

{
Test obj;

int x = 3 ;

obj.func(x);

cout << "\nTest::y = " << Test::y;

return 0;

Output:

Value of static x is 1

Value of local x is 3

Test::y = 2;

Aim: 8 Write a C++ program to allocate memory using new operator

#include <iostream>

using namespace std;

int main ()

// Pointer initialization to null

int* p = NULL;

// Request memory for the variable

// using new operator


p = new(nothrow) int;

if (!p)

cout << "allocation of memory failed\n";

else

// Store value at allocated address

*p = 29;

cout << "Value of p: " << *p << endl;

// Request block of memory

// using new operator

float *r = new float(75.25);

cout << "Value of r: " << *r << endl;

// Request block of memory of size n

int n = 5;

int *q = new(nothrow) int[n];

if (!q)

cout << "allocation of memory failed\n";

else

for (int i = 0; i < n; i++)

q[i] = i+1;
cout << "Value store in block of memory: ";

for (int i = 0; i < n; i++)

cout << q[i] << " ";

// freed the allocated memory

delete p;

delete r;

// freed the block of allocated memory

delete[] q;

return 0;

Output:

Value of p: 29

Value of r: 75.25

Value store in block of memory: 1 2 3 4 5

Aim: 9. Write a C++ program to create multilevel inheritance. (Hint: Classes A1, A2, A3)

// inheritance.cpp
#include <iostream>

using namespace std;

class base //single base class

public:

int x;

void getdata()

cout << "Enter value of x= "; cin >> x;

};

class derive1 : public base // derived class from base class

public:

int y;

void readdata()

cout << "\nEnter value of y= "; cin >> y;

};

class derive2 : public derive1 // derived from class derive1

private:

int z;
public:

void indata()

cout << "\nEnter value of z= "; cin >> z;

void product()

cout << "\nProduct= " << x * y * z;

};

int main()

derive2 a; //object of derived class

a.getdata();

a.readdata();

a.indata();

a.product();

return 0;

} //end of program

Output

Enter value of x= 2

Enter value of y= 3

Enter value of z= 3
Product= 18

Aim: 10. Write a C++ program to create an array of pointers. Invoke functions using array
objects

#include<stdio.h>

int main()

// Pointer to an integer

int *p;

// Pointer to an array of 5 integers

int (*ptr)[5];

int arr[5];

// Points to 0th element of the arr.

p = arr;

// Points to the whole array arr.

ptr = &arr;

printf("p = %p, ptr = %p\n", p, ptr);

p++;

ptr++;
printf("p = %p, ptr = %p\n", p, ptr);

return 0;

Output:

p = 0x7fff4f32fd50, ptr = 0x7fff4f32fd50

p = 0x7fff4f32fd54, ptr = 0x7fff4f32fd64

Aim: 11. Write a C++ program to use pointer for both base and derived classes and call the
member function. Use Virtual keyword

// CPP program to illustrate

// concept of Virtual Functions

#include<iostream>

using namespace std;

class base

public:

virtual void print ()

{ cout<< "print base class" <<endl; }

void show ()

{ cout<< "show base class" <<endl; }


};

class derived:public base

public:

void print ()

{ cout<< "print derived class" <<endl; }

void show ()

{ cout<< "show derived class" <<endl; }

};

int main()

base *bptr;

derived d;

bptr = &d;

//virtual function, binded at runtime

bptr->print();

// Non-virtual function, binded at compile time

bptr->show();

Output:

print derived class

show base class

You might also like