Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Header file
- #ifndef _CPERSON_H
- #define _CPERSON_H
- class CPerson {
- private:
- char *name;
- double height;
- double weight;
- public:
- CPerson();
- CPerson(char *, double, double);
- //setter
- int setName(char*);
- int setHeight(double);
- int setWeight(double);
- //getter
- char * getName() const;
- double getHeight() const;
- double getWeight() const;
- int print() const;
- int read();
- double bmi() const;
- int result() const;
- };
- #endif
- CPerson cpp
- #include "CPerson.h"
- #include <iostream>
- //default
- CPerson::CPerson() {
- name = new char[2];
- name = " ";
- weight= 0.;
- height = 0.;
- }
- //consturctor with parms
- CPerson::CPerson(char * name, double weight, double height) :
- name(new char[strlen(name) + 1]),
- weight(weight), height(height)
- {
- //where, how many, from where
- strcpy_s(this->name, strlen(name), name);
- }
- //setter
- int CPerson::setName(char*name){
- if (this -> name!=NULL)
- {
- delete[] this -> name;
- }
- this->name = new char[strlen(name) + 1];
- strcpy_s(this->name, strlen(name), name);
- return 0;
- }
- int CPerson::setHeight(double height){
- this->height = height;
- return 0;
- }
- int CPerson::setWeight(double weight){
- this->weight = weight;
- return 0;
- }
- //getter
- char * CPerson::getName() const{
- return name;
- }
- double CPerson::getHeight() const{
- return height;
- }
- double CPerson::getWeight() const{
- return weight;
- }
- int CPerson::print() const{
- std::cout << "Name: " << name << std::endl;
- std::cout << "Weight: " << weight << std::endl;
- std::cout << "Height: " << height << std::endl;
- return 0;
- }
- int CPerson::read(){
- std::cout << "Please enter name: ";
- name = new char[100];
- std::cin.getline(name, 99);
- std::cout << "Please enter weight in kg: ";
- std::cin >> weight;
- std::cout << "Please enter height in cm: ";
- std::cin >> height;
- //cin and getline
- std::cin.ignore();
- return 0;
- }
- double CPerson::bmi() const{
- return weight / ((0.01*height)*(0.01*height));
- }
- int CPerson::result() const{
- double temp = bmi();
- if (temp < 16) {
- std::cout << "Severely underweight eat smth!\n";
- return 0;
- }
- else if (temp < 18.5) {
- std::cout << "Underweight! \n";
- return 0;
- }
- else if (temp < 25) {
- std::cout << "Normal! \n";
- return 0;
- }
- else {
- std::cout << "Overweight! \n";
- }
- return 0;
- }
- Main cpp
- #include "CPerson.h"
- #include <iostream>
- using namespace std;
- int main() {
- CPerson * Pesho= new CPerson;
- Pesho->read();
- cout << endl;
- Pesho->print();
- cout << "BMI of " << Pesho->getName() <<" is "<<Pesho->bmi()<<"."<< endl;
- cout << endl;
- CPerson Mariika;
- Mariika.read();
- cout << endl;
- Mariika.print();
- cout << "BMI of " << Mariika.getName() << " is " << Mariika.bmi() << "." << endl;
- CPerson ** PeopleDArray = new CPerson*[5];
- CPerson PeopleArray[5];
- for (size_t i = 0; i < 5; i++)
- {
- PeopleArray[i].print();
- }
- delete Pesho;
- delete[]PeopleDArray;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement