OOPM lab manual using c++
OOPM lab manual using c++
Algorithm:
1. Define the class consisting the members of name, rollno and grade of the student.
2. Read the student name, roll number and grade.
3. Print the student name, roll number and grade.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int k=0;
Class stud
{
public: char name[12];
int rollno;
char grade[2];
};
class stud st[3];
while(k<3)
{ clrscr();
gotoxy(2,4);
cout<<”Name”;
gotoxy(17,4);
cin>>st[k].name;
gotoxy(2,5);
cout<<”Roll Number”;
gotoxy(17,5);
cin>>st[k].rollno;
cout<<”Grade”;
gotoxy(17,6);
cin>>st[k].grade;
st[k].grade[1]=’\0’;
puts(“press any key..”);
getch();
k++;
}
k=0;
clrscr();
cout<<”\n Name\tRoll No.\t Grade\n”;
while(k<3)
{
cout<<st[k].name<<”\t”<<st[k].rollno<<”\t”<<st[k].grade<<”\n”;
k++;
}
}
Output:
Name RollNo Grade
Balaji 50 A
Manoj 51 B
Sanjay 55 C
2 Write a C++ program to declare struct. Initialize and display contents of member
variables.
Algorithm:
1. Define the structure consisting of members name , roll number and marks of the student
2. Read the student name, roll number and marks of the student.
3. Print the student name, roll number and marks of the student.
Source Code:
#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
3. Write a C++ program to declare a class . Declare pointer to class. Initialize and display
the contents of the class members
Algorithm:
Output:
5
5
4. Given that EMPLOYEE class contains following members: data members: Employee
number, Employee name, Basic, DA, IT, Net Salary, and print data members.
Source Code:
#include<iostream>
using namespace std;
class employee
{
Int empno;
Char empname[50];
Int basic;
Int da;
Int it;
Int gs;
Int ns;
Public:
Void getdata()
{
Cout<<”enter the employee number”;
Cin>>empno;
Cout<<”enter the employee name”;
Cin>>empname;
Cout<<”enter the basic salary,da,it”;
Cin>>basic>>da>>it;
Gs=da+it;
Ns=gs+basic
}
Void putdata()
{
Cout<<”\nEmployee number”<<empno;
Cout<<”\nEmployee name”<<empname;
Cout<<”\nbasic\tda\tit\n”<<basic<<da<<it;
Cout<<”\n gross salary\n net salary”<<gs<<ns;
}
};
Void main()
{
Employee e;
e.getdata();
e.putdata();
}
Output:
Enter the employee number 101
Enter the employee name Rakesh
Enter the basic salary , da, it
15000
500
600
Employee number101
Employee name Rakesh
Basic da ta 15000 500 600
Gross salary1100 netsalary 16100
5. Write a C++ program to read the data of N employee and compute the net salary of each
employee (DA=52% of basic and IT=30% of gross salary)
Source Code
#include<iostream>
using namespace std;
class employee
{
Int empno;
char empname[50];
int basic;
int da;
int it;
int gs;
int ns;
public:
void getdata()
{
Cout<<”enter the employee number”;
Cin>>empno;
Cout<<”enter the employee name”;
Cin>>empname;
Cout<<”enter the basic salary,da,it”;
Cin>>basic>>da>>it;
Gs=basic+da+it;
Ns=
}
Void putdata()
{
Cout<<”\nEmployee number”<<empno;
Cout<<”\nEmployee name”<<empname;
Cout<<”\nbasic\tda\tit\n”<<basic<<da<<it;
Cout<<”\n gross salary\n net salary”<<gs<<ns;
}
};
Void main()
{
Employee e[20];
Int n,i;
Cout<<”enter the number of employees”;
Cin>>n;
For(i=0;i<n;i++)
{
Cout<<”enter the employee <<i<< details<<”\n”;
e[i].getdata();
e[i].putdata();
}
delete[] pointer;
return 0;
}
Output
Input an integer
2
Input 2 integers
456
-98
Elements entered by you are
456
-98
9. Write a C++ program to create multilevel inheritance
Source Code
#include <iostream>
#include <conio.h>
using namespace std;
class person
{
char name[100],gender[10];
int age;
public:
void getdata()
{
cout<<"Name: ";
fflush(stdin); /*clears input stream*/
gets(name);
cout<<"Age: ";
cin>>age;
cout<<"Gender: ";
cin>>gender;
}
void display()
{
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
}
};
int main()
{
programmer p;
cout<<"Enter data"<<endl;
p.getdata();
cout<<endl<<"Displaying data"<<endl;
p.display();
getch();
return 0;
}
Output
Enter data
Name: Karl Lens
Age: 31
Gender: Male
Name of Company: Dynamic Info
Salary: $21000
Number of programming language known: 4
Displaying data
Name: Karl Lens
Age: 31
Gender: Male
Name of Company: Dynamic Info
Salary: $21000
Number of programming language known: 4
10. Write a C++ program to create an array of pointers. Invoke functions using array
objects
#include <iostream>
using namespace std;
const int MAX = 3;
int main () {
int var[MAX] = {10, 100, 200};
int *ptr[MAX];
for (int i = 0; i < MAX; i++) {
ptr[i] = &var[i]; // assign the address of integer.
}
for (int i = 0; i < MAX; i++) {
cout << "Value of var[" << i << "] = ";
cout << *ptr[i] << endl;
}
return 0;
}
Output:
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
11. Write a C++ program to use pointer for both base and derived classes and call the
member function. Use virtual keyword
Source Code
class Base
{
public:
virtual void show()
{
cout << "Base class\n";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
}
int main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Late Binding Ocuurs
}
Output:
Derived class