question archive 1) The class will represent a Dog
Subject:Computer SciencePrice:4.86 Bought12
1) The class will represent a Dog. Consider the breed, size and is registered. Initialize all properties of the parent class in the new constructor. This time, promote the use of accessors and mutators for the new properties. Instantiate a Dog object in the main function and be able to set the values of the properties of the Dog object using the mutators. Display all the properties of the Dog object using the accessors.
2. The class will represent a Student. Consider the academic program, year in college and enrolled university. Initialize all the properties of the parent class in the new constructor. This time, promote the use of accessors and mutators for the new properties. Instantiate a Student object in the main function and be able to set the values of the properties of the Student object using the mutators. Display all the properties of the Student object using the accessors.
#include <iostream>
class Dog {
private:
std::string breed;
int size;
bool registered;
public:
Dog() {
breed = "";
size = 0;
registered = false;
}
std::string getBreed() const {
return breed;
}
void setBreed(const std::string& breedParameter) {
breed = breedParameter;
}
bool isRegistered() {
return registered;
}
void setRegister(bool registeredParameter) {
registered = registeredParameter;
}
int getSize() {
return size;
}
void setSize(int sizeParameter) {
size = sizeParameter;
}
};
class Student {
private:
std::string academicProgram;
int yearInCollege;
std::string enrolledUniversity;
public:
Student() {
academicProgram = "";
yearInCollege = 0;
enrolledUniversity = "";
}
const std::string& getAcademicProgram() const {
return academicProgram;
}
void setAcademicProgram(const std::string& academicProgramParameter) {
academicProgram = academicProgramParameter;
}
const std::string& getEnrolledUniversity() const {
return enrolledUniversity;
}
void setEnrolledUniversity(const std::string& enrolledUniversityParameter) {
enrolledUniversity = enrolledUniversityParameter;
}
int getYearInCollege() const {
return yearInCollege;
}
void setYearInCollege(int yearInCollegeParameter) {
yearInCollege = yearInCollegeParameter;
}
};
int main() {
Dog* dog = new Dog();
dog->setBreed("Poodle");
dog->setRegister(true);
dog->setSize(10);
std::cout << "Dog Breed: " << dog->getBreed()
<< ", Is Registered: " << dog->isRegistered()
<< ", Size: " << dog->getSize() << std::endl;
Student* student = new Student();
student->setAcademicProgram("Y Academic Program");
student->setYearInCollege(2);
student->setEnrolledUniversity("Z University");
std::cout << "Student Academic Program: " << student->getAcademicProgram()
<< ", Year in College: " << student->getYearInCollege()
<< ", Size: " << student->getEnrolledUniversity() << std::endl;
}
Step-by-step explanation
In C++, Class is created using the keyword "class". I'll use the Dog as an example for the explanation (this is also applicable for the Student Class).
// start of Dog Class
class Dog {
// private access modifier, only visible in this class
private:
// three properties of the dog which are breed, size and is registered (if the start of variable is "is" verb, usually it is a boolean type (true or false value)
std::string breed;
int size; // assuming inches
bool registered;
// public access modifier. can be accessed outside this class - in this case, functions are called in main function
public:
// constructor - same name of the class; no return type in the declaration
Dog() {
// initialization of the properties
breed = "";
size = 0;
registered = false;
}
// mutators - all with "get" in the start of function name; can modify or change the property value
// accessors - all with "set" or "is" (for boolean) at the start of function name; for retrieving the value of a property since a property is normally private and can't be accessed directly outside its class.
// you can change or rename the functions - I just provided a good naming convention for these functions.
std::string getBreed() const {
return breed;
}
// In the parameter, const means that we can't change the parameter value and "&" means that this is pass by reference (actual variable from main is passed. it points to the same variable instance in main and it is not a copy). "&" is for efficiency purpose as well. for primitive data types, it's normally pass by value (no "&").
void setBreed(const std::string& breedParameter) {
breed = breedParameter;
}
bool isRegistered() {
return registered;
}
void setRegister(bool registeredParameter) {
registered = registeredParameter;
}
int getSize() {
return size;
}
void setSize(int sizeParameter) {
size = sizeParameter;
}
}; // end of Dog Class
int main() {
// to use the Dog class, create an instance using new.
// "*" means pointer - it holds the created dog instance
Dog* dog = new Dog();
// to access the public mutator or accessor, use "->" operator
dog->setBreed("Poodle");
dog->setRegister(true);
dog->setSize(10);
// printing of Dog properties using accessors
std::cout << "Dog Breed: " << dog->getBreed()
<< ", Is Registered: " << dog->isRegistered()
<< ", Size: " << dog->getSize() << std::endl;
...