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.
#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;
}
Comments
Leave a comment