Sharif University of Technology C Programming Languages Lecturer

  • Slides: 15
Download presentation
Sharif University of Technology C++ Programming Languages Lecturer: Omid Jafarinezhad Fall 2013 Lecture 3

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

Classes and class members struct Date. Struct struct { int n. Month; int n.

Classes and class members struct Date. Struct struct { int n. Month; int n. Day; int n. Year; }; // Here is a function to initialize a date void Set. Date(Date. Struct &s. Date, int n. Month, int n. Day, int n. Year) { s. Date. n. Month = n. Month; s. Date. n. Month s. Date. n. Day = n. Day; s. Date. n. Year = n. Year; } // … In main … Date. Struct s. Today; Date. Struct // Initialize it manually s. Today. n. Month = 10; s. Today. n. Day = 14; s. Today. n. Year = 2040; Set. Date(s. Today, 10, 14, 2020); 2

Classes and class members struct Date. Struct { int n. Month; int n. Day;

Classes and class members struct Date. Struct { int n. Month; int n. Day; int n. Year; }; class Date { public: int m_n. Month; int m_n. Day; int m_n. Year; }; Date c. Today; // declare a Object of class Date // Assign values to our members using the member selector operator (. ) c. Today. m_n. Month = 10; c. Today. m_n. Day = 14; c. Today. m_n. Year = 2020; 3

Classes and class members #include <iostream> #include Employee c. Alex; class Employee c. Alex.

Classes and class members #include <iostream> #include Employee c. Alex; class Employee c. Alex. Set. Info("Alex", 1, 25. 00); { Employee c. Joe; public: public c. Joe. Set. Info("Joe", 2, 22. 25); char m_str. Name[25]; int m_n. ID; double m_d. Wage; c. Alex. Print(); c. Joe. Print(); // Set the employee information void Set. Info(char *str. Name, int n. ID, double d. Wage) { strncpy(m_str. Name, 25); m_n. ID = n. ID; m_d. Wage = d. Wage; } // Print employee information to the screen void Print() { using namespace std; cout << "Name: " << m_str. Name << " Id: " << m_n. ID << " Wage: $" << m_d. Wage << endl; } }; 4

Public vs private access specifiers class Access { int m_n. A; // private by

Public vs private access specifiers class Access { int m_n. A; // private by default int Get. A() { return m_n. A; } // private by default private: int m_n. B; // private int Get. B() { return m_n. B; } // private Access c. Access; // ok because m_n. D is public protected: int m_n. C; // protected c. Access. m_n. D = 5; int Get. C() { return m_n. C; } // protected // ok because Get. D() is public std: : cout << c. Access. Get. D(); public: int m_n. D; // public // WRONG because m_n. A is private int Get. D() { return m_n. D; } // public c. Access. m_n. A = 2; // WRONG because Get. B() is private }; std: : cout << c. Access. Get. B(); 5

Access functions and encapsulation class Date { private: int m_n. Month; int m_n. Day;

Access functions and encapsulation class Date { private: int m_n. Month; int m_n. Day; int m_n. Year; public: // Getters int Get. Month() { return m_n. Month; } Get int Get. Day() { return m_n. Day; } int Get. Year() { return m_n. Year; } // Setters void Set. Month( int n. Month) { m_n. Month = n. Month; } Set int void Set. Day(int n. Day) { m_n. Day = n. Day; } void Set. Year(int n. Year) { m_n. Year = n. Year; } }; 6

Public vs private access specifiers class Change { public: int m_n. Value; }; int

Public vs private access specifiers class Change { public: int m_n. Value; }; int main() { Change c. Change; c. Change. m_n. Value = 5; std: : cout << c. Change. m_n. Value << std: : endl; }; class Change class { private: int m_n. Value; public: void Set. Value(int n. Value) { m_n. Value = n. Value; } int Get. Value() { return m_n. Value; } }; int main() { Change c. Change; c. Change. Set. Value(5); std: : cout << c. Change. Get. Value() << std: : endl; } 7

Constructors • A constructor is constructor a special kind of class member function that

Constructors • A constructor is constructor 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 8

Constructors class Fraction { private: Fraction c. Default; // calls Fraction() constructor int m_n.

Constructors class Fraction { private: Fraction c. Default; // calls Fraction() constructor int m_n. Numerator; int m_n. Denominator; public: Fraction() // default constructor { m_n. Numerator = 0; m_n. Denominator = 1; } int Get. Numerator() { return m_n. Numerator; } int Get. Denominator() { return m_n. Denominator; } // … }; 9

Constructors with parameters class Fraction c. Default; // calls Fraction() constructor { Fraction c.

Constructors with parameters class Fraction c. Default; // calls Fraction() constructor { Fraction c. Five. Thirds(5, 3); // calls Fraction(int, int) private: constructor int m_n. Numerator; int m_n. Denominator; Fraction Six(6); // calls Fraction(int, int) constructor public: Fraction() { // default constructor m_n. Numerator = 0; m_n. Denominator = 1; } // Constructor with parameters Fraction(int n. Numerator, int n. Denominator = 1) { assert(n. Denominator != 0); m_n. Numerator = n. Numerator; m_n. Denominator = n. Denominator; } int Get. Numerator() { return m_n. Numerator; } int Get. Denominator() { return m_n. Denominator; } // … }; 10

Destructors • destructor is called when an object is destroyed • Like constructors, destructors

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 one destructor may exist per class there is no way to overload destructors since they can not no way to overload destructors be differentiated from each other based on arguments – The destructor has no return type 11

Destructors int main() { My. String c. My. Name("Alex"); // call constructor std: :

Destructors int main() { My. String c. My. Name("Alex"); // call constructor std: : cout << "My name is: " << c. My. Name. Get. String(); return 0; } // c. My. Name destructor called here! class My. String { private: private char *m_pch. String; int m_n. Length; public: My. String(const char *pch. String="") { My. String(const char *pch. String="") m_n. Length = strlen(pch. String) + 1; //Plus one character for a terminator m_pch. String = new char[m_n. Length]; strncpy(m_pch. String, m_n. Length); m_pch. String[m_n. Length-1] = ''; // Make sure the string is terminated } ~My. String() { // destructor delete[] m_pch. String; // We need to deallocate our buffer m_pch. String = 0; // Set m_pch. String to null just in case } char* Get. String() { return m_pch. String; } int Get. Length() { return m_n. Length; } }; 12

The hidden “this” pointer class Simple { private: int m_n. ID; public: Simple(int n.

The hidden “this” pointer class Simple { private: int m_n. ID; public: Simple(int n. ID) { Set. ID(n. ID); } void Set. ID(int n. ID) {this-> m_n. ID = n. ID; } this-> int Get. ID() { return this->m_n. ID; } this-> }; void Set. ID(int n. ID) { m_n. ID = n. ID; } // becomes (by compiler): void Set. ID(Simple* const this, int n. ID) { this->m_n. ID = n. ID; } Simple* const this-> 13

Friend functions // A function can be a friend of more than one class

Friend functions // A function can be a friend of more than one class Humidity; class Temperature { int m_n. Temp; public: Temperature(int n. Temp) { m_n. Temp = n. Temp; } friend void Print. Weather(Temperature &c. Temperature, Humidity &c. Humidity); Print. Weather }; class Humidity { int m_n. Humidity; public: Humidity(int n. Humidity) { m_n. Humidity = n. Humidity; } friend void Print. Weather(Temperature &c. Temperature, Humidity &c. Humidity); friend Print. Weather }; void Print. Weather(Temperature &c. Temperature, Humidity &c. Humidity) { Print. Weather std: : cout << c. Temperature. m_n. Temp c. Humidity. m_n. Humidity << std: : endl; c. Humidity. m_n. Humidity } 14

Friend classes class Display; Storage c. Storage(5, 6. 7); class Storage { Display c.

Friend classes class Display; Storage c. Storage(5, 6. 7); class Storage { Display c. Display(); int m_n. Value; double m_d. Value; c. Display. Item(c. Storage); public: Storage(int n. Value, double d. Value) { m_n. Value = n. Value; m_d. Value = d. Value; } // Make the Display class a friend of Storage friend class Display; }; class Display { /* … */ public: Display() { } void Display. Item(Storage &c. Storage) { cout << c. Storage. m_n. Value << " " << c. Storage. m_d. Value; c. Storage. m_n. Value c. Storage. m_d. Value } }; 15