Hi, need your help on my C++ programming class. Recycle code from codes below, N
ID: 3711375 • Letter: H
Question
Hi, need your help on my C++ programming class.
Recycle code from codes below, Need to rewrite the code to Polygon that will dispaly sides, color and dodecagon. Initialize all the properties of the parent class in the new constructor.
class Person
{
public:
string name, address, occupation;
int age;
char gender;
void printname();
void printaddress();
void printoccupation();
void printgender();
void printage()
{
cout << "Age: " << age;
}
};
void Person::printname(){
cout << "Name: "<< name;
}
void Person::printaddress(){
cout << "Address: "<< address;
}
void Person::printoccupation(){
cout << "Occupation: "<< occupation;
}
void Person::printgender(){
cout << "Gender: "<< gender;
}
int main() {
Person Info;
Info.name="Mary";
Info.age=21;
Info.address="California";
Info.occupation="Engineer";
Info.printname();
cout << endl;
Info.printaddress();
cout << endl;
Info.printage();
cout << endl;
Info.printoccupation();
cout << endl;
Thank you
Explanation / Answer
#include <iostream>
using namespace std;
class Dodecagon // Dodecagon is a polygon that has 12 sides
{
private:
string color;
double sides[12];
public:
Dodecagon(string color,double sides[12])// argument constructor
{
this->color = color;
for(int i = 0;i<12;i++)
this->sides[i] = sides[i];
}
void printSides();
void printColor();
};
void Dodecagon::printSides()
{
cout<<" The sides of Dodecagon are : ";
for(int i = 0;i<12;i++)
cout<<sides[i]<<endl;
}
void Dodecagon::printColor()
{
cout<<" Color = "<<color;
}
int main() {
double sides[12];
string color;
cout<<"Enter the length of 12 sides of Dodecagon : ";
for(int i = 0;i<12;i++)
cin>>sides[i];
cout<<" Enter the color of Didecagon : ";
cin>>color;
Dodecagon d(color,sides);
d.printColor();
d.printSides();
}
Output:
Color = red
The sides of Dodecagon are :
3.4
5.6
9.1
2.3
4.5
6.6
7.1
4.1
8.8
2.3
1.7
3.8
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.