Class and Objects based Problems Question 1 Circle radius do
Class and Objects based Problems:
Question 1: Circle -radius: double +Circle() +Circle(r: double) +setRadius(radius: double): void +getRadius():double +getArea():double +getPerimeter():double
Design a class named Circle with the features shown in the UML Diagram. Write a test class and create two objects of circle class and ask the user to enter the radius for each object and give the entered radius as input to circle class to find the area and perimeter. The default value is 1 for radius. Display the radius, area, and perimeter of circle in this order. Question
2. Design a class named Person, Student and Employee. a. A Person has a Name, Address and phone Number. b. A Student has an id and major. c. An Employee has a Salary and Employee Number. Write a constructor, setter and getter for each class. Write display() method in each class to display all the fields. Write a test program that creates a Person, Student, Employee, and invokes their display() methods.
Solution
1
#include <iostream>
using namespace std;
class Circle
{
private:
double radius;
public:
Circle() //default constructor with default radius =1
{
radius = 1;
}
Circle(double r) //parameterized constructor
{
radius = r;
}
void setRadius(double r) //set and get methods
{
radius = r;
}
double getRadius()
{
return radius;
}
double getArea() //calculate area
{
return (3.1412*radius*radius);
}
double getPerimeter() //calculate perimeter
{
return (2*3.1412*radius);
}
};
int main()
{
Circle c1 ; //object 1
double radius;
cout<<\"\ Enter the radius of the circle \";
cin>>radius;
c1.setRadius(radius);
cout<<\"\ Radius= \"<<c1.getRadius();
cout<<\"\ Area of the Circle = \"<<c1.getArea();
cout<<\"\ Perimeter of the circle = \"<<c1.getPerimeter();
Circle c2(4.5); //object 2
cout<<\"\ Radius= \"<<c2.getRadius();
cout<<\"\ Area of the Circle = \"<<c2.getArea();
cout<<\"\ Perimeter of the circle = \"<<c2.getPerimeter();
return 0;
}
output:
Success time: 0 memory: 3472 signal:0
2.
#include <iostream>
using namespace std;
class Person
{
private:
string name;
string address;
string phone;
public:
Person(string n,string a,string p) //default constructor
{
name = n;
address =a;
phone =p;
}
void setName(string n) //set and get methods
{
name = n;
}
string getName()
{
return name;
}
void setAddress(string a) //set and get methods
{
address=a;
}
string getAddress()
{
return address;
}
void setPhone(string p) //set and get methods
{
phone = p;
}
string getPhone()
{
return phone;
}
void display()
{
cout<<\"\ Person Name :\"<<name;
cout<<\"\ Address :\"<<address;
cout<<\"\ Phone Number :\"<<phone;
}
};
class Student
{
private:
int id;
string major;
public:
Student(int idNo,string m) //default constructor
{
id = idNo;
major = m;
}
void setID(int idNo) //set and get methods
{
id=idNo;
}
double getID()
{
return id;
}
void setMajor(string m) //set and get methods
{
major=m;
}
string getMajor()
{
return major;
}
void display()
{
cout<<\"\ Student ID Number :\"<<id;
cout<<\"\ Major :\"<<major;
}
};
class Employee
{
private:
double salary;
int empno;
public:
Employee(double s,int eno) //default constructor
{
salary=s;
empno= eno;
}
void setSalary(double s) //set and get methods
{
salary=s;
}
double getSalary()
{
return salary;
}
void setEmpno(int eno) //set and get methods
{
empno = eno;
}
double getEmpno()
{
return empno;
}
void display()
{
cout<<\"\ Employee Number :\"<<empno;
cout<<\"\ Employee Salary :\"<<salary;
}
};
int main()
{
Person personObj(\"Smith Williams\",\"456,Lane 6,NJ\",\"768699696\");
Student studentObj(76768,\"Computer Science\");
Employee employeeObj(4500.00,56575);
personObj.display();
studentObj.display();
employeeObj.display();
return 0;
}
Success time: 0 memory: 3472 signal:0



