Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'microsoft visual c_2', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Object-Oriented Programming in C Employee Employee const string stName const string stEmployer Person stName m_stEmployer stEmployer Empty. void Employee Print const cout Name m_stName endl cout Company m_stEmployer endl In the main function we cannot create an object of the Person class as it is an abstract class. Neither can we let the pointer pPerson point at such an object. However we can let it point at an object of the class Student or Employee. The condition for Student and Employee being concrete classes was that they defined every pure virtual method so we can be sure that there always exists a definition of Print to call. Main.cpp include string include iostream using namespace std include Person.h include Student.h include Employee.h void main Does not work as Person is an abstract class. Person person John Smith person.Print cout endl Student student Mark Jones Berkeley student.Print cout endl Employee employee Adam Brown Microsoft employee.Print cout endl In this version there is no object person to point at. Person pPerson person pPerson- Print cout endl 64 Chapter 2 Person pPerson student pPerson- Print Calls Print in Student. cout endl pPerson employee pPerson- Print Calls Print in Employee. Arrays of Objects An array of objects is not really so much different from an array of values. However one issue to consider is that there is no way to call the constructor of each object individually. Therefore the class must have a default constructor or no constructor at all. Remember that if a class has one or more constructors one of them must be called every time an object of the class is created. The default constructor is called for each car object. Car carArray 3 carArray 2 .IncreaseSpeed 100 The default constructor is called for each car object. Car pDynamicArray new Car 5 pDynamicArray 4 .IncreaseSpeed 100 delete pDynamicArray Just as for values we can also initialize an object array with a list. In that case we can call constructors other than the .