Lab Report 3

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

Task No.

5 :-
Write a C++ program to enter the data of an employee and
display the data of an employee using class and object.
Code:-
#include <iostream>
using namespace std;

class Employee {
private:
string name, n;
int employeeId, id;
string department, dept;

public:

void setData() {
cout << "Enter name:";
cin >> name;
cout << "Enter Employ id:";
cin >> id;
cout << "Enter department:";
cin >> dept;

void displayData() {
cout << "Employee Name: " << name << endl;
cout << "Employee ID: " << id << endl;
cout << "Department: " << dept<< endl;
}
};

int main()
{
Employee emp;
emp.setData();
emp.displayData();

return 0;
}
Output:-

Task No.6 :-
Write a C++ program to enter number and cost of a class
item using get data () and using put data () to display data.
Write get data () outside the class item.
Code:-
#include <iostream>
using namespace std;

class Item {
private:

int number;
float cost;
public:
void getData();
void putData() {
cout << "Number: " << number << endl;
cout << "Cost:" << cost << endl;
}
};

void Item::getData() {
cout << "Enter number: ";
cin >> number;
cout << "Enter cost:";
cin >> cost;
}

int main() {
Item item;
item.getData();
item.putData();

return 0;
}

Output:-

Task No.7 :-
Create a 'DISTANCE' class with : - feet and inches as data members -
member function to input distance
- member function to output distance
- member function to add two distance objects
Write a main function to create objects of DISTANCE class. Input
two distances and output the sum.
Code:-
#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inches;
public:
void putdistance()
{
cout << "Enter feet: "<<endl;
cin >> feet;
cout << "Enter inches: "<<endl;
cin >> inches;
}
void getdistance()
{
cout << "Distance is: feet= " << feet << ", inches= " << inches << endl;
}
void add(Distance d1, Distance d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches + d2.inches;
feet = feet + (inches / 12);
inches = inches % 12;
}
};
int main()
{
Distance d1, d2, d3;
d1.putdistance();
d2.putdistance();
d3.add(d1, d2);
d3.getdistance();
return 0;
}

Output:-
Task No.8:-
Create a class 'COMPLEX' to hold a complex number. Write a friend
function to add two complex numbers. Write a main function to add two
COMPLEX objects.

Code:-
#include <iostream>
using namespace std;

class COMPLEX {
private:
float real;
float imag;

public:

friend COMPLEX addComplex(COMPLEX c1, COMPLEX c2);

void setValues(float r, float i) {


real = r;
imag = i;
}

void display() {
std::cout << "Sum: " << real << " + " << imag << "i" << std::endl;
}
};
COMPLEX addComplex(COMPLEX c1, COMPLEX c2) {
float realSum = c1.real + c2.real;
float imagSum = c1.imag + c2.imag;
COMPLEX result;
result.setValues(realSum, imagSum);
return result;
}

int main()
{
COMPLEX num1, num2;
num1.setValues(3.5, 2.5);
num2.setValues(1.2, 4.8);

COMPLEX sum = addComplex(num1, num2);


sum.display();

return 0;
}

Output:-

Task No.9:-
Create a class called 'EMPLOYEE' that has - EMPCODE and
EMPNAME as data members - member function getdata( ) to input data
- member function display( ) to output data
Write a main function to create EMP, an array of EMPLOYEE objects.
Accept and display the details of at least 6 employees.
Code:-
#include <iostream>
using namespace std;

class EMPLOYEE {
private:
int EMPCODE;
string EMPNAME;

public:
void getdata() {
cout << "Enter Employee Code: ";
cin >> EMPCODE;
cout << "Enter Employee Name: ";
cin >> EMPNAME;
}

void display() {
cout << "Employee Code: " << EMPCODE << endl;
cout << "Employee Name: " << EMPNAME << endl;
}
};

int main()
{
const int numEmployees = 6;
EMPLOYEE EMP[numEmployees];

for (int i = 0; i < numEmployees; ++i) {


cout << "Enter details for Employee " << i + 1 << endl;
EMP[i].getdata();
}

cout << "\nEmployee Details:\n";


for (int i = 0; i < numEmployees; ++i) {
cout << "Employee " << i + 1 << ":\n";
EMP[i].display();
cout << endl;
}

return 0;
}
Output:-

Task No.10:-
Write a C++ program to show the use of static data member and static
member function.

Code:-
#include <iostream>
using namespace std;

class MyClass {
public:
static int staticData;
static void staticFunction() {
cout << "Static Function is called." << endl;
}
};

int MyClass::staticData = 5;

int main() {
cout << "Initial value of staticData: " << MyClass::staticData << endl;

MyClass::staticFunction();

MyClass::staticData = 10;
cout << "Updated value of staticData: " << MyClass::staticData << endl;
return 0;
}

Output:-

---------------------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy