Created
May 2, 2024 15:21
-
-
Save CodingDino/3a7965d5984a505d939bf2b4131a7ade to your computer and use it in GitHub Desktop.
Example of Getters and Setters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Dog.h" | |
#include <iostream> | |
using namespace std; | |
Dog::Dog() | |
: name("") | |
, breed("") | |
, color("") | |
, coatType("") | |
, chipNumber(0) | |
, age(0.0f) | |
{ | |
cout << "Constructor for Dog run - default constructor" << endl; | |
} | |
Dog::Dog(string newName, string newBreed, | |
string newColor, string newCoatType) | |
: name (newName) | |
, breed(newBreed) | |
, color(newColor) | |
, coatType(newCoatType) | |
, chipNumber(0) | |
, age(0.0f) | |
{ | |
cout << "Constructor for Dog run - parameter constructor" << endl; | |
} | |
Dog::Dog(Dog& other) | |
: name(other.name) | |
, breed(other.breed) | |
, color(other.color) | |
, coatType(other.coatType) | |
, chipNumber(0) | |
, age(0.0f) | |
{ | |
cout << "Copy constructor run" << endl; | |
} | |
Dog::~Dog() | |
{ | |
cout << "Destructor called" << endl; | |
} | |
void Dog::Print() | |
{ | |
cout << "Name: " << name << endl; | |
cout << "Breed: " << breed << endl; | |
cout << "Color: " << color << endl; | |
cout << "CoatType: " << coatType << endl; | |
cout << "Chip Number: " << chipNumber << endl; | |
cout << "Age: " << age << endl; | |
} | |
void Dog::Bark() | |
{ | |
cout << "WOOF WOOF" << endl; | |
} | |
void Dog::Shed() | |
{ | |
cout << "You are now covered in " << color << " dog hair." << endl; | |
} | |
void Dog::InstallChip(int newChip) | |
{ | |
chipNumber = newChip; | |
} | |
float Dog::Age(float amount) | |
{ | |
age = age + amount; | |
return age; | |
} | |
float Dog::GetAge() | |
{ | |
return age; | |
} | |
void Dog::SetAge(float newAge) | |
{ | |
age = newAge; | |
} | |
int Dog::GetChipNumber() | |
{ | |
return chipNumber; | |
} | |
void Dog::SetChipNumber(int newChipNumber) | |
{ | |
chipNumber = newChipNumber; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <string> | |
using namespace std; | |
class Dog | |
{ | |
private: | |
string name; | |
int chipNumber; | |
float age; | |
string breed; | |
string color; | |
string coatType; | |
public: | |
Dog(); | |
Dog(string newName, string newBreed, | |
string newColor, string newCoatType); | |
Dog(Dog& other); | |
~Dog(); | |
void Print(); | |
void Shed(); | |
void InstallChip(int newChip); | |
float Age(float amount); | |
float GetAge(); | |
void SetAge(float newAge); | |
int GetChipNumber(); | |
void SetChipNumber(int newChipNumber); | |
private: | |
void Bark(); | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Dog.h" | |
#include <iostream> | |
#include <vector> | |
int main() | |
{ | |
int myInt; | |
Dog* dogptr = nullptr; | |
dogptr = new Dog(); | |
dogptr->Print(); | |
delete dogptr; | |
Dog myDog1("Lassie", "Rough Collie", | |
"White and Brown", "Rough" ); | |
myDog1.SetAge(14.0f); | |
Dog myDog2("Toto", "Cairn Terrier", | |
"Black", "Double"); | |
myDog2.SetAge(6.0f); | |
Dog myDog3("Scoobie Doo", "Great Dane", | |
"Brown", "Short"); | |
myDog3.SetChipNumber(123456789); | |
myDog3.SetAge(7.0f); | |
Dog myDog4 = myDog3; | |
myDog1.Print(); | |
myDog2.Print(); | |
myDog3.Print(); | |
myDog4.Print(); | |
// Print the chip numbers only for these dogs: | |
cout << "Dog 1 chip number = " << myDog1.GetChipNumber() << endl; | |
cout << "Dog 2 chip number = " << myDog2.GetChipNumber() << endl; | |
cout << "Dog 3 chip number = " << myDog3.GetChipNumber() << endl; | |
cout << "Dog 4 chip number = " << myDog4.GetChipNumber() << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment