Câu lệnh tiền xử lý trong C++ | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create 3 classes Person, Student, Teacher within 3 following files: Person.cpp, Student.cpp, Teacher.cpp with the following information:

Explanantion:

Person claas

  • name, age, address: attributes indicate name, age and address of the object.
  • Person(name: String, age: int, address: String) is parameter constructor which is used for initializing values to attribues.
  • display(): method displays object information on the screen. For example, if name = "Khanh", age = 23, address = "Ha Noi", when we call this method, the screen will display as below:
  • Name: Khanh
    Age: 23
    Address: Ha Noi​
  • Other methods are setter and getter.

Student class

  • Student class inherits from Person class.
  • Student(name: String, age: int, address: String, gpa: double) is the parameter constructor which is used for initializing values to attribues.
  • setGpa() and getGpa() are setter and getter.
  • display() is an overridden method from Person class, it is used for displaying the object information on the screen. For example, if name = "Khanh", age = 23, address = "Ha Noi", gpa = 9.0, when we call this method, the screen will display as below:
  • Name: Khanh
    Age: 23
    Address: Ha Noi​
    GPA: 9.0​

Teacher class

  • Teacher: class inherits from Person class.
  • Teacher(name: String, age: int, address: String, salary: int) is the parameter constructor which is used for initializing values to attribues.
  • setSalary() and getSalary() are setter and getter.
  • display() is an overridden method from Person class which is used for displaying object information on the screen. For example, if name = "Tung", age = 34, address = "Ha Noi", salary = 1700, when we call this method, the screen will display as below:
    Name: Tung
    Age: 34
    Address: Ha Noi​
    Salary: 1700​

A program to test the above classes:

#include "Student.cpp"
#include "Person.cpp"
#include "Teacher.cpp"

int main() {
	Student s("Khanh", 23, "Ha Noi", 9.0);
	s.display();
	Teacher t("Tung", 34, "Ha Noi", 1700);
	t.display();
	return 0;
}

When the above code is compiled and executed, it produces the following result:

Name: Khanh
Age: 23
Address: Ha Noi
GPA: 9.0

Name: Tung
Age: 34
Address: Ha Noi
Salary: 1700

Theory

First you may notice that you need to create the following files: Person.cpp, Student.cpp, Teacher.cpp. In Student.cpp file will be included in both files Student.cpp and Teacher.cpp. First of all, try to create 4 files with the following content:

File Person.cpp:

class Person {
};

File Student.cpp:

#include "Person.cpp"

class Student : public Person {
};

File Teacher.cpp:

#include "Person.cpp"

class Teacher : public Person {
};

File source.cpp (file that contains main function):

#include "Student.cpp"
#include "Serson.cpp"
#include "Teacher.cpp"

int main() {
	return 0;
}

When running source.cpp file, the following error displays on the screen:

It can be seen that the code in the three files above is completely normal, but the program displays an error because Person.cpp file has been included many times leading the compiler to understand that there are more than one Person class. You can simply understand that the #include statement copies and pastes the file included in the current file, so the source code in the source.cpp file looks like this:

class Person {
};

class Person {
};

class Student : public Person {
};

class Person {
};

class Teacher : public Person {
};

int main() {
	return 0;
}

When running the above program, you will see an error similar to the one above. Therefore, the problem comes from including Person.cpp file multiple times. To avoid this error you can add #pragma once preprocessor statement to Person.cpp file to avoid multiple inclusions:

#pragma once
class Person {
};

In addition to #pragma once, you can use the following preprocessor statements #ifndef, #define, #endif as below:

#ifndef PERSON_CPP
#define PERCON_CPP
class Person {
};
#endif

Instruction

Code sample:

File Person.cpp:

#pragma once
#include<iostream>

using namespace std;

class Person {
private:
	string name;
	int age;
	string address;

public:
	Person(string name, int age, string address) {
		this->name = name;
		this->age = age;
		this->address = address;
	}

	string getName() {
		return name;
	}

	void setName(string name) {
		this->name = name;
	}

	int getAge() {
		return age;
	}

	void setAge(int age) {
		this->age = age;
	}

	string getAddress() {
		return address;
	}

	void setAddress(string address) {
		this->address = address;
	}

	void display() {
		cout << "Name: " << name << endl;
		cout << "Age: " << age << endl;
		cout << "Address: " << address << endl;
	}
};

File Student.cpp:

#include "Person.cpp"

using namespace std;

class Student : public Person {
private:
	double gpa;

public:
	Student(string name, int age, string address, double gpa) : Person(name, age, address) {
		this->gpa = gpa;
	}

	double getGpa() {
		return gpa;
	}

	void setGpa(double gpa) {
		this->gpa = gpa;
	}

	void display() {
		Person::display();
		cout << "GPA: " << gpa << endl;
	}
};

File Teacher.cpp:

#include "Person.cpp"

class Teacher : public Person {
private:
	int salary;

public:
	Teacher(string name, int age, string address, int salary) : Person(name, age, address) {
		this->salary = salary;
	}

	int getSalary() {
		return salary;
	}

	void setSalary(int salary) {
		this->salary = salary;
	}
	void display() {
		Person::display();
		cout << "Salary: " << salary << endl;
	}
};

Reset
...