0% found this document useful (0 votes)
11 views25 pages

Oop Attempt Review Oop

The document details an OOP (Object-Oriented Programming) attempt review for a course on Data Structures and Algorithms at the University of Science, Ho Chi Minh City. It includes a series of programming exercises related to classes such as Point and Circle, with the student successfully completing all tasks and achieving full marks. The document outlines the implementation of various methods and operator overloads, demonstrating the student's understanding of OOP concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views25 pages

Oop Attempt Review Oop

The document details an OOP (Object-Oriented Programming) attempt review for a course on Data Structures and Algorithms at the University of Science, Ho Chi Minh City. It includes a series of programming exercises related to classes such as Point and Circle, with the student successfully completing all tasks and achieving full marks. The document outlines the implementation of various methods and operator overloads, demonstrating the student's understanding of OOP concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

lOMoARcPSD|44569428

OOP Attempt review - OOP

Data Structures and Algorithms (rường Đại học Bách khoa, Đại học Quốc gia Thành phố
Hồ Chí Minh)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Đã bắt đầu vào Thứ hai, 25 Tháng chín 2023, 7:17 AM


lúc
Tình trạng Đã hoàn thành
Hoàn thành vào Thứ sáu, 29 Tháng chín 2023, 10:48 AM
lúc
Thời gian thực 4 ngày 3 giờ
hiện
Điểm 11,00/11,00
Điểm 10,00 của 10,00 (100%)

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 1/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Câu hỏi 1
Chính xác

Điểm 1,00 của 1,00

In the coordinate plane, we have class Point to store a point with it's x-y coordinate.

Your task in this exercise is to implement functions marked with /* * STUDENT ANSWER */.

Note: For exercises in Week 1, we have #include <bits/stdc++.h> and using namespace std;

For example:

Test Result

Point A(2, 3); 2 3


cout << A.getX() << " " << A.getY();

Point A(2, 3); 5


Point B(1, 1);
cout << pow(A.distanceToPoint(B), 2);

Answer: (penalty regime: 0 %)

Reset answer

6
7 public:
8 Point() : m_x {0}, m_y {0}
9 ▼ {
10 }
11
12 Point(double x, double y) : m_x {x}, m_y {y}
13 ▼ {
14 }
15
16 void setX(double x)
17 ▼ {
18 m_x = x;
19 }
20
21 void setY(double y)
22 ▼ {
23 m_y = y;
24 }
25
26 double getX() const
27 ▼ {
28 return m_x;
29 }
30
31 double getY() const
32 ▼ {
33 return m_y;
34 }
35
36 double distanceToPoint(const Point& pointA)
37 ▼ {
38 double deltax {pointA.m_x - m_x};
39 double deltay {pointA.m_y - m_y};
40 return std:: sqrt(deltax*deltax + deltay*deltay);
41
42 ▼ /*

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 2/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Test Expected Got

 Point A(2, 3); 2 3 2 3 


cout << A.getX() << " " << A.getY();

 Point A(2, 3); 5 5 


Point B(1, 1);
cout << pow(A.distanceToPoint(B), 2);

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 3/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Câu hỏi 2
Chính xác

Điểm 1,00 của 1,00

In the coordinate plane, a circle is defined by center and radius.

Your task in this exercise is to implement functions marked with /* * STUDENT ANSWER */.

Note: you can use implemented class Point in previous question

For example:

Test Result

Circle A; Center: {0.00, 0.00} and Radius 0.00


A.printCircle();

Answer: (penalty regime: 0 %)

Reset answer
62 }
63
64 Circle(Point center, double radius) : m_center {center} ,m_radius {radius}
65 ▼ {
66 ▼ /*
67 * STUDENT ANSWER
68 */
69 }
70
71 Circle(const Circle &circle) : m_center { circle.m_center} , m_radius {circle.m_radius}
72 ▼ {
73 ▼ /*
74 * STUDENT ANSWER
75 */
76 }
77
78 void setCenter(Point point)
79 ▼ {
80 m_center = point;
81 }
82
83 void setRadius(double radius)
84 ▼ {
85 ▼ /*
86 * STUDENT ANSWER
87 */
88 m_radius = radius;
89 }
90
91 Point getCenter() const
92 ▼ {
93 return m_center;
94 }
95
96 double getRadius() const
97 ▼ {
98 return m_radius;
99 }

Test Expected Got

 Circle A; Center: {0.00, 0.00} and Radius 0.00 Center: {0.00, 0.00} and Radius 0.00 
A.printCircle();

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 4/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 5/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Câu hỏi 3
Chính xác

Điểm 1,00 của 1,00

In this exercise, you can use implemented functions in previous question (if needed) and implement these following functions.

bool containsPoint(const Point point){}

bool containsTriangle(const Point pointA, const Point pointB, const Point pointC){}

For example:

Test Result

Point pointO(0, 2); 1


Point point1(1, 2);
Circle A = Circle(pointO, 2);
cout << A.containsPoint(point1);

Point pointO(0, 0); 0


Point point1(1, 0), point2(-1, 0), point3(0, 3);
Circle A = Circle(pointO, 3);
cout << A.containsTriangle(point1, point2, point3);

Answer: (penalty regime: 0 %)

Reset answer

59 * STUDENT ANSWER
60 * TODO: set zero center's x-y and radius
61 */
62 }
63
64 Circle(Point center, double radius) : m_center {center} ,m_radius {radius}
65 ▼ {
66 ▼ /*
67 * STUDENT ANSWER
68 */
69 }
70
71 bool containsPoint(const Point point)
72 ▼ {
73 ▼ /*
74 * STUDENT ANSWER
75 * TODO: check if a given point is entirely within the circle (does not count if the point lies on th
76 If contain, return true.
77 */
78 if (m_center.distanceToPoint(point) < m_radius)
79 return true;
80 else
81 return false;
82 }
83
84 bool containsTriangle(const Point pointA, const Point pointB, const Point pointC)
85 ▼ {
86 ▼ /*
87 * STUDENT ANSWER
88 * TODO: check if a given triangle ABC (A, B, C are not on the same line) is entirely within the circ
89 If contain, return true.
90 */
91 if (containsPoint(pointA) && containsPoint(pointB) && containsPoint(pointC)) return true;
92 else return false;
93 }
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 6/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review


}
94 };
 

Test Expected Got

 Point pointO(0, 2); 1 1 


Point point1(1, 2);
Circle A = Circle(pointO, 2);
cout << A.containsPoint(point1);

 Point pointO(0, 0); 0 0 


Point point1(1, 0), point2(-1, 0), point3(0, 3);
Circle A = Circle(pointO, 3);
cout << A.containsTriangle(point1, point2, point3);

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 7/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Câu hỏi 4
Chính xác

Điểm 1,00 của 1,00

In this exercise, you can use implemented functions in previous question (if needed) and implement these following functions.

1. Overload operator =
2. Overload operator == (The two circles are equal if they have the same center and radius)
3. Overload operator >> (stdin center.x, center.y, radius in order)

For example:

Test Input Result

Point pointO(0, 0); 1


Circle A = Circle(pointO, 3);
Circle B;
B = A;
cout << (B == A);

Circle A; 2 Center: {2.00, 3.50} and Radius 2.00


cin >> A; 3.5
A.printCircle(); 2

Answer: (penalty regime: 0 %)

Reset answer
46 }
47
48
49 friend bool operator== (const Point& pointA, const Point& pointB)
50 ▼ {
51 if (pointA.m_x == pointB.m_x && pointA.m_y == pointB.m_y)
52 return true;
53 else return false;
54 }
55 };
56
57 class Circle
58 ▼ {
59 private:
60 Point m_center;
61 double m_radius;
62
63 public:
64 Circle() : m_center(), m_radius {0}
65 ▼ {
66 ▼ /*
67 * STUDENT ANSWER
68 * TODO: set zero center's x-y and radius
69 */
70 }
71
72 Circle(Point center, double radius) : m_center {center} ,m_radius {radius}
73 ▼ {
74 ▼ /*
75 * STUDENT ANSWER
76 */
77 }
78
79 void operator=(const Circle &circle)
80 ▼ {
81 // same as copy assignment
82 m_center = circle.m_center;
 
83 m radius = circle m radius;
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 8/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Test Input Expected Got

 Point pointO(0, 0); 1 1 


Circle A = Circle(pointO, 3);
Circle B;
B = A;
cout << (B == A);

 Circle A; 2 Center: {2.00, 3.50} and Radius 2.00 Center: {2.00, 3.50} and Radius 2.00 
cin >> A; 3.5
A.printCircle(); 2

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 9/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Câu hỏi 5
Chính xác

Điểm 1,00 của 1,00

In a game, we have class Character to store characters' data.


The class Character is declared as below:
class Character {
protected:
int hp;
int x;
int y;
public:
// Constructor: set the values of x and y and hp to 0
Character();

// Constructor: set the values of hp, x and y to each parameter


Character(int hp, int x, int y);

// Set and get hp


int getHp();
void setHp(int hp);

// Set and get x


int getX();
void setX(int x);

// Set and get y


int getY();
void setY(int y);

// Get Manhattan distance to other character


int getManhattanDistTo(Character* other);
};

Your task is to define the constructors and the methods of the class.

Note:
In this task, iostream library has been included, and namespace std is being used. No other libraries are allowed.

For example:

Test Result

Character ch1(100, 3, 6); 100 3 6


cout << ch1.getHp() << " " << ch1.getX() << " " << ch1.getY();

Answer: (penalty regime: 0 %)

Reset answer

1 #include <cmath>
2 ▼ Character::Character() {
3 x = 0;
4 y = 0;
5 hp = 0;
6 }
7
8 ▼ Character::Character(int hp, int x, int y) {
9 this->hp = hp;
10 this->x = x;
11 this->y = y;
12 }
13
14 ▼ int Character::getHp() {
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 10/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review


15 return hp;
16 }
17
18 ▼ void Character::setHp(int hp) {
19 this->hp = hp;
20 }
21
22 ▼ int Character::getX() {

Test Expected Got

 Character ch1(100, 3, 6); 100 3 6 100 3 6 


cout << ch1.getHp() << " " << ch1.getX() << " " << ch1.getY();

 Character ch2; 0 0 0 0 0 0 
cout << ch2.getHp() << " " << ch2.getX() << " " << ch2.getY();

 Character* ch31 = new Character(100, 1, 2); 6 6 


Character* ch32 = new Character(100, -3, 4);
cout << ch31->getManhattanDistTo(ch32);
delete ch31;
delete ch32;

 Character ch4; 4 4 
ch4.setX(4);
cout << ch4.getX();

 Character ch5; 5 5 
ch5.setY(5);
cout << ch5.getY();

 Character ch6; 6 6 
ch6.setHp(6);
cout << ch6.getHp();

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 11/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Câu hỏi 6
Chính xác

Điểm 1,00 của 1,00

In a game, we have class Character to store characters' data.


The class Character is declared as below:
class Character {
protected:
int hp;
int x;
int y;
public:
Character();
Character(int hp, int x, int y);
int getHp();
void setHp(int hp);
int getX();
void setX(int x);
int getY();
void setY(int y);
int getManhattanDistTo(Character* other);

// Operator =: copy all data from Character other


void operator=(const Character& other);

// Operator <: Character a < Character b when a's hp is less than or equal b's hp
bool operator<(const Character& other);

// Operator () with zero parameters: print data of the instance with format: hp-x-y
void operator()();
};

Your task is to overload these following operators: =, < and (). Their functions are described above.

Note:
In this task, iostream library has been included, and namespace std is being used. No other libraries are allowed.

For example:

Test Result

Character ch1(100, 3, 6); 100-3-6


ch1();

Answer: (penalty regime: 0 %)

Reset answer

1 // Copy all data from Character other


2 void Character::operator=(const Character& other)
3 ▼ {
4 x = other.x;
5 y = other.y;
6 hp = other.hp;
7 }
8
9 // Character a < Character b when a's hp is less than or equal b's hp
10 bool Character::operator<(const Character& other)
11 ▼ {
12 return (hp < other.hp || hp == other.hp);
13 }
14
15 // Print data of the instance with format: hp-x-y
16 void Character::operator()()
{
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 12/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review


17 ▼ {
18 std:: cout << hp <<'-' << x << '-' << y ;
19 }

Test Expected Got

 Character ch1(100, 3, 6); 100-3-6 100-3-6 


ch1();

 Character ch21(10, 20, 30); false false 


Character ch22(5, 5, 6);
cout << ((ch21 < ch22) ? "true" : "false");

 Character ch31; true true 


Character ch32;
cout << ((ch31 < ch32) ? "true" : "false");

 Character ch4; 0-0-0 0-0-0 


ch4(); 5-10-20 5-10-20
cout << "\n";
ch4 = Character(5, 10, 20);
ch4();

 Character(3, 4, 5)(); 3-4-5true 3-4-5true 


cout << ((Character(3, 4, 5) < Character(3, 4, 5)) ? "true" : "false");

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 13/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Câu hỏi 7
Chính xác

Điểm 1,00 của 1,00

In a game, we have class Character to store characters' data.


The class Character is declared as below:
class Character {
private:
int x;
int y;
protected:
int hp;
public:
Character();
Character(int hp, int x, int y);
int getHp();
void setHp(int hp);
int getX();
void setX(int x);
int getY();
void setY(int y);
int getManhattanDistTo(Character* other);
void operator()();
};

Your task is to define a new class Player which is a derived class of class Character. The requirements of the new class are listed below:
Methods of base class Character cannot be accessed outside Player class using Player instances
Example: Player pl; pl.setX(); will raise errors when compiled.
Player class has these methods and constructors:
Constructor Player(): acts just like Character()
Constructor Player(int hp, int x, int y): acts just like Character(hp, x, y)
Method void printPlayerData(): prints data of the instance with format: hp-x-y
Method void moveTo(int x, int y): sets the values of x, y to new values
The mentioned constructors and methods can be accessed outside Player class.

Note:
In this task, iostream library has been included, and namespace std is being used. No other libraries are allowed.

For example:

Test Result

Player pl1(100, 3, 6); 100-3-6


pl1.printPlayerData();

Answer: (penalty regime: 0 %)

Reset answer
y
11 */
12 class Player : protected Character
13 ▼ {
14 //all the private becomes inaccesible
15 //all the public and protected becomes protected
16 //include the base and also the derived itself
17 public:
18 Player() : Character()
19 {}
20
21
22 Player(int hp,int x, int y) : Character(hp,x,y)
23 {}
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 14/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review


{}
24
25
26 void printPlayerData()
27 ▼ {
28 operator()();
29 }
30
31
32 void moveTo(int x, int y)

Test Expected Got

 Player pl1(100, 3, 6); 100-3-6 100- 


pl1.printPlayerData(); 3-6

 Player pl2; 0-0-0 0-0-0 


pl2.printPlayerData();

 Player pl3(300, 1, 2); 300-3-4 300- 


pl3.moveTo(3, 4); 3-4
pl3.printPlayerData();

 Player pl4(300, 1, 2); 


const bool condition = (is_unambiguous_public_base_of<Character>(&pl4) == nullptr &&
is_base_of<Character, Player>::value == true);
assert(condition);

 Player pl5(300, 1, 2); 300-9-7 300- 


pl5.moveTo(9, 7); 9-7
pl5.printPlayerData();

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 15/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Câu hỏi 8
Chính xác

Điểm 1,00 của 1,00

Hoang is a K19 student studying at Bach Khoa University. He plans to write a book management software for the library. In the class design,
Hoang has designed the class Book as follows:

class Book
{
private:
char* title;
char* authors;
int publishingYear;
public:
// some method
}

Your task in this exercise is to implement functions marked with /* * STUDENT ANSWER */.

Note: For exercises in Week 2, we have #include <bits/stdc++.h> and using namespace std;

For example:

Test Result

Book book1("Giai tich 1","Nguyen Dinh Huy",2000); Giai tich 1


book1.printBook(); Nguyen Dinh Huy
2000

Book book1("Giai tich 1","Nguyen Dinh Huy",2000); Giai tich 1


Book book2 = book1; Nguyen Dinh Huy
book2.printBook(); 2000

Answer: (penalty regime: 0 %)

Reset answer

14 */
15 }
16
17 Book(const char* title, const char* authors, int publishingYear)
18 ▼ {
19 ▼ /*
20 * STUDENT ANSWER
21 */
22 std:: size_t lent{ std:: strlen(title) + 1 };
23 //allocating memory before using
24 m_title = new char[lent];
25 std:: strcpy(m_title,title); //copy until the \0
26
27 m_publishingYear = publishingYear;
28
29 std:: size_t lena {std:: strlen(authors) + 1 };
30 m_authors = new char[lena];
31 std:: strcpy(m_authors, authors);
32
33 }
34
35 Book(const Book &book)
36 ▼ {
37 ▼ /*
38 * STUDENT ANSWER
39 * TODO: deep copy constructor
40 */
41 //book.m_tille
42 //book.m_authors
43 m_publishingYear = book.m_publishingYear;
44 td i t l t{ td t l (b k titl ) 1 }
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 16/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review


44 std:: size_t lent{ std:: strlen(book.m_title) + 1 };
45 //allocating memory before using
46 m_title = new char[lent];
47 std:: strcpy(m_title,book.m_title); //copy until the \0
48
49 std:: size_t lena {std:: strlen(book.m_authors) + 1 };
50 m_authors = new char[lena];
51 std:: strcpy(m_authors, book.m_authors);
52 }
53
54 void setTitle(const char* title)
55 ▼ {
56 delete[] m_title;
57 std:: size_t lent{ std:: strlen(title) + 1 };
58 //allocating memory before using
59 m_title = new char[lent];
60 std:: strcpy(m_title,title); //copy until the \0
61 }
62
63 void setAuthors(const char* authors)

Test Expected Got

 Book book1("Giai tich 1","Nguyen Dinh Huy",2000); Giai tich 1 Giai tich 1 
book1.printBook(); Nguyen Dinh Huy Nguyen Dinh Huy
2000 2000

 Book book1("Giai tich 1","Nguyen Dinh Huy",2000); Giai tich 1 Giai tich 1 
Book book2 = book1; Nguyen Dinh Huy Nguyen Dinh Huy
book2.printBook(); 2000 2000

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 17/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Câu hỏi 9
Chính xác

Điểm 1,00 của 1,00

In this exercise, you can use implemented functions in previous question (if needed) and implement these following functions.
friend bool checkAuthor(Book book, char* author){}

In the authors attribute, it is possible to have more than one author writing a book together. So authors will have the following format:
“author1, author2, …, authorN”

The function returns true if the author is on the book's authors list, otherwise it returns false
Note: Both first and last name must match. If only a partial match, the function still returns false

For example:

Test Result

Book book1("Giai tich 1","Nguyen Dinh Huy, Nguyen Thi Xuan Anh",2000); 1
cout << checkAuthor(book1,"Nguyen Dinh Huy");

Book book1("Giai tich 1","Nguyen Dinh Huy, Nguyen Thi Xuan Anh",2000); 0
cout << checkAuthor(book1,"Nguyen Thi Xuan");

Answer: (penalty regime: 0 %)

Reset answer

28
29 std:: size_t lena {std:: strlen(authors) + 1 };
30 m_authors = new char[lena];
31 std:: strcpy(m_authors, authors);
32
33 }
34
35 ~Book()
36 ▼ {
37 delete[] m_title;
38 delete[] m_authors;
39 }
40
41 friend bool checkAuthor(Book& book, const char* author)
42 ▼ {
43 //copy the string of the book
44 char * token {};
45 std:: size_t len{std:: strlen(book.m_authors) + 1};
46 token = new char[len];
47 std:: strcpy (token,book.m_authors);
48
49 char* check = std::strtok(token,",");
50
51 while (check)
52 ▼ {
53 if (check[0] == ' ') check += 1;
54 //moving to the actual tokenisation
55 if (check)
56 {if ( std:: strcmp(check,author) == 0) return true; }
57 check = std:: strtok (nullptr, ",");
58 }
59
60 delete[] token;
61 return false;
62
63 }
64 };

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 18/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Test Expected Got

 Book book1("Giai tich 1","Nguyen Dinh Huy, Nguyen Thi Xuan Anh",2000); 1 1 
cout << checkAuthor(book1,"Nguyen Dinh Huy");

 Book book1("Giai tich 1","Nguyen Dinh Huy, Nguyen Thi Xuan Anh",2000); 0 0 
cout << checkAuthor(book1,"Nguyen Thi Xuan");

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 19/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Câu hỏi 10
Chính xác

Điểm 1,00 của 1,00

In this exercise, you will implement function printBook(const Book book) in class Printer to print information of the book. See example for
output format (no spaces at the end of each line and no empty lines at the end).

Note: In the authors attribute, it is possible to have more than one author writing a book together. So authors will have the following format:
“author1, author2, …, authorN”

For example:

Test Result

Book book1("Giai tich 1", "Nguyen Dinh Huy, Nguyen Thi Xuan Anh", 2000); Giai tich 1
Printer::printBook(book1); Nguyen Dinh Huy
Nguyen Thi Xuan Anh
2000

Book book1("Introduction to Algorithms", "Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Introduction to
Clifford Stein", 1990); Algorithms
Printer::printBook(book1); Thomas H. Cormen
Charles E. Leiserson
Ronald L. Rivest
Clifford Stein
1990

Answer: (penalty regime: 0 %)

Reset answer

35 ~Book()
36 ▼ {
37 delete[] m_title;
38 delete[] m_authors;
39 }
40
41 friend class Printer;
42 };
43
44 class Printer
45 ▼ {
46 public:
47 static void printBook(const Book& book) //calling the copy constructor, we don't actuallt specify it
48 //this will use the memberwise copy constructor, pointing to the same memory
49 ▼ {
50 //now we can have access to the book actually
51 std:: cout << book.m_title << '\n';
52 char *token{new char[std:: strlen(book.m_authors) + 1]};
53 std:: strcpy(token,book.m_authors);
54
55 char * check {std:: strtok(token, ",") };
56 while (check)
57 ▼ {
58 if (check[0] == ' ') check += 1;
59
60 std:: cout << check;
61
62 check = std::strtok(nullptr,",");
63
64 if (check) std:: cout << '\n';
65 }
66 std:: cout << '\n';
67 std:: cout << book.m_publishingYear;
68
69 delete[] token;
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 20/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review


6 de ete[] to e ;
70 }
71 };

Test Expected Got

 Book book1("Giai tich 1", "Nguyen Dinh Huy, Nguyen Thi Xuan Anh", 2000); Giai tich 1 Giai tich 1 
Printer::printBook(book1); Nguyen Dinh Huy Nguyen Dinh Huy
Nguyen Thi Xuan Anh Nguyen Thi Xuan Anh
2000 2000

 Book book1("Introduction to Algorithms", "Thomas H. Cormen, Charles E. Introduction to Introduction to 


Leiserson, Ronald L. Rivest, Clifford Stein", 1990); Algorithms Algorithms
Printer::printBook(book1); Thomas H. Cormen Thomas H. Cormen
Charles E. Charles E.
Leiserson Leiserson
Ronald L. Rivest Ronald L. Rivest
Clifford Stein Clifford Stein
1990 1990

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 21/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

Câu hỏi 11
Chính xác

Điểm 1,00 của 1,00

1. In the toy store, all toy has a price. Car toy has a price and color, Puzzle toy has a price and size. We have to implement class CarToy and
class PuzzleToy which inherit from class Toy.
2. class ToyBox has a pointer array to store a list of toys (up to 5 items including car and puzzle) and number of items in the box.

Your task is to implement two function addItem(…) in class ToyBox. If successfully added, the function returns the current number of toys in the
box. If the box is full, return -1.

For example:

Test Result

CarToy car(20000,red); This is a car toy


PuzzleToy puzzle(30000,small); This is a puzzle toy
car.printType();
puzzle.printType();

CarToy car(20000,red); This is a car toy


PuzzleToy puzzle(30000,small); This is a puzzle toy

ToyBox box;
box.addItem(car);
box.addItem(puzzle);
box.printBox();

Toy* toy = new CarToy(30000,red); This is a car toy


toy->printType();

Answer: (penalty regime: 0 %)

Reset answer

43 }
44
45 friend class ToyBox;
46 };
47
48 class PuzzleToy : public Toy
49 ▼ {
50 private:
51 Size size;
52
53 public:
54 PuzzleToy(double price, Size size) : Toy(price)
55 ▼ {
56 this->size = size;
57 }
58
59 void printType()
60 ▼ {
61 cout << "This is a puzzle toy\n";
62 }
63
64 friend class ToyBox;
65 };
66
67 class ToyBox
68 ▼ {
69 private:
70 Toy* toyBox[5]; //array of Toy pointer
71 int numberOfItems;
72
bli
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 22/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review


73 public:
74 ToyBox() : toyBox {nullptr}, numberOfItems {0}
75 ▼ {
76 ▼ /*
77 * STUDENT ANSWER
78 * TODO: set zero numberOfItems and nullptr toyBox
79 */
80 }
81
82 int addItem(const CarToy& carToy)
83 ▼ {
84 //0 1 2 3 4
85 ▼ /*
86 * STUDENT ANSWER
87 * TODO: function add a new Car toy to the box.
88 If successfully added, the function returns the current number of toys in the box.
89 If the box is full, return -1.
90 */
91 if (numberOfItems == 5) return -1;
92 toyBox [numberOfItems] =(Toy* )&carToy;

Test Expected Got

 CarToy car(20000,red); This is a car toy This is a car toy 


PuzzleToy puzzle(30000,small); This is a puzzle toy This is a puzzle toy
car.printType();
puzzle.printType();

 CarToy car(20000,red); This is a car toy This is a car toy 


PuzzleToy puzzle(30000,small); This is a puzzle toy This is a puzzle toy

ToyBox box;
box.addItem(car);
box.addItem(puzzle);
box.printBox();

 Toy* toy = new CarToy(30000,red); This is a car toy This is a car toy 
toy->printType();

Passed all tests! 

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

BÁCH KHOA E-LEARNING

WEBSITE

HCMUT
MyBK
BKSI

LIÊN HỆ

 268 Lý Thường Kiệt, P.14, Q.10, TP.HCM

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 23/24
Downloaded by Nguy?n V?n Hóa ([email protected])
lOMoARcPSD|44569428

9/29/23, 10:52 AM OOP: Attempt review

 (028) 38 651 670 - (028) 38 647 256 (Ext: 5258, 5234)

[email protected]

Copyright 2007-2022 BKEL - Phát triển dựa trên Moodle

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1365710&cmid=185477 24/24
Downloaded by Nguy?n V?n Hóa ([email protected])

You might also like