I finally learned Classes.
Since i finished being comfortable with them today , i decided to make a little application.
Here it is.
main.cpp
Library.hCode:#include "Library.h" using namespace std; int main() { Dog dog; string enterdogname; string enterdogcolor; int enterdogweight; cout <<"Hello, What's your dog name? "<<endl; cout <<">>"; cin >> enterdogname; cout <<"\n\n"; dog.setName(enterdogname); system("cls"); cout <<"What color is your dog?"<<endl; cout <<">>"; cin >> enterdogcolor; cout <<"\n\n"; dog.setColor(enterdogcolor); system("cls"); cout <<"What's is the weight of your dog?"<<endl; cout <<">>"; cin >> enterdogweight; cout <<"\n\n"; dog.setWeight(enterdogweight); system("cls"); cout <<"Dog name: "<<dog.getName()<<endl; cout <<"Dog color: "<<dog.getColor()<<endl; cout <<"Dog weight: "<<dog.getWeight()<<endl; cout <<"Press enter to reset your dog informations"; cin.get(); cin.get(); dog.reset(); system("cls"); cout <<"Let's check if your dog informations has been reset.Press enter again."<<endl; cin.get(); cout <<"Dog name: "<<dog.getName()<<endl; cout <<"Dog color: "<<dog.getColor()<<endl; cout <<"Dog weight: "<<dog.getWeight()<<endl; cout <<"BYE"; cin.get(); return 0; }
Code://Lib #include <windows.h> #include <fstream> #include <iostream> #include <string> using namespace std; //Headers #include "Dog.h"
Dog.cpp
Code:#include "Library.h" //Dog name string Dog::getName() { return name; } void Dog::setName(string dogname) { name = dogname; } //Dog Weight int Dog::getWeight() { return weight; } void Dog::setWeight(int dogweight) { weight = dogweight; } //Dog Color string Dog::getColor() { return color; } void Dog::setColor(string dogcolor) { color = dogcolor; } //Destructor/Constructor Dog::Dog() { } Dog::~Dog() { } //reset int Dog::reset() { name = ""; color = ""; weight = NULL; return true; }
Dog.h
Code://Dog class class Dog { public: Dog(); ~Dog(); //Name string getName(); void setName(string dogname); //Weight int getWeight(); void setWeight(int dogweight); //Color string getColor(); void setColor(string dogcolor); //reset data int reset(); private: string name; int weight; string color; };
This will create the dog, and finish by destroying it. You may compile it and check it. Now, when you checked it. There's a bug huh? It doesnt destroy the weight of the dog. Now, in the destructor, i have decided to put weight = 0;. But, it will show Dog weight: 0 on the screen. I want it to just destroy it forever and don't show it. Can anyone help me. Thank you.



LinkBack URL
About LinkBacks



