Answer to Question #191897 in C++ for Ajith M

Question #191897

Create the class Person consisting of data members person_name, age, height and weight. Use constructor default construcor and parameterized constructor to assign values for data members and display() member function is used to display the information of person fitness. Finally free the resources of data objects using destructor member function.


1
Expert's answer
2021-05-11T10:00:17-0400
#include <iostream>
using namespace std;
class Person{
    char* person_name;
    int age;
    float height, weight;
    public:
        Person(){}
        Person(string s, int age, float height, float weight){
            person_name = new char[s.length()];
            for(int i = 0; i < s.length(); i++) person_name[i] = s[i];
            this->age = age;
            this->height = height;
            this->weight = weight;
        }
        void display(){
            cout<<"Name: "<<person_name<<endl;
            cout<<"Age: "<<age<<endl;
            cout<<"Height: "<<height<<endl;
            cout<<"Weigt: "<<weight<<endl;
        }
        ~Person(){
            cout<<"\n***freeing memory***\n";
            delete [] person_name;
        }
};
int main(){
    Person p("Lucious", 25, 188, 65);
    p.display();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment