Oop Lab 9
Oop Lab 9
BEE-3C
Class
1. Code the example given above with mentioning some message indicating the class
construction and destruction in the constructors and destructors of each class and
check the calling of constructors and destructors.
CODE:
#include<iostream>
#include<string>
using namespace std;
class studentRecord
{
private:
string degree;
public:
studentRecord()
{
cout<<"This is student record constructor."<<endl;
}
~studentRecord()
{
cout<<"This is student record destructor."<<endl;
}
void getdata()
{
cout<<"Enter Degree: ";
cin>>degree;
}
};
class employeeRecord
{
private:
int emp_id;
double salary;
public:
employeeRecord ()
{
cout<<"This is employee record constructor."<<endl;
}
~employeeRecord ()
{
cout<<"This is employee record destructor."<<endl;
}
void getdata()
{
cout<<"Enter Employee ID: ";
cin>>emp_id;
cout<<"Enter Salary: ";
cin>>salary;
}
};
class manager
{
private:
string title;
double dues;
employeeRecord emp;
studentRecord stu;
public:
manager(){
cout<<"This is manager constructor."<<endl;
}
~manager(){
cout<<"This is manager destructor."<<endl;
}void getdata()
{emp.getdata();
cout<<"Enter Title: ";
cin>>title;
cout<<"Enter Dues: ";
cin>>dues;
stu.getdata();
}
};
int main()
{
manager m1;
cout<<"Enter data for manager 1: "<<endl;
m1.getdata();
return 0;
}
OUTPUT:
2. Create an Address class, which contains street#, house#, city and code (all of type
char*). Create another class Person that contains an address of type Address. Give
appropriate get and set functions for both classes. Test class person in main.
CODE:
#include <iostream>
#include<string>
#include<cstring>
using namespace std;
class Address {
char houseno[20];
char street[20];
char city[20];
char code[20];
public:
void getdata() {
cout << "Enter street number:" ;
cin >> street;
cout << "Enter house number:" ;
cin >> houseno;
cout << "Enter city name:" ;
cin >> city;
cout << "Enter code:" ;
cin >> code;
}
void putdata() {
cout << "street#" << street << endl;
cout << "house#" << houseno << endl;
cout << "city:" << city << endl;
cout << "code:" << code << endl;
}
};
class Person {
Address a;
public:
void get() {
a.getdata();
}
void put() {
a.putdata();
}
};
int main() {
Person p;
p.get();
p.put();
return 0;
}
OUTPUT:
3. Write the program, which has two classes one, is Date having members (day, month,
year) and the other class is called Employee. The employee has Date class as
member as each employee has Date of joining, Date of Birth etc. a. Determine if an
employee joined the organization within last five years if the current year is 2012. b.
Determine if an Employee has age less than 40 years?
CODE:
#include <iostream>
using namespace std;
class Date {
private:
int day;
int month;
int year;
public:
Date() {
day = month = year = 0;
}
void input() {
cout << "Enter day/month/year: ";
cin >> day >> month >> year;
}
void display() {
cout << day << "/" << month << "/" << year << endl;
}
int getYear() {
return year;
}
};
class Employee {
private:
Date d,d1;
public:
Employee() {
}
void input() {
cout << "Enter date of Birth" << endl;
d.input();
cout << "Enter date of Joining" << endl;
d1.input();
}
void display() {
cout << "Enter date of Birth" << endl;
d.display();
cout << "Enter date of Joining" << endl;
d1.display();
}
int getDoBYear() {
return d.getYear();
}
int getDoJYear() {
return d1.getYear();
}
};
int main() {
Employee emps[5];
int currentYear;
int n = 0, k = 0;
cout << "No of employees joined in last 5 years: " << n << endl
<< "No of employees with age less than 40: " << k << endl;
return 0;
}
OUTPUT:
➢ HOME TASKS
#include <iostream>
using namespace std;
class CharacterConverter {
public:
void convertAndDisplay() {
char c;
cout << "Enter a character: ";
cin >> c;
cout << "Converted value of " << c << " is " << int(c) <<endl;
}
};
int main() {
CharacterConverter converter;
converter.convertAndDisplay();
return 0;
}
OUTPUT:
➢ CONCLUSION