Oop PR7
Oop PR7
Oop PR7
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();
}