In: Computer Science
// TODO 1
class Cat(private val name: String) {
var sleep: Boolean = false
fun toSleep() {
println()
}
}
fun main() {
// TODO 2
val gippy = Cat("")
gippy.toSleep()
gippy.sleep = true
gippy.toSleep()
}
TODO 1:
Complete the code in the Cat class with the following conditions:
Create a getter setter function for the sleep property in which there is a function to print text:
The getter / setter function is called
Add code to the toSleep () function to print text:
[name], sleep!
if sleep is true and text:
name, let's play!
if sleep is false.
TODO 2:
Complete initialization with Cat class.
If run the console will display text like the following:
The getter function is called
Gippy, let's play!
The setter function is called
The getter function i
#include <iostream>
using namespace std;
// TODO 1
class Cat
{
private:
string name;//private variable name
public:
//constructor called when object created and set name
Cat(string name){
this->name = name;
}
//boolean sleep
bool sleep = false;
//toSleep function to print the status of sleep
void toSleep()
{
if(getSleep()){ //when sleep is true
cout<<this->name<<",sleep!"<<endl;
}
else{
cout<<this->name<<",let's play!"<<endl;
}
}
//set sleep
void setSleep(bool sleep){
this->sleep = sleep;
}
//get sleep
bool getSleep(){
return this->sleep;
}
};
//main driver
int main()
{
Cat gippy = Cat("gippy");
gippy.setSleep(true);
gippy.toSleep();
gippy.setSleep(false);
gippy.toSleep();
return 0;
}