Description
Design a class named Student. Data which are stored in Student class consists of age(int), first_name(string), last_name(string), student_id(string).
Constructors:
- The default constructor of the class should initialize age to 0, and initialize first_name, last_name, student_id to empty string.
- The parameterized constructor Student(int a, string s, string f, string l) should initialize Box's age, student_id, first_name, last_name to a, s, f and l.
Methods:
- void setAge(int a) – Should set student’s age to a
- void setStudentId(string s) - Should set student’s student_id to s
- void setFirstName(string f) - Should set student’s first_name to f
- void setLastName (string l) - Should set student’s last_name to l
- int getAge () - Return student’s age
- string getStudentId() - Return student’s student_id
- string getFirstName() - Return student’s first_name
- string getLastName() - Return student’s last_name
- void yearAfter() – Should add 1 to the student’s age. And add 1 to the first letter of student_id; if the first letter is ‘9’ than turn it to ‘0’.
- string toString() - Return the new string: first_name last_name, student_id, age
ref: https://www.hackerrank.com/challenges/c-tutorial-class/problem
function.cpp
#include "function.h"
//#include <sstream>
Student::Student() : age(0), student_id(""), first_name(""), last_name("") {}
/* Todo */
void Student::setAge(int _a) { age = _a; }
/* Todo */
Input
No need to handle input. (Input would be number 1~5 to represent the index of test-case)
Output
Depend on test case show in main.cpp.
Sample Input Download
Sample Output Download
Partial Judge Code
13028.cppPartial Judge Header
13028.hTags
Discuss
Description
Use main.cpp, and complete 5 classes that consists some functions:
Animal
- Run() – should print: Running!!! (type)
- Sleep() – should print: Sleeping~~~ (type)
Dog
- Run()
- Sleep()
- Woof() – should print: Woof! Woof!
- type: dog
Bird
- Run()
- Sleep()
- Fly() – should print: Flying!!! (type)
- Sing() – Should print: The (type) sings like "sound"
- Feed(string food) – Should print: You just feed the type some food, at the first line. If the food is the type’s secretfood than call LayAnEgg(); otherwise print: Nothing happened..., at the second line.
Chicken
- Run()
- Sleep()
- Fly()
- Sing()
- Feed(string food)
- type: chicken
- sound: COCK-A-DOODLE-DOO
- secretFood: uncooked rice
Duck
- Run()
- Sleep()
- Fly()
- Sing()
- Feed(string food)
- Swim() – Should print: Swimming~~~
- type: duck
- sound: GUA-GUA-GUAAA
- secretFood: bookworms
Examples:
Duck duck;
duck.Run(); // Running!!! (duck)
duck.Fly(); // Flying!!! (duck)
duck.Sing(); // The duck sings like "GUA-GUA-GUAAA"
duck.Swim(); // Swimming~~~
duck.Feed("worms"); // You just feed the duck some worms
// Nothing happened...
duck.Feed("bookworms"); // You just feed the duck some worms
// "BURP!" the duck just laid an egg
duck.Sleep(); // Sleeping~~~ (duck)
main.cpp
#include <iostream>
#include <string>
using namespace std;
/* Todo */
// 1. Should invoke Run(), Sleep()
class Animal
{
public:
Animal() : type("UNKNOWN") {}
protected:
string type;
};
/* Todo */
// 1. Implement Inheritance
// 2. Should invoke Woof()
class Dog
{
public:
Dog()
{
type = "dog";
}
};
/* Todo */
// 1. Implement Inheritance
// 2. Should invoke Fly(), Sing()
// 3. Should complete Feed(string food)
class Bird
{
public:
Bird() : sound("UNKNOWN"), secretFood("UNKNOWN")
{
type = "BIRD";
}
void Feed(string food)
{
cout << "You just feed the " << type << " some " << food << endl;
/* Todo */
cout << "Nothing happened..." << endl;
}
protected:
string sound;
string secretFood;
private:
void LayAnEgg()
{
cout << "\"BURP!\" the " << type << " just laid an egg" << endl;
}
};
/* Todo */
// 1. Implement Inheritance
// 2. Should handle parameters sound, secretfood in constructor
class Chicken
{
public:
Chicken()
{
type = "chicken";
/* Todo */
}
};
/* Todo */
// 1. Implement Inheritance
// 2. Should handle parameters sound, secretfood in constructor
// 3. Should invoke Swim()
class Duck
{
public:
Duck()
{
type = "Duck";
/* Todo */
}
};
int main()
{
int testcase = 0;
/* Todo */
// 1. Handle input
// 2. Create Dog, Chicken, Duck Object
// 3. No need to change anything in switchcase
switch (testcase)
{
case 1:
dog.Run();
dog.Woof();
dog.Sleep();
break;
case 2:
chicken.Run();
chicken.Fly();
chicken.Sing();
chicken.Feed("rice");
chicken.Sleep();
break;
case 3:
chicken.Run();
chicken.Fly();
chicken.Sing();
chicken.Feed("uncooked rice");
chicken.Sleep();
break;
case 4:
duck.Run();
duck.Fly();
duck.Sing();
duck.Swim();
duck.Feed("worms");
duck.Sleep();
break;
case 5:
duck.Run();
duck.Fly();
duck.Sing();
duck.Swim();
duck.Feed("bookworms");
duck.Sleep();
break;
default:
cout << "Remember to handle input!" << endl;
break;
}
return 0;
}
Input
No need to handle input. (Input would be number 1~5 to represent the index of test-case)
Output
Depend on test case show in main.cpp.