Technology will change our live

Bài Tập C++ Về Tính Kế Thừa


Chào tất cả mọi người. Trong bài trước thì mình đã chia sẻ tới mọi người về tính kế thừa trong lập trình hướng đối tượng. Trong bài này chúng ta cùng làm một số bài tập C++ để thực hành về nó nhé

Bài Tập Quản Lý Sinh Viên

Đề bài

Tạo class People gồm:
  • Thuộc tính: name, age,address để lưu lần lượt các giá trị tên, tuổi và địa chỉ.
  • Phương thức trong class People gồm: set(), get() là hàm nhập và xuất; hàm khởi tạo không tham số và hàm huỷ.
Tạo class Students kế thừa từ class People.
Class Students sẽ có thêm:
  • Thuộc tính id để lưu mã sinh viên, math lưu điểm môn toán, physical để lưu điểm môn vật lý, chemistry để lưu điểm môn hoá học.
  • Phương thức: set(), get(), GPA() để tính điểm trung bình 3 môn học.

Lời giải

Với những bài tập dạng này, chúng ta sẽ chia từng phần một để làm.
Ở đây mình sẽ tạo class People trước.

class People
{
protected:
    string name, address;
    int age;

public:
    People()
    {
        name = address = "";
        age = 0;
    }
    ~People()
    {
        name = address = "";
        age = 0;
    }
    void set()
    {
        cout << "Input" << endl;
        fflush(stdin);
        cout << "Name: ";
        fflush(stdin);
        getline(cin, this->name);
        cout << "Address: ";
        fflush(stdin);
        getline(cin, this->address);
        cout << "Age: ";
        cin >> this->age;
    }

    void get()
    {
        cout << "Output" << endl;
        cout << "Name: " << this->name << endl;
        cout << "Address: " << this->address << endl;
        cout << "Age: " << this->age << endl;
    }
};

Tiếp theo đó mình sẽ tạo tiếp class Students


class Students : public People
{
private:
    string id;
    int math, physical, chemistry;

public:
    Students()
    {
        id = "";
        math = physical = chemistry = 0;
    }

    ~Students()
    {
        id = "";
        math = physical = chemistry = 0;
    }

    void set()
    {
        People ::set();
        cout << "ID: ";
        cin >> this->id;
        cout << "Math: ";
        cin >> this->math;
        cout << "Physical: ";
        cin >> this->physical;
        cout << "Chemistry: ";
        cin >> this->chemistry;
    }

    void get()
    {
        People ::get();
        cout << "ID: " << this->id << endl;
        cout << "Math: " << this->math << endl;
        cout << "Physical: " << this->physical << endl;
        cout << "Chemistry: " << this->chemistry << endl;
        cout << "GPA = " << GPA();
    }

    float GPA()
    {
        return (this->math + this->physical + this->chemistry) / 3;
    }
};

Trong class Students, tại 2 phương thức set()get() thay vì việc viết lại code để nhập tên, tuổi, địa chỉ, mình đã gọi lại hàm set()get() trong class cha của nó.
Để gọi phương thức từ một lớp, mình đã sử dụng toán tử ::.
Các bạn có thể xem lại Tại đây
Code hoàn chỉnh:

#include <bits/stdc++.h>
using namespace std;

class People
{
protected:
    string name, address;
    int age;

public:
    People()
    {
        name = address = "";
        age = 0;
    }
    ~People()
    {
        name = address = "";
        age = 0;
    }
    void set()
    {
        cout << "Input" << endl;
        fflush(stdin);
        cout << "Name: ";
        fflush(stdin);
        getline(cin, this->name);
        cout << "Address: ";
        fflush(stdin);
        getline(cin, this->address);
        cout << "Age: ";
        cin >> this->age;
    }

    void get()
    {
        cout << "Output" << endl;
        cout << "Name: " << this->name << endl;
        cout << "Address: " << this->address << endl;
        cout << "Age: " << this->age << endl;
    }
};

class Students : public People
{
private:
    string id;
    int math, physical, chemistry;

public:
    Students()
    {
        id = "";
        math = physical = chemistry = 0;
    }

    ~Students()
    {
        id = "";
        math = physical = chemistry = 0;
    }

    void set()
    {
        People ::set();
        cout << "ID: ";
        cin >> this->id;
        cout << "Math: ";
        cin >> this->math;
        cout << "Physical: ";
        cin >> this->physical;
        cout << "Chemistry: ";
        cin >> this->chemistry;
    }

    void get()
    {
        People ::get();
        cout << "ID: " << this->id << endl;
        cout << "Math: " << this->math << endl;
        cout << "Physical: " << this->physical << endl;
        cout << "Chemistry: " << this->chemistry << endl;
        cout << "GPA = " << GPA();
    }

    float GPA()
    {
        return (this->math + this->physical + this->chemistry) / 3;
    }
};

int main()
{
    Students obj;
    obj.set();
    cout << endl;
    obj.get();
}

Input

Nguyen Van A
Ha Noi
20
1
7 8 9

Output

Output
Name: Nguyen Van A
Address: Ha Noi
Age: 20
ID: 1
Math: 7
Physical: 8
Chemistry: 9
GPA = 8

Bài Tập Màu Sắc

Đề bài

Xây dựng lớp Color gồm:
Thuộc tính: TenMau, MaMau
Phương thức:
  • Cấu tử không tham số
  • Cấu tử có tham số
  • Hủy
  • Nạp chồng toán tử nhập
  • Nạp chồng toán tử xuất
  • getTenMau() : hàm trả về TenMau
Xây dựng lớp Point gồm:
Thuộc tính: int x, y
Phương thức:
  • Cấu tử không tham số
  • Cấu tử có tham số
  • Hủy
  • Nạp chồng toán tử nhập
  • Nạp chồng toán tử xuất
  • CheoChinh : hàm kiểm tra Point có thuộc đường chéo chính hay không (1 điểm thuộc đường chéo chính khi và chỉ khi tung độ bằng hoành độ).
Xây dựng lớp Pixel kế thừa từ lớp Color và Point bao gồm thêm:
Phương thức:
  • Cấu tử không tham số
  • Cấu tử có tham số
  • Nạp chồng toán tử nhập
  • Nạp chồng toán tử xuất
  • KiemTra: hàm kiểm tra Pixel thuộc đường chéo chính và có màu “Xanh” hay không?
    Chương trình chính: Nhập vào từ bàn phím n Pixel (n nhập từ bàn phím). Hiển thị thông
    tin các Pixel thuộc đường chéo chính và có màu xanh.

Lời Giải

Trước tiên chúng ta cũng chia bài tập này ra thành các phần nhỏ
Phần đầu tiên chúng ta sẽ tạo lớp Color


class Color
{
protected:
 string tenmau, mamau;

public:
 Color()
 {
  tenmau = mamau = "";
 }
 Color(string tenmau, string mamau)
 {
  this->tenmau = tenmau;
  this->mamau = mamau;
 }
 ~Color()
 {
  tenmau = mamau = "";
 }

 friend istream &operator>>(istream &is, Color &obj)
 {
  cout << "Nhap Ten Mau: "; fflush(stdin); getline(is, obj.tenmau);
  cout << "Nhap Ma Mau: "; fflush(stdin); getline(is, obj.mamau);
  return is;
 };

 friend ostream &operator<<(ostream &os, Color obj)
 {
  os << "Ten Mau: " << obj.tenmau << endl;
  os << "Ma Mau: " << obj.mamau << endl;
  return os;
 }
 string getTenMau()
 {
  return this->tenmau;
 }
};

Tiếp theo là tạo lớp Point


class Point
{
protected:
 int x, y;

public:
 Point()
 {
  x = y = 0;
 }
 Point(int x, int y)
 {
  this->x = x;
  this->y = y;
 }
 ~Point()
 {
  x = y = 0;
 }

 friend istream &operator>>(istream &is, Point &obj)
 {
  cout << "Nhap x, y";
  is >> obj.x >> obj.y;
  return is;
 };
 friend ostream &operator<<(ostream &os, Point obj)
 {
  os << "x = " << obj.x << endl;
  os << "y = " << obj.y << endl;
  return os;
 }

 bool CheoChinh()
 {
  if (x == y)
   return true;
  else
   return false;
 }
};

Cuối cùng là lớp Pixel kế thừa từ 2 lớp ColorPoint
class Pixel : public Color, public Point
{
public:
 Pixel()
 {
  x = y = 0;
  tenmau = mamau = "";
 }

 Pixel(int x, int y, string tenmau, string mamau)
 {
  this->x = x;
  this->y = y;
  this->tenmau = tenmau;
  this->mamau = mamau;
 }

 ~Pixel()
 {
  x = y = 0;
  tenmau = mamau = "";
 }
 friend istream &operator>>(istream &is, Pixel &obj)
 {
  cout << "Nhap x: "; is >> obj.x;
  cout << "Nhap y: "; is >> obj.y;
  cout << "Nhap Ten Mau: "; is >> obj.tenmau;
  cout << "Nhap Ma Mau: "; is >> obj.mamau;
  return is;
 }
 friend ostream &operator<<(ostream &os, Pixel obj)
 {
  os << "x = " << obj.x << endl;
  os << "y = " << obj.y << endl;
  os << "Ten Mau = " << obj.tenmau << endl;
  os << "Ma Mau = " << obj.mamau << endl;
  return os;
 }

 bool Check()
 {
  if (x == y && mamau == "#0000FF")
   return true;
  else
   return false;
 }
};

Code hoàn chỉnh cho bài tập

#include <bits/stdc++.h>
using namespace std;

class Color
{
protected:
 string tenmau, mamau;

public:
 Color()
 {
  tenmau = mamau = "";
 }
 Color(string tenmau, string mamau)
 {
  this->tenmau = tenmau;
  this->mamau = mamau;
 }
 ~Color()
 {
  tenmau = mamau = "";
 }

 friend istream &operator>>(istream &is, Color &obj)
 {
  cout << "Nhap Ten Mau: "; fflush(stdin); getline(is, obj.tenmau);
  cout << "Nhap Ma Mau: "; fflush(stdin); getline(is, obj.mamau);
  return is;
 };

 friend ostream &operator<<(ostream &os, Color obj)
 {
  os << "Ten Mau: " << obj.tenmau << endl;
  os << "Ma Mau: " << obj.mamau << endl;
  return os;
 }
 string getTenMau()
 {
  return this->tenmau;
 }
};

class Point
{
protected:
 int x, y;

public:
 Point()
 {
  x = y = 0;
 }
 Point(int x, int y)
 {
  this->x = x;
  this->y = y;
 }
 ~Point()
 {
  x = y = 0;
 }

 friend istream &operator>>(istream &is, Point &obj)
 {
  cout << "Nhap x, y";
  is >> obj.x >> obj.y;
  return is;
 };
 friend ostream &operator<<(ostream &os, Point obj)
 {
  os << "x = " << obj.x << endl;
  os << "y = " << obj.y << endl;
  return os;
 }

 bool CheoChinh()
 {
  if (x == y)
   return true;
  else
   return false;
 }
};

class Pixel : public Color, public Point
{
public:
 Pixel()
 {
  x = y = 0;
  tenmau = mamau = "";
 }

 Pixel(int x, int y, string tenmau, string mamau)
 {
  this->x = x;
  this->y = y;
  this->tenmau = tenmau;
  this->mamau = mamau;
 }

 ~Pixel()
 {
  x = y = 0;
  tenmau = mamau = "";
 }
 friend istream &operator>>(istream &is, Pixel &obj)
 {
  cout << "Nhap x: "; is >> obj.x;
  cout << "Nhap y: "; is >> obj.y;
  cout << "Nhap Ten Mau: "; is >> obj.tenmau;
  cout << "Nhap Ma Mau: "; is >> obj.mamau;
  return is;
 }
 friend ostream &operator<<(ostream &os, Pixel obj)
 {
  os << "x = " << obj.x << endl;
  os << "y = " << obj.y << endl;
  os << "Ten Mau = " << obj.tenmau << endl;
  os << "Ma Mau = " << obj.mamau << endl;
  return os;
 }

 bool Check()
 {
  if (x == y && mamau == "#0000FF")
   return true;
  else
   return false;
 }
};

int main()
{
 cout << "Nhap So Luong: "; int n; cin >> n;
 Pixel *arr = new Pixel[n];
 for (int i = 0; i < n; i++) cin >> arr[i];

 cout << "Output" << endl;
 for (int i = 0; i < n; i++)
  if (arr[i].Check() == true)
   cout << arr[i];
 return 0;
}
Input

3
1 2 Red #FF0000
2 2 Blue #0000FF
2 3 Yellow #FFFF00

Output
Output
x = 2
y = 2
Ten Mau = Blue
Ma Mau = #0000FF
x = 4
y = 5
Ten Mau = Xanh
Ma Mau = #Xanh

Bài viết của mình đến đây là hết rồi. Mình rất mong nhận được sự quan tâm cũng như những ý kiến đóng góp của các bạn để bài viết ngày một tốt hơn. Cảm ơn các bạn rất nhiều.

Nếu các bạn muốn xem thêm bài tập kèm lời giải thì có thể truy cập tại:
  1. Github
Share:

0 nhận xét:

Đăng nhận xét

Follower

Biểu mẫu liên hệ

Tên

Email *

Thông báo *