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

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 39. Copy Constructor.
Advertisements

C++ Classes & Data Abstraction
OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
COEN 244 LECTURE SLIDES CLASSES AND CLASS MEMBERS struct DateStruct { int nMonth; int nDay; int nYear; };
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
C to C++ © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
C++ Review (3) Structs, Classes, Data Abstraction.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
CSC241 Object-Oriented Programming (OOP) Lecture No. 7.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 CSC241: Object Oriented Programming Lecture No 02.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
System Programming Practical Session 7 C++ Memory Handling.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 CSC241: Object Oriented Programming Lecture No 03.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
11 Introduction to Object Oriented Programming (Continued) Cats.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
1 Introduction to Object Oriented Programming Chapter 10.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Class 19 Chapter 13 – Creating a class definition.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
Overloading Operator MySting Example
Andy Wang Object Oriented Programming in C++ COP 3330
CS410 – Software Engineering Lecture #11: C++ Basics IV
14.4 Copy Constructors.
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
group work #hifiTeam
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS212: Object Oriented Analysis and Design
Classes Short Review of Topics already covered Constructor
CS410 – Software Engineering Lecture #5: C++ Basics III
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS31 Discussion 1D Winter18: week 6
Types of Computer Languages
Object Oriented Programming
Presentation transcript:

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);