1 / 15

C ++ Programming Languages

Sharif University of Technology. C ++ Programming Languages. Lecturer: Omid Jafarinezhad Fall 2013 Lecture 3. Department of Computer Engineering. Classes and class members. struct DateStruct { int nMonth ; int nDay ; int nYear ; }; // Here is a function to initialize a date

andra
Download Presentation

C ++ Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sharif University of Technology C++ Programming Languages Lecturer: OmidJafarinezhad Fall 2013 Lecture 3 Department of Computer Engineering

  2. Classes and class members structDateStruct { intnMonth; intnDay; intnYear; }; // Here is a function to initialize a date void SetDate(DateStruct &sDate, intnMonth, intnDay, intnYear) { sDate.nMonth = nMonth; sDate.nDay = nDay; sDate.nYear = nYear; } // … In main … DateStructsToday; // Initialize it manually sToday.nMonth = 10; sToday.nDay = 14; sToday.nYear = 2040; SetDate(sToday, 10, 14, 2020);

  3. Classes and class members structDateStruct { intnMonth; intnDay; intnYear; }; class Date { public: intm_nMonth; intm_nDay; intm_nYear; }; 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;

  4. Classes and class members #include <iostream> classEmployee { public:     char m_strName[25]; intm_nID;     double m_dWage; // Set the employee information voidSetInfo(char *strName, intnID, doubledWage)  { 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;     } }; EmployeecAlex; cAlex.SetInfo("Alex", 1, 25.00); EmployeecJoe; cJoe.SetInfo("Joe", 2, 22.25); cAlex.Print(); cJoe.Print();

  5. Public vs private access specifiers classAccess { intm_nA; // private by default intGetA() { return m_nA; } // private by default private: intm_nB; // private intGetB() { return m_nB; } // private protected: intm_nC; // protected intGetC() { return m_nC; } // protected public: intm_nD; // public intGetD() { return m_nD; } // public }; AccesscAccess; // 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();

  6. Access functions and encapsulation classDate { private: intm_nMonth; intm_nDay; intm_nYear; public: // Getters intGetMonth() { returnm_nMonth; } intGetDay() { returnm_nDay; } intGetYear() { returnm_nYear; } // Setters voidSetMonth(intnMonth) { m_nMonth = nMonth; } voidSetDay(intnDay) { m_nDay = nDay; } voidSetYear(intnYear) { m_nYear = nYear; } };

  7. Public vs private access specifiers class Change { private: intm_nValue; public: void SetValue(intnValue) { m_nValue = nValue; } intGetValue() { return m_nValue; } }; int main() {     Change cChange; cChange.SetValue(5);     std::cout << cChange.GetValue() << std::endl; } classChange { public: intm_nValue; }; int main() {     Change cChange; cChange.m_nValue = 5;     std::cout << cChange.m_nValue << std::endl; };

  8. Constructors • A constructor is a special kind of class member function that is executed when an object of that class is instantiated • 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: • Constructors should always have the same name as the class • Constructors have no return type (not even void) • A constructor that takes no parameters (or has all optional parameters) is called a default constructor

  9. Constructors class Fraction { private: intm_nNumerator; intm_nDenominator; public: Fraction()// default constructor     { m_nNumerator = 0; m_nDenominator = 1;     } intGetNumerator() { return m_nNumerator; } intGetDenominator() { return m_nDenominator; }     // … }; Fraction cDefault; // calls Fraction() constructor

  10. Constructors with parameters class Fraction { private: intm_nNumerator; intm_nDenominator; public: Fraction() { // default constructor m_nNumerator = 0; m_nDenominator = 1;     } // Constructor with parameters Fraction(intnNumerator, intnDenominator = 1)     {         assert(nDenominator != 0); m_nNumerator = nNumerator; m_nDenominator = nDenominator;     } intGetNumerator() { return m_nNumerator; } intGetDenominator() { 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

  11. Destructors • destructor is called when an object is destroyed • Like constructors, destructors have specific naming rules: • The destructor must have the same name as the class, preceded by a tilde (~) • The destructor can not take arguments • 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 • The destructor has no return type

  12. Destructors int main() { MyStringcMyName("Alex"); // call constructor     std::cout << "My name is: " << cMyName.GetString();     return 0; } // cMyName destructor called here! classMyString { private: char *m_pchString; intm_nLength; public: MyString(const char *pchString="") {        m_nLength = strlen(pchString) + 1; //Plus one character for a terminator   m_pchString = new char[m_nLength];   strncpy(m_pchString, pchString, m_nLength); m_pchString[m_nLength-1] = '\0'; // Make sure the string is terminated     } ~MyString() { // destructor        delete[] m_pchString;   // We need to deallocate our buffer    m_pchString = 0; // Set m_pchString to null just in case     } char*GetString() { returnm_pchString; } intGetLength() { returnm_nLength; } };

  13. The hidden “this” pointer class Simple { private: intm_nID; public:     Simple(intnID)     { SetID(nID);     }     void SetID(intnID) {this-> m_nID = nID; } intGetID() { return this->m_nID; } }; void SetID(intnID) { m_nID = nID; } // becomes (by compiler): void SetID(Simple* const this, intnID) { this->m_nID = nID; }

  14. Friend functions // A function can be a friend of more than one class class Humidity; class Temperature { intm_nTemp; public:     Temperature(intnTemp) { m_nTemp = nTemp; } friendvoidPrintWeather(Temperature &cTemperature, Humidity &cHumidity); }; class Humidity { intm_nHumidity; public:     Humidity(intnHumidity) { m_nHumidity = nHumidity; }  friend voidPrintWeather(Temperature&cTemperature, Humidity&cHumidity); }; voidPrintWeather(Temperature&cTemperature, Humidity&cHumidity) {     std::cout << cTemperature.m_nTemp << cHumidity.m_nHumidity << std::endl; }

  15. Friend classes class Display; class Storage { intm_nValue; doublem_dValue; public: Storage(intnValue, doubledValue) {  m_nValue = nValue; m_dValue = dValue;   } // Make the Display class a friend of Storage friendclassDisplay; }; class Display {   /* … */ public:     Display() { } voidDisplayItem(Storage &cStorage)  { cout << cStorage.m_nValue << " " << cStorage.m_dValue;     } }; StoragecStorage(5, 6.7); DisplaycDisplay(); cDisplay.DisplayItem(cStorage);

More Related