[go: up one dir, main page]

0% found this document useful (0 votes)
129 views17 pages

Write C++ Programs For The Following Questions Given. Your Answer Should Also Include Output of Programs

This document provides instructions for 4 programming assignments involving classes. It includes the details of 4 classes to create: Employee, Car, PersonalInfo, and RetailItem. For each class, it describes the required member variables and functions. It also provides sample code to demonstrate how to create objects from each class and call their functions. The goal is to write the class definitions and a program for each class assignment that tests its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views17 pages

Write C++ Programs For The Following Questions Given. Your Answer Should Also Include Output of Programs

This document provides instructions for 4 programming assignments involving classes. It includes the details of 4 classes to create: Employee, Car, PersonalInfo, and RetailItem. For each class, it describes the required member variables and functions. It also provides sample code to demonstrate how to create objects from each class and call their functions. The goal is to write the class definitions and a program for each class assignment that tests its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Week-3: Assignment-3

Due date: 28th June 2020 – [11:55PM] Total marks : 100

Roll no 6331
Name Jesis Maharjan

Write C++ programs for the following questions given. Your answer should also include
output of programs.

1. Employee Class
Write a class named Employee that has the following member variables:
• name. A string that holds the employee’s name.
• idNumber. An int variable that holds the employee’s ID number.
• department. A string that holds the name of the department where the employee works.
• position. A string that holds the employee’s job title.
The class should have the following constructors:
• A constructor that accepts the following values as arguments and assigns them to the
appropriate member variables: employee’s name, employee’s ID number, department, and
position.
• A constructor that accepts the following values as arguments and assigns them to the
appropriate member variables: employee’s name and ID number. The department and position
fields should be assigned an empty string ("").
• A default constructor that assigns empty strings ("") to the name, department, and position
member variables, and 0 to the idNumber member variable.
Write appropriate mutator functions that store values in these member variables and accessor
functions that return the values in these member variables. Once you have written the class, write
a separate program that creates three Employee objects to hold the following data.

The program should store this data in the three objects and then display the data for each
employee on the screen.

#include <iostream>
#include<iomanip>
using namespace std;
class Employee
{
private:
string name;
int idn;
string dep;
string pos;
public:
Employee(string,int,string,string);
Employee(string,int);
Employee()
{
name = "";
idn = 0;
dep = "";
pos = "";
}
void display()
{

cout <<setw(20)<<left<<
name<<setw(15)<<left<<idn<<setw(15)<<left<<dep<<setw(15)<<left<<pos<<endl;

}
void setName(string n)
{
name = n;
}
string getName()
{
return name;
}
void setId(int id)
{
idn = id;
}
int getId()
{
return idn;
}
void setDepartment(string de)
{
dep = de;
}
string getDepartment()
{
return dep;
}
void setPosition(string po)
{
pos = po;
}
string getPosition()
{
return pos;
}
};
Employee::Employee(string ename,int eid,string edep,string epos)
{
name = ename;
idn = eid;
dep = edep;
pos = epos;
}
Employee::Employee(string emname , int eidn)
{
name = emname;
idn = eidn;
}
int main()
{
Employee e[3];
string fn,ln,n,d,p;
int id,i;
for (i=0;i<3;i++)
{
cout <<"First Name: ";
cin >> fn;
cout <<"Last Name: ";
cin >> ln;
cout <<"Id: ";
cin >> id;
cout <<"Department: ";
cin >> d;
cout <<"Position: ";
cin >> p;
n = fn + " " + ln;
e[i].setName(n);
e[i].setId(id);
e[i].setDepartment(d);
e[i].setPosition(p);
cout <<
"-------------------------------------------------------------------------------"<<endl;
}
cout << "-------------------------------------------------------------------------------"<<endl;
cout <<setw(20)<<left<<"Name"<<setw(15)<<left<<"ID
Number"<<setw(15)<<left<<"Department"<<setw(15)<<left<<"Position"<<endl;
cout << "-------------------------------------------------------------------------------"<<endl;
for (i=0;i<3;i++)
{
e[i].display();
}
cout << "-------------------------------------------------------------------------------";
}

2. Car Class
Write a class named Car that has the following member variables:
• yearModel. An int that holds the car’s year model.
• make. A string that holds the make of the car.
• speed. An int that holds the car’s current speed.
In addition, the class should have the following constructor and other member functions.
• Constructor. The constructor should accept the car’s year model and make as arguments.
These values should be assigned to the object’s yearModel and make member variables. The
constructor should also assign 0 to the speed member variables.
• Accessor. Appropriate accessor functions to get the values stored in an object’s yearModel,
make, and speed member variables.
• accelerate. The accelerate function should add 5 to the speed member variable each time it is
called.
• brake. The brake function should subtract 5 from the speed member variable each time it is
called.
Demonstrate the class in a program that creates a Car object, and then calls the accelerate
function five times. After each call to the accelerate function, get the current speed of the car and
display it. Then, call the brake function five times. After each call to the brake function, get the
current speed of the car and display it.

In car.h
#ifndef CAR_H
#define CAR_H
#include <iostream>
#include <string>

using namespace std;

class Car{
private:
int yearModel;
string make;
int speed;

public:

Car(int y, string m){


yearModel = y;
make = m;
speed = 0;
}

int getYear() const{


return yearModel;
}

string getMake() const{


return make;
}

int getSpeed() const{


return speed;
}

void accelerate(){
speed += 5;
}

void brake(){
speed -= 5;
}
};

#endif

In Carmain.cpp

#include <iostream>
#include <string>
#include "Car.h"

using namespace std;


int main()
{

Car car1(2020, "Ford");

cout << "Car's Current speed: ";


cout << car1.getSpeed() << endl;

cout << "\nCalling function accelerate repeatedly...\n";


for(int i = 0; i < 5; i++){
cout << "Accelerating. ";
car1.accelerate();
cout << "Current speed: ";
cout << car1.getSpeed();
cout << endl;
}

cout << "\nCalling function brake repeatedly...\n";


for(int i = 0; i < 5; i++){
cout << "Braking. ";
car1.brake();
cout << "Current speed: ";
cout << car1.getSpeed();
cout << endl;
}
return 0;
}
3. Personal Information Class
Design a class that holds the following personal data: name, address, age, and phone number.
Write appropriate accessor and mutator functions. Demonstrate the class by writing a program
that creates three instances of it. One instance should hold your information, and the other two
should hold your friends’ or family members’ information.

#include <iostream>
#include <cstring>
using namespace std;
const int SIZE = 60;
class PersonalInfo
{
private:
char name[SIZE];
char address[SIZE];
int age;
char phone[SIZE];

public:
PersonalInfo()
{ name[0] = '\0';
address[0] = '\0';
age = 0;
phone[0] = '\0';
}

PersonalInfo(char n[], char addr[], int a, char p[])


{
setName(n);
setAddress(addr);
setAge(a);
setPhone(p);
}

void setName(char n[])


{ strncpy(name, n, SIZE);
name[SIZE-1] = '\0';
}

void setAddress(char a[])


{ strncpy(address, a, SIZE);
address[SIZE-1] = '\0';
}

void setAge(int a)
{ age = a; }

void setPhone(char p[])


{ strncpy(phone, p, SIZE);
phone[SIZE-1] = '\0';
}

const char *getName() const


{ return name; }

const char *getAddress() const


{ return address; }

int getAge() const


{ return age; }

const char *getPhone() const


{ return phone; }
};

void displayPersonalInfo(PersonalInfo);

int main()
{

PersonalInfo me("Jesis Maharjan",


"Kathmandu",
23, "9803999850");

PersonalInfo Brother("Sajil Maharjan",


"Naikap,Kathmandu",
19, "9813601862");

PersonalInfo Friend("Shreeya Shahi",


"Lake Charles, Louisiana",
22, "3372493516");

displayPersonalInfo(me);
cout<< "-------------------------------------------------------"<<endl;
displayPersonalInfo(Brother);
cout<< "-------------------------------------------------------"<<endl;
displayPersonalInfo(Friend);
cout<< "-------------------------------------------------------"<<endl;

return 0;
}

void displayPersonalInfo(PersonalInfo obj)


{
cout << "Name: " << obj.getName()
<< endl;
cout << "Address: " << obj.getAddress()
<< endl;
cout << "Age: " << obj.getAge()
<< endl;
cout << "Phone: " << obj.getPhone()
<< endl << endl;
}

4. RetailItem Class
Write a class named RetailItem that holds data about an item in a retail store. The class should
have the following member variables:
• description. A string that holds a brief description of the item.
• unitsOnHand. An int that holds the number of units currently in inventory.
• price. A double that holds the item’s retail price.
Write a constructor that accepts arguments for each member variable, appropriate mutator
functions that store values in these member variables, and accessor functions that return the
values in these member variables. Once you have written the class, Write a separate program that
creates three RetailItem objects and stores the following data in them.
#include<iostream>
#include<iomanip>
using namespace std;
class RetailItem
{
private:
string description;
int unitsOnHand;
float price;
public:
RetailItem()
{
description = "";
unitsOnHand = 0;
price = 0;
}
RetailItem(string,int,float);
void setDescription(string descript)
{
description = descript;
}
string getDescription()
{
return description;
}
void setUnitsOnHand(int uoh)
{
unitsOnHand = uoh;
}
int getUnitsOnHand()
{
return unitsOnHand;
}
void setPrice(float pri)
{
price = pri;
}
float getPrice()
{
return price;
}
};
RetailItem::RetailItem(string des,int unit,float pr)
{
description = des;
unitsOnHand = unit;
price = pr;
}
int main()
{
RetailItem it1("Jacket",12,59.95) , it2("Designer Jeans",40,34.95),
it3("Shirt",20,24.95);
cout<< "-------------------------------------------------------"<<endl;
cout << setw(11)<<"
"<<setw(20)<<left<<"Description"<<setw(20)<<left<<"Units On
Hand"<<"Price"<<endl;
cout<< "-------------------------------------------------------"<<endl;
cout<<setw(11)<<left<<"Item
#1"<<setw(20)<<left<<it1.getDescription()<<setw(20)<<left<<it1.getUnitsOnHan
d()<<it1.getPrice()<<endl;
cout<<setw(11)<<left<<"Item
#2"<<setw(20)<<left<<it2.getDescription()<<setw(20)<<left<<it2.getUnitsOnHan
d()<<it2.getPrice()<<endl;
cout<<setw(11)<<left<<"Item
#3"<<setw(20)<<left<<it3.getDescription()<<setw(20)<<left<<it3.getUnitsOnHan
d()<<it3.getPrice()<<endl;
cout<< "-------------------------------------------------------"<<endl;
}

5. Circle Class
Write a Circle class that has the following member variables:
radius: a double pi: a double initialized with the value 3.14159
The class should have the following member functions:
• Default Constructor. A default constructor that sets radius to 0.0.
• Constructor. Accepts the radius of the circle as an argument.
• setRadius. A mutator function for the radius variable.
• getRadius. An accessor function for the radius variable.
• getArea. Returns the area of the circle, which is calculated as area = pi * radius * radius
• getDiameter. Returns the diameter of the circle, which is calculated as diameter = radius * 2
• getCircumference. Returns the circumference of the circle, which is calculated as
circumference = 2 * pi * radius
Write a program that demonstrates the Circle class by asking the user for the circle’s radius,
creating a Circle object, and then reporting the circle’s area, diameter, and circumference.

In circle.h

#ifndef Circle_h
#define Circle_h

#include <iostream>

using namespace std;

class Circle{
private:
double radius;
double pi = 3.14159;

public:
Circle(){
radius = 0.0;
}

Circle(double r){
radius = r;
}

void setRadius(double r){


radius = r;
}

double getRadius() const{


return radius;
}

double getArea() const{


return pi * radius * radius;
}

double getDiameter() const{


return 2 * radius;
}

double getCircumference() const{


return 2 * pi * radius;
}
};

#endif

In main.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "Circle.h"

using namespace std;

int main()
{
double rad;

cout << "Enter the radius of your circle: ";


cin >> rad;

cout << "Please wait while we create a circle object with radius" <<endl;
cout << " of your choice"<<endl;
Circle c1(rad);
cout << "......Thank you for waiting."<<endl;

cout << setprecision(2) << fixed;


cout << "\nThe circle area: " << c1.getArea();
cout << "\nThe circle diameter: " << c1.getDiameter();
cout << "\nThe circle circumference: " << c1.getCircumference();
return 0;
}

You might also like