C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 3 Department of Computer Engineering 1
Classes and class members 2 struct struct DateStruct { int nMonth; int nDay; int nYear; }; // Here is a function to initialize a date void SetDate(DateStruct &sDate, int nMonth, int nDay, int nYear) { sDate.nMonth sDate.nMonth = nMonth; sDate.nDay = nDay; sDate.nYear = nYear; } // … In main … DateStruct DateStruct sToday; // Initialize it manually sToday.nMonth = 10; sToday.nDay = 14; sToday.nYear = 2040; SetDate(sToday, 10, 14, 2020);
Classes and class members 3 struct DateStruct { int nMonth; int nDay; int nYear; }; class Date { public: int m_nMonth; int m_nMonth; int m_nDay; int m_nDay; int m_nYear; int m_nYear;}; // declare a Object of class Date Date cToday; // declare a Object of class Date // Assign values to our members using the member selector operator (.) cToday.m_nMonth = 10; cToday.m_nDay = 14; cToday.m_nYear = 2020;
Classes and class members 4 #include #include class Employee { public public: char m_strName[25]; int m_nID; double m_dWage; // Set the employee information void SetInfo(char *strName, int nID, double dWage) { strncpy(m_strName, strName, 25); m_nID = nID; m_dWage = dWage; } // Print employee information to the screen void Print() { using namespace std; cout << "Name: " << m_strName << " Id: " << m_nID << " Wage: $" << m_dWage << endl; } }; Employee Employee cAlex; cAlex.SetInfo("Alex", 1, 25.00); Employee Employee cJoe; cJoe.SetInfo("Joe", 2, 22.25); cAlex.Print(); cJoe.Print();
Public vs private access specifiers 5 class class Access { // private by default int m_nA; // private by default // private by default int GetA() { return m_nA; } // private by default private: // private int m_nB; // private // private int GetB() { return m_nB; } // private protected: // protected int m_nC; // protected // protected int GetC() { return m_nC; } // protected public: // public int m_nD; // public // public int GetD() { return m_nD; } // public }; Access Access cAccess; // ok because m_nD is public cAccess.m_nD = 5; // ok because GetD() is public std::cout << cAccess.GetD(); // WRONG because m_nA is private cAccess.m_nA = 2; // WRONG because GetB() is private std::cout << cAccess.GetB();
Access functions and encapsulation 6 class class Date {private: int int m_nMonth; int int m_nDay; int int m_nYear; public: // Getters Getreturn int GetMonth() { return m_nMonth; } return int GetDay() { return m_nDay; } return int GetYear() { return m_nYear; } // Setters void Setint void SetMonth(int nMonth) { m_nMonth = nMonth; } void void SetDay(int nDay) { m_nDay = nDay; } void void SetYear(int nYear) { m_nYear = nYear; } };
Public vs private access specifiers 7 class class Change { public: int m_nValue; }; int main() { Change cChange; cChange.m_nValue = 5; cChange.m_nValue = 5; cChange.m_nValue std::cout << cChange.m_nValue << std::endl; }; class class Change {private: int m_nValue; int m_nValue; public: void SetValue(int nValue) { m_nValue = nValue; } int GetValue() { return m_nValue; } int GetValue() { return m_nValue; } }; int main() { Change cChange; cChange.SetValue(5); cChange.GetValue() std::cout << cChange.GetValue() << std::endl; }
Constructors constructorclass member function executed when an object of that class is instantiated A constructor is a special kind of class member function that is executed when an object of that class is instantiated initialize member variables or to allow the user to easily initialize those member variables Constructors are typically used to initialize member variables of the class to appropriate default values, or to allow the user to easily initialize those member variables to whatever values are desired Constructors have specific rules for how they must be named: always have the same name as the class – Constructors should always have the same name as the class have no return type (not even void) – Constructors have no return type (not even void) constructor that takes no parameters default constructor A constructor that takes no parameters (or has all optional parameters) is called a default constructor 8
Constructors 9 Fraction class Fraction { private: int m_nNumerator; int m_nDenominator; public: Fraction() // default constructor { m_nNumerator = 0; m_nDenominator = 1; } int GetNumerator() { return m_nNumerator; } int GetDenominator() { return m_nDenominator; } // … // … }; Fraction cDefault; // calls Fraction() constructor
Constructors with parameters 10 Fraction class Fraction { private: int m_nNumerator; int m_nDenominator; public: Fraction() // default constructor Fraction() { // default constructor m_nNumerator = 0; m_nDenominator = 1; } // Constructor with parameters Fraction (int nNumerator, int nDenominator = 1) { assert(nDenominator != 0); m_nNumerator = nNumerator; m_nDenominator = nDenominator; } int GetNumerator() { return m_nNumerator; } int GetDenominator() { return m_nDenominator; } // … // … }; Fraction cDefault; // calls Fraction() constructor Fraction cFiveThirds(5, 3); // calls Fraction(int, int) constructor Fraction Six(6); // calls Fraction(int, int) constructor
Destructors when an object is destroyed destructor is called when an object is destroyed Like constructors, destructors have specific naming rules: the same name as the class, preceded by a tilde (~) – The destructor must have the same name as the class, preceded by a tilde (~) can not take arguments – The destructor can not take arguments one destructor may exist per class no way to overload destructors implies that only one destructor may exist per class, as there is no way to overload destructors since they can not be differentiated from each other based on arguments has no return type – The destructor has no return type 11
Destructors 12 class class MyString { private private: char *m_pchString; int m_nLength;public: MyString(const char *pchString="") MyString(const char *pchString="") { //Plus one character for a terminator m_nLength = strlen(pchString) + 1; //Plus one character for a terminator m_pchString = new char[m_nLength]; strncpy(m_pchString, pchString, m_nLength); // Make sure the string is terminated m_pchString[m_nLength-1] = '\0'; // Make sure the string is terminated } ~MyString() { // destructor ~MyString() { // destructor delete[] m_pchString; // We need to deallocate our buffer delete[] m_pchString; // We need to deallocate our buffer m_pchString = 0; // Set m_pchString to null just in case m_pchString = 0; // Set m_pchString to null just in case } char* GetString() { return m_pchString; } int GetLength() { return m_nLength; } }; int main() { MyString // call constructor MyString cMyName("Alex"); // call constructor std::cout << "My name is: " << cMyName.GetString(); return 0; // cMyName destructor called here! } // cMyName destructor called here!
The hidden “this” pointer 13 class Simple { private: int m_nID; public: Simple(int nID) { SetID(nID); } this-> void SetID(int nID) {this-> m_nID = nID; } this-> int GetID() { return this->m_nID; } }; void SetID(int nID) { m_nID = nID; } // becomes (by compiler): Simple* const this this-> void SetID(Simple* const this, int nID) { this->m_nID = nID; }
Friend functions 14 // A function can be a friend of more than one class class Humidity; class Temperature { int m_nTemp; public: Temperature(int nTemp) { m_nTemp = nTemp; } friend PrintWeather friend void PrintWeather(Temperature &cTemperature, Humidity &cHumidity); }; class Humidity { int m_nHumidity; public: Humidity(int nHumidity) { m_nHumidity = nHumidity; } friend PrintWeather friend void PrintWeather(Temperature &cTemperature, Humidity &cHumidity); }; PrintWeather void PrintWeather(Temperature &cTemperature, Humidity &cHumidity) { cTemperature.m_nTempcHumidity.m_nHumidity std::cout << cTemperature.m_nTemp << cHumidity.m_nHumidity << std::endl; }
Friend classes 15 class Display; class Storage { int m_nValue; double m_dValue; public: Storage(int nValue, double dValue) { m_nValue = nValue; m_dValue = dValue; } // Make the Display class a friend of Storage friend class friend class Display; }; class Display /* … */ class Display { /* … */ public: Display() { } void DisplayItem(Storage &cStorage) { cStorage.m_nValuecStorage.m_dValue cout << cStorage.m_nValue << " " << cStorage.m_dValue; } }; Storage cStorage(5, 6.7); Display cDisplay(); cDisplay.DisplayItem(cStorage);