Create a class in C++ and name it phone. Declare data members (cost of phone and number of sim slots) inside the class.
Create three objects of the Phone class: (You are going to have three brands of phone) Assign values to the data members declared
Display cost of the three objects of phone created
Display sim slots of the three objects created.
#include <iostream>
using namespace std;
class Phone
{
private:
float cost;
int numberOfSimSlots;
public:
void setCost(float cost)
{
this->cost = cost;
}
void setNumberOfSimSlots(int numberOfSimSlots)
{
this->numberOfSimSlots = numberOfSimSlots;
}
float getCost()
{
return cost;
}
int getNumberOfSimSlots()
{
return numberOfSimSlots;
}
void display()
{
cout << "cost: " << cost << endl;
cout << "number of sim slots: " << numberOfSimSlots << endl << endl;
}
};
int main()
{
Phone siemens;
siemens.setCost(100);
siemens.setNumberOfSimSlots(1);
Phone redmi;
redmi.setCost(200);
redmi.setNumberOfSimSlots(2);
Phone iphone;
iphone.setCost(300);
iphone.setNumberOfSimSlots(2);
cout << "Phone 1 info" << endl;
siemens.display();
cout << "Phone 2 info" << endl;
redmi.display();
cout << "Phone 3 info" << endl;
iphone.display();
return 0;
}
Comments
Leave a comment