#include<iostream>
#include<string>
using namespace std;
//-------------------Student
Record----------------
--------------------------- //
class studentRecord
{
private:
string degree;
public:
studentRecord()
{}
void getdata()
{
cout<<"Enter Degree: "; //Student Degree
cin>>degree;
}
};
//----------------------Employee
Record------------
---------------------------- //
class employeeRecord
{
private:
int emp_id;
double salary;
public:
employeeRecord ()
{}
void getdata() //calling from class manager
{
cout<<"Enter Employee ID: "<<endl; //Emp Id
cin>>emp_id;
cout<<"Enter Salary: "<<endl; //Emp salary
cin>>salary;
}
};
//----------------------- Manager
----------------- ---------------------- //
class manager
{
private:
string title;
double dues;
employeeRecord emp;
studentRecord stu;
public:
void getdata()
{
emp.getdata();
//first we need to add employee and calling
employeeRecord class
cout<<"Enter Title: "; //After employ record,
cin>>title; //then we add title cout<<"Enter
Dues: ";
cin>>dues; //add dues
//calling class studentRecord
stu.getdata();
}
};
//----------------------- Main
Function------------
--------------------------- //
int main()
{
manager m1;
cout<<"Enter data for manager 1: "<<endl;
m1.getdata();
}
// C++ program to demonstrate
// prefix increment operator overloading
#include <iostream>
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the prefix operator
Integer operator++()
{
Integer temp;
temp.i = ++i;
return temp;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before increment: ";
i1.display();
// Using the pre-increment operator
Integer i2 = ++i1;
cout << "After pre increment: ";
i2.display();
}
Example 2.2
// C++ program to demonstrate
// Overloading the “==” Operator in C++
#include<iostream>
using namespace std;
class ProductPrice
{
private:
int price;
public:
void userInput()
{
cin>>price;
}
bool operator == (const ProductPrice &p)
{
if (price==p.price)
{
return 1;
}
else
return 0;
}
};
int main()
{
ProductPrice P1, P2;
cout<<"Please enter the price of first
product"<<endl;
P1.userInput();
cout<<"Please enter the price of Second
product"<<endl;
P2.userInput();
if(P1==P2)
{
cout<<"The price of both products is
euqal"<<endl;
}
else
cout<<"The price of both products is not
euqal"<<endl;
}
Examine above all three example code and implement it.
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.
3. Create a class, overload --operator for pre decrement, use the object of class as a loop counter
for printing a descending order number in main function.
#include <iostream>
#include <cstring>
class Address {
private:
char* street;
char* house;
char* city;
char* code;
public:
Address(const char* s, const char* h, const char* c, const char* cd) {
street = strdup(s);
house = strdup(h);
city = strdup(c);
code = strdup(cd);
}
~Address() {
delete[] street;
delete[] house;
delete[] city;
delete[] code;
}
// Get functions
const char* getStreet() const { return street; }
const char* getHouse() const { return house; }
const char* getCity() const { return city; }
const char* getCode() const { return code; }
// Set functions
void setStreet(const char* s) {
delete[] street;
street = strdup(s);
}
void setHouse(const char* h) {
delete[] house;
house = strdup(h);
}
void setCity(const char* c) {
delete[] city;
city = strdup(c);
}
void setCode(const char* cd) {
delete[] code;
code = strdup(cd);
}
};
class Person {
private:
Address address;
public:
Person(const Address& a) : address(a) {}
// Get address function
const Address& getAddress() const {
return address;
}
// Set address function
void setAddress(const Address& a) {
address = a;
}
};
int main() {
// Create an Address object
Address myAddress("123 Main St", "Apt 4", "Cityville", "12345");
// Create a Person object with the Address
Person person(myAddress);
// Display the initial address
std::cout << "Initial Address:" << std::endl;
std::cout << "Street: " << person.getAddress().getStreet() << std::endl;
std::cout << "House: " << person.getAddress().getHouse() << std::endl;
std::cout << "City: " << person.getAddress().getCity() << std::endl;
std::cout << "Code: " << person.getAddress().getCode() << std::endl;
// Modify the address
Address newAddress("456 Oak St", "Suite 10", "Townsville", "54321");
person.setAddress(newAddress);
// Display the updated address
std::cout << "\nUpdated Address:" << std::endl;
std::cout << "Street: " << person.getAddress().getStreet() << std::endl;
std::cout << "House: " << person.getAddress().getHouse() << std::endl;
std::cout << "City: " << person.getAddress().getCity() << std::endl;
std::cout << "Code: " << person.getAddress().getCode() << std::endl;
return 0;
}
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.
1. Determine if an employee joined the organization within last five years if the current
year is 2012.
2. Determine if an Employee has age less than 40 years?
#include <iostream>
class Date {
public:
int day, month, year;
Date(int d, int m, int y) : day(d), month(m), year(y) {}
};
class Employee {
private:
Date dateOfBirth;
Date dateOfJoining;
public:
Employee(const Date& dob, const Date& doj) : dateOfBirth(dob),
dateOfJoining(doj) {}
// Function to determine if an employee joined the organization within the last
five years
bool joinedWithinLastFiveYears(int currentYear) const {
return (currentYear - dateOfJoining.year) <= 5;
}
// Function to determine if an Employee has age less than 40 years
bool ageLessThan40(int currentYear) const {
return (currentYear - dateOfBirth.year) < 40;
}
};
int main() {
// Assume the current year is 2012
int currentYear = 2012;
// Create an Employee object
Employee employee(Date(1990, 5, 15), Date(2008, 9, 1));
// Test the functions
if (employee.joinedWithinLastFiveYears(currentYear)) {
std::cout << "Employee joined within the last five years." << std::endl;
} else {
std::cout << "Employee did not join within the last five years." << std::endl;
}
if (employee.ageLessThan40(currentYear)) {
std::cout << "Employee has age less than 40 years." << std::endl;
} else {
std::cout << "Employee does not have age less than 40 years." << std::endl;
}
return 0;
}