Oop PR7

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Practical No.

7: Program to Implement Array of Objects

Practical Related Questions


1.State the output of the following program
#include<iostream.h>
class books
{
char title[30];
float price;
public:
void getdata ();
void putdata ();
};
void books::getdata ()
{
cout<<"Title:";
cin>>title;
cout<<"Price:";
cin>>price;
}
void books::putdata ()
{
cout<<"Title:"<<title<<"\n";
cout<<"Price:"<<price<<"\n";
const int size=3;
}
void main ()
{
books book [size];
for(int i=0;i<size;i++)
{
cout<<"Enter details of book:"<<(i+1)<<"\n";
book[i].getdata();
}
for(i=0;i<size;i++)
{cout<<"\nBook"<<(i+1)<<"\n";
book[i].putdata();
}
Ans) size is not declared
Exercise
1. Write a C++ program to define a class “Employee” having data members
emp_id, emp_name and emp_salary. Accept and display data for employees
having salary greater than 25,000/-.
Code:
#include<iostream.h>
#include<conio.h>
class Employee
{
int emp_id;
char emp_name [20];
public: float emp_salary;
void getdata()
{
cout<<"Enter employee id, name and salary:"<<endl;
cin>>emp_id>>emp_name>>emp_salary;
}
void putdata()
{
cout<<"Employee id:"<<emp_id<<endl;
cout<<"Employee name:"<<emp_name<<endl;
cout<<"Employee salary:"<<emp_salary<<endl;
}
};
void main()
{
Employee e[4];
clrscr();
cout<<"Output prepared Ashwath Bhekare (CO3IB) 22203B0015"<<endl;
for(int i=0;i<4;i++)
{
e[i].getdata();
}
for(i=0;i<4;i++)
{
if(e[i].emp_salary>25000)
{
e[i].putdata();
cout<<endl;
}
}
getch();
}
Output:

2. Write a C++ program to define a class “City” having data members name,
population. Accept and display this data for 10 cities.
Code:
#include<iostream.h>
#include<conio.h>
class City
{
int population;
char name [20];
public:
void input()
{
cout<<"Enter City's name and population: ";
cin>>name>>population;
}
void display()
{
cout<<endl<<"City's name:"<<name;
cout<<endl<<"Population of the city:"<<population;
}
};
void main()
{
City c[10];
clrscr();
cout<<"Output prepared Ashwath Bhekare (CO3IB) 22203B0015"<<endl;
for(int i=0;i<10;i++)
{
c[i].input();
}
for(i=0;i<10;i++)
{
c[i].display();
}
getch();
}
Output:
3. Complete the given table:
Program Code Write and justify Output
1. #include<iostream.h>
class Student
{
char name[20];
int marks;
public:
void getName()
{
cin>>name;
}
void getMarks()
{
cin>>marks; }
void displayInfo()
{
cout<<"Name:"<<name<<endl;
cout<<"Marks:"<<marks<<endl;
}
};
void main ()
{
Student st[5];
for(int i=0;i<5;i++)
{
cout<<"Student"<<i+1<<endl;
cout<<"Enter name"<<endl;st[i].getName
();
cout<<"Enter marks"<<endl;
st[i].getMarks();
}
for(i=0;i<5;i++)
{
cout<<"Student"<<i+1<<endl;
st[i].displayInfo();
}
}
2. #include<iostream.h>
#include<conio.h>
class Employee
{
int Id;
char Name[25];
int Age;
long Salary;
public:
void Getdata()
{
cout<<"\n\tEnter Employee Id: ";
cin>>Id;
cout<<"\n\tEnter Employee Name: ";
cin>>Name;
cout<<"\n\tEnter Employee Salary: ";
cin>>Salary;
}
void Putdata()
{
cout<<"\n"<<Id<<"\t"<<Name<<"\
t"<<Age<<"\t"<<Salary;
}
};
void main ()
{
int i;
Employee E[3];
for(i=0;i<3;i++)
{
cout<<"\nEnter details of "<<i+1<<"
Employee";
E[i].Getdata();
}
cout<<"\nDetails of Employees";
for(i=0;i<3;i++)
E[i].Putdata();
}

You might also like