Oop Attempt Review Oop
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)
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
Câu hỏi 1
Chính xác
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
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
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
Câu hỏi 2
Chính xác
Your task in this exercise is to implement functions marked with /* * STUDENT ANSWER */.
For example:
Test Result
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 }
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
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
Câu hỏi 3
Chính xác
In this exercise, you can use implemented functions in previous question (if needed) and implement these following functions.
bool containsTriangle(const Point pointA, const Point pointB, const Point pointC){}
For example:
Test Result
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
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
Câu hỏi 4
Chính xác
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:
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
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
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
Câu hỏi 5
Chính xác
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
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
Character ch2; 0 0 0 0 0 0
cout << ch2.getHp() << " " << ch2.getX() << " " << ch2.getY();
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();
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
Câu hỏi 6
Chính xác
// 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
Reset answer
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
Câu hỏi 7
Chính xác
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
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
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
Câu hỏi 8
Chính xác
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
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
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
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
Câu hỏi 9
Chính xác
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");
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
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");
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
Câu hỏi 10
Chính xác
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
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
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
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
Câu hỏi 11
Chính xác
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
ToyBox box;
box.addItem(car);
box.addItem(puzzle);
box.printBox();
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
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();
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
WEBSITE
HCMUT
MyBK
BKSI
LIÊN HỆ
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
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])