170 likes | 334 Views
C++. More about Object and Classes Constructors Destructors The ‘this’ pointer Inheritance. C++. Constructors More than one constructor is possible due to C++’s function overloading Each has to have its own signature (unique parameters) Used to initialize the members of the class object
E N D
C++ • More about Object and Classes • Constructors • Destructors • The ‘this’ pointer • Inheritance
C++ • Constructors • More than one constructor is possible due to C++’s function overloading • Each has to have its own signature (unique parameters) • Used to initialize the members of the class object • Primary form: Student s1 = Student(“name”,”address”,4000); Or Student s1 = {“name”,”address”,4000} for C++11 • Short form: Student s1(“name”,”address”,4000); • Dynamic form: Student *s1 = new Student(“name”,”address”,4000); • Default form: Student s1();
C++ • Default Constructor • Created by C++ only if no constructor is supplied • It is implicit in the language • It is a constructor with no parameters and no body • Typically, not good practice to ignore data initialization.
C++ • Destructors • Used for tracking object termination. • Main responsibility is cleanup. • In general, a program should not call a destructor explicitly. • If not provided, a default constructor is generated by C++ • Good practice to provide a destructor
C++ • The ‘this’ pointer • A special pointer that every member function has access to. • It points to its own object • If const is used after the parameter ( ), this points as a constant and cannot be used to change the object • ‘*this’ refers to the object itself whereas ‘this’ refers to the address of the object
C++ // student.cpp file #include <iostream> #include "lab4.h" using namespace std; Student::Student() { ssn = ""; gpa = 0.0; address = ""; tuitionOwed = 0; name = ""; } Student::~Student() { } . . . floatStudent::getStudentTuition(std::string SSN) { float rc = tuitionOwed; return(rc); } Student & Student::compareTuition(Student & s) { if (s.getSudentTuition() > this->getStudentTuition()){ return s; } else{ return *this; } } #ifndefSTUDENT_H #define STUDENT_H #include <iostream> #include <string> class Student { private: std::string ssn; float gpa; std::string address; float tuitionOwed; public: std::string name; Student(); ~Student(); . . . float getStudentTuition(); Student & compareTuition(const Student & s); }; #endif
C++ • Static Constants and Enumerations with class scope • C++ won’t allow a member to be assigned a value with the exception of static const and enum • Enumeration Case (Symbolic constants) • enum {MAXHOURS=21}; • enum {MAXHOURS=21, MINHOURS=12} • Static Case (Real constant stored with all statics) • static const float MINGPA = 0.0; static const float MAXGPA=4.0; • Enumeration as a class • enum class HOURS {PARTTIME, FULLTIME, CONT_ED }; HOURS ed = HOURS::PARTTIME;
C++ • Inheritance • A method of providing reusable code. • A way to develop safer code • Typically already used and tested • Allows the developer to concentrate on new code. • Allows a developer • To expand the functionality of a class • To append data into the class • Change the way in which class methods behave.
C++ • Inheritance enables class derivation • A way of creating a “new” class from an existing one • Derived object stores the members of the base type(s) • Derived object has access to use methods of the base type(s) • Must have its own constructor and destructor
C++ • Inherited Relationships • Derived class has access to non-private base class data members. • Derived class has access to non-private base class member functions. • Base class object can point to a derived class object. • C++ typically is very strongly typed and a cast must be used in order to allow one type access to another type. • Type casting rules are relaxed for inherited classes.
C++ • Is-a Relationship • Typically, uses public inheritance • The Is-a statement should be able to be used • An engineering student is a student • An orange is a fruit • Is a Student a College – no. So the Student class should not inherit College • Has a relationships usually mean that one object contains the other object as one of its members • College has a Student for example • C++ doesn’t disallow has a relationships from inheritance
C++ • Virtual Functions • Static and Dynamic binding • Static binding occurs at compile-time • Dynamic binding occurs during run-time • Use keyword ‘virtual’ to declare a virtual method. • A virtual function of a derived class can be used to “overwrite” virtual function of a base class. • Held within a “hidden” virtual function table in each object • Each address to the virtual function is contained in the virtual function table (VFT). • Therefore, an object of a derived class has a different VFT than the base object. If a function is redefined the redefined function address is kept in the VFT of the object redefining the function.
C++ • Inherited constructors • A derived class has no access to base class private members • Base class private member data must be passed through the derived constructor • Base class is omitted, the default constructor is used. • What are the takeaway principles • Base class is constructed first • Derived class constructor should pass base class information for the base class constructor • Derived class constructor should initialize the data members that were appended to the class.
C++ class HonorStudent: public Student { private: intHonorCredits; public: HonorStudent(inthc, float grade, std::string addr, float tuition) : Student( grade, addr, tuition) ); ~HonorStudent(); boolsetStudentGPA(float GPA); }; #endif #ifndefSTUDENT_H #define STUDENT_H #include <iostream> #include <string> class Student { private: std::string ssn; float gpa; std::string address; float tuitionOwed; public: std::string name; Student(); Student(std::string s, float grade, std::string addr, float tuition); ~Student(); boolsetStudentName(std::string studentName); boolsetStudentSSN(std::string SSN); virtual boolsetStudentGPA(float GPA); boolsetStudentAddress(std::string studentAddr); boolsetStudentTuition(float tuition); std::string getStudentName(); std::string getStudentSSN(); float getStudentGPA(); std::string getStudentAddress(); float getStudentTuition(); Student & compareTuition(const Student & s); };
C++ #include <iostream> #include <string> #include <student.h> Student::Student( ) { ssn= “” ; gpa = 0.0; tuitionOwed = 0.0; address = “”; } Student:: Student(std::string s, float grade, std::string addr, float tuition) { ssn = s; gpa = grade; tuitionOwed = tuition; address = “”; } HonorStudent::HonorStudent (inthc, float grade, std::string addr, float tuition) : Student( grade, addr, tuition) { HonorCredits = hc; } boolHonorStudent::setStudentGPA(float GPA) { boolrc = false; if (GPA > =0 && GPA <= 5.0) { gpa = GPA; rc = true; } else rc = false; return rc; }; boolStudent::setStudentGPA(float GPA) { boolrc = false; if (GPA > =0 && GPA <= 4.0) { gpa = GPA; rc = true; } else rc = false; return rc; };
C++ #include <iostream> #include <string> #include <student.h> using namespace std; int main ( ) { boolrc = false; Student baseStudent(); HonorStudenthStudent(8, 4.5, “#1 East Ln, Somewhere, AL”, 2000.00); rc = hStudent.setStudentGPA(4.75); cout << “honor student set: “ << rc << std::endl; rc = baseStudent.setStudentGPA(3.75); cout << “base student set: “ << rc << std::endl; }