Advertisement
wintest

OOП: Person char* name

Apr 9th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. Header file
  2.  
  3. #ifndef _CPERSON_H
  4.  
  5. #define _CPERSON_H
  6.  
  7. class CPerson {
  8. private:
  9.     char *name;
  10.     double height;
  11.     double weight;
  12. public:
  13.     CPerson();
  14.     CPerson(char *, double, double);
  15.     //setter
  16.     int setName(char*);
  17.     int setHeight(double);
  18.     int setWeight(double);
  19.     //getter
  20.     char * getName() const;
  21.     double getHeight() const;
  22.     double getWeight() const;
  23.  
  24.     int print() const;
  25.     int read();
  26.  
  27.     double bmi() const;
  28.     int result() const;
  29. };
  30.  
  31. #endif
  32.  
  33.  
  34. CPerson cpp
  35. #include "CPerson.h"
  36. #include <iostream>
  37.     //default
  38. CPerson::CPerson() {
  39.     name = new char[2];
  40.     name = " ";
  41.     weight= 0.;
  42.     height = 0.;
  43.  
  44. }
  45.     //consturctor with parms
  46. CPerson::CPerson(char * name, double weight, double height) :
  47.     name(new char[strlen(name) + 1]),
  48.     weight(weight), height(height)
  49. {
  50.     //where, how many, from where
  51.     strcpy_s(this->name, strlen(name), name);
  52.    
  53.  
  54. }
  55.  
  56. //setter
  57. int CPerson::setName(char*name){
  58.     if (this -> name!=NULL)
  59.     {
  60.     delete[] this -> name;
  61.     }
  62.     this->name = new char[strlen(name) + 1];
  63.     strcpy_s(this->name, strlen(name), name);
  64.     return 0;
  65. }
  66. int CPerson::setHeight(double height){
  67.     this->height = height;
  68.  
  69.     return 0;
  70. }
  71. int CPerson::setWeight(double weight){
  72.     this->weight = weight;
  73.     return 0;
  74. }
  75.     //getter
  76. char * CPerson::getName() const{
  77.     return name;
  78. }
  79. double CPerson::getHeight() const{
  80.     return height;
  81. }
  82. double CPerson::getWeight() const{
  83.     return weight;
  84. }
  85.  
  86. int CPerson::print() const{
  87.     std::cout << "Name: " << name << std::endl;
  88.     std::cout << "Weight: " << weight << std::endl;
  89.     std::cout << "Height: " << height << std::endl;
  90.     return 0;
  91. }
  92. int CPerson::read(){
  93.     std::cout << "Please enter name: ";
  94.     name = new char[100];
  95.     std::cin.getline(name, 99);
  96.     std::cout << "Please enter weight in kg: ";
  97.     std::cin >> weight;
  98.     std::cout << "Please enter height in cm: ";
  99.     std::cin >> height;
  100.     //cin and getline
  101.     std::cin.ignore();
  102.     return 0;
  103. }
  104.  
  105. double CPerson::bmi() const{
  106.     return weight / ((0.01*height)*(0.01*height));
  107. }
  108. int CPerson::result() const{
  109.     double temp = bmi();
  110.     if (temp < 16) {
  111.         std::cout << "Severely underweight eat smth!\n";
  112.         return 0;
  113.  
  114.     }
  115.     else if (temp < 18.5) {
  116.         std::cout << "Underweight! \n";
  117.         return 0;
  118.     }
  119.     else if (temp < 25) {
  120.         std::cout << "Normal! \n";
  121.         return 0;
  122.     }
  123.     else {
  124.         std::cout << "Overweight! \n";
  125.     }
  126.     return 0;
  127. }
  128.  
  129.  
  130. Main cpp
  131. #include "CPerson.h"
  132. #include <iostream>
  133.  
  134. using namespace std;
  135.  
  136. int main() {
  137.     CPerson * Pesho= new CPerson;
  138.     Pesho->read();
  139.     cout << endl;
  140.     Pesho->print();
  141.     cout << "BMI of " << Pesho->getName() <<" is "<<Pesho->bmi()<<"."<< endl;
  142.     cout << endl;
  143.    
  144.     CPerson Mariika;
  145.     Mariika.read();
  146.     cout << endl;
  147.     Mariika.print();
  148.     cout << "BMI of " << Mariika.getName() << " is " << Mariika.bmi() << "." << endl;
  149.     CPerson ** PeopleDArray = new CPerson*[5];
  150.     CPerson PeopleArray[5];
  151.  
  152.     for (size_t i = 0; i < 5; i++)
  153.     {
  154.         PeopleArray[i].print();
  155.  
  156.     }
  157.    
  158.     delete Pesho;
  159.     delete[]PeopleDArray;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement