Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures, Classes and Objects Handling data and objects Unit - 03.

Similar presentations


Presentation on theme: "Structures, Classes and Objects Handling data and objects Unit - 03."— Presentation transcript:

1 Structures, Classes and Objects Handling data and objects Unit - 03

2 Unit Introduction This unit covers structures, classes and objects

3 Unit Objectives After covering this unit you will understand… Structures Structures Classes Classes Static data and member functions Static data and member functions Differences between structures and classes Differences between structures and classes References References Friend functions and classes Friend functions and classes

4 Structures Following will be discussed in order to understand structures: Following will be discussed in order to understand structures: Declaration Declaration Definition Definition Using structures Using structures Nested structures Nested structures

5 Declaring Structures struct keyword is used for declaration struct keyword is used for declaration A structure is collection of variables and methods A structure is collection of variables and methods In a structure, variables can have different data types In a structure, variables can have different data types Can have private, protected and public access specifiers Can have private, protected and public access specifiers The default access specifier is public The default access specifier is public Semicolon “;” comes in the end to complete structure declaration Semicolon “;” comes in the end to complete structure declaration

6 Example: Declaring Structures #include #include struct SPart { private: private: int partType; // type of part, by default public int partType; // type of part, by default public public: public: float cost; // cost of part, be default public float cost; // cost of part, be default public int GetPartType() int GetPartType() { return partType; return partType; } }; // semicolon indicates end of declaration

7 Defining Structures With declaration With declaration A structure can be defined while declaring the structure A structure can be defined while declaring the structure Structure Tag (which is used to name the structure) is not required, if a structure variable is defined while declaring the structure Structure Tag (which is used to name the structure) is not required, if a structure variable is defined while declaring the structure Without declaration Without declaration Structures can be defined separately after declaration Structures can be defined separately after declaration

8 Example: Defining Structures // declaring and defining together struct // tag is not required here { int partType; // type of part int partType; // type of part float cost; // cost of part float cost; // cost of part } part; // declared and defined // declaring and defining separately struct SMachine // tag is required to define it separately { int machineType; // type of machine int machineType; // type of machine float cost;// cost of machine float cost;// cost of machine }; // declared only SMachine machine; // defined now

9 Using Structures Structure can be initialised either by: Structure can be initialised either by: putting values in it; or putting values in it; or assigning other structure of same type assigning other structure of same type A structure variable can be assigned to other if they are instance of same structure A structure variable can be assigned to other if they are instance of same structure Assigning different structure types (even though they have exactly the same data types) is not allowed and will generate an error Assigning different structure types (even though they have exactly the same data types) is not allowed and will generate an error

10 Using Structures (contd.) Structures are initialised using curly braces Structures are initialised using curly braces Dot (.) operator is used to access the variables Dot (.) operator is used to access the variables

11 Example: Using Structures // Initialising a structure variables # include # include struct SPart { int partType; // type of part int partType; // type of part float cost; // cost of part float cost; // cost of part}; void main() { SPart part1 = {12,23.56}; // Initializing variables SPart part1 = {12,23.56}; // Initializing variables SPart part2; SPart part2; part2 = part1; part2 = part1; part2.cost = 13.5f;// cost variable accessed part2.cost = 13.5f;// cost variable accessed}

12 Structures Structures Nesting can be to any level (nth level) Nesting can be to any level (nth level) Dot (.) operator is used to access the each inner level Dot (.) operator is used to access the each inner level When the inner most level is reached, the dot (.) operator will be used to access the variables and functions When the inner most level is reached, the dot (.) operator will be used to access the variables and functions

13 Example: Nested Structures // Example of Structure nesting # include # include struct SDistance { int feet; int feet; float inches; float inches;}; struct SRoom { SDistance length; SDistance length; SDistance width; SDistance width;};

14 Example: Nested Structures (contd.) void main() { SRoom dining = {{10,120},{10,120}} // Initializing SRoom dining = {{10,120},{10,120}} // Initializing SRoom study; SRoom study; study.length.feet = 10; // Assigning values study.length.feet = 10; // Assigning values study.length.inches = 120; study.length.inches = 120; study.width.feet = 10; study.width.feet = 10; study.width.inches = 120; study.width.inches = 120;}

15 Classes in C++ Following will be discussed in order to understand Classes in C++: Following will be discussed in order to understand Classes in C++: Classes and objects Classes and objects Access specifiers Access specifiers Member functions Member functions Constructors Constructors Destructors Destructors Static class data Static class data

16 Classes and Objects Classes are the infrastructures while objects are runtime utilisation of those infrastructures Classes are the infrastructures while objects are runtime utilisation of those infrastructures A class has functions and data A class has functions and data Member functions and data can be of type private, public or protected Member functions and data can be of type private, public or protected Default access specifier is private Default access specifier is private Variables cannot be initialised during declaration (as you can in Java) Variables cannot be initialised during declaration (as you can in Java)

17 Example: A Simple Class Example: A Simple Class // demonstrates an object #include #include class SmallObj // Specify a class name { private: private: int m_SomeData; // Class data int m_SomeData; // Class data public: public: void SetData(int data) // member function to set data void SetData(int data) // member function to set data { m_SomeData = data; m_SomeData = data; } void ShowData() // member function to display data void ShowData() // member function to display data { cout << “\nData is “ << m_SomeData; cout << “\nData is “ << m_SomeData; }};

18 Example: A Simple Class (contd.) void main() { SmallObj s1, s2; // defining two objects SmallObj s1, s2; // defining two objects s1.SetData(1234); // calling member function to set data s1.SetData(1234); // calling member function to set data s2.SetData(5677); s2.SetData(5677); s1.ShowData(); // calling member function to display s1.ShowData(); // calling member function to display // data // data s2.ShowData(); s2.ShowData();}

19 Constructors Constructor is a class method with exactly the same name as class name Constructor is a class method with exactly the same name as class name A constructor may accept argument(s) but does not return anything, not even void A constructor may accept argument(s) but does not return anything, not even void They are called when an instance of the class is created They are called when an instance of the class is created A default constructor does not accept any argument A default constructor does not accept any argument

20 Constructors (contd.) If a default constructor is not provided, compiler assumes there exists but it does not do anything If a default constructor is not provided, compiler assumes there exists but it does not do anything Constructors can be overloaded in their argument type and number of arguments Constructors can be overloaded in their argument type and number of arguments In constructors, variables can be initialised to a default value In constructors, variables can be initialised to a default value A copy constructor is used when objects are copied A copy constructor is used when objects are copied

21 Example: Constructors #include #include class Customer // Specify a class name { private: private: int m_CustomerId; // Class data int m_CustomerId; // Class data public: public: Customer() // no-arg or default constructor Customer() // no-arg or default constructor { m_CustomerId = 0; // default customer ID m_CustomerId = 0; // default customer ID } Customer(int newCustomerId) // one-arg constructor Customer(int newCustomerId) // one-arg constructor { m_CustomerId = newCustomerId; m_CustomerId = newCustomerId; }};

22 Example: Constructors void main() { Customer ordinaryCustomer; // creating instance using Customer ordinaryCustomer; // creating instance using // default constructor // default constructor Customer registeredCustomer(49); // creating instance Customer registeredCustomer(49); // creating instance // using one-arg constructor // using one-arg constructor}

23 Copy Constructor Copy constructor is used when objects are copied Copy constructor is used when objects are copied Default copy constructor performs shallow copy Default copy constructor performs shallow copy Explicit copy constructor can be provided to perform deep copy Explicit copy constructor can be provided to perform deep copy

24 Shallow and Deep Copy m_pAddress name : e1 City : LHR State : Punjab Country : PK Address m_pAddress name : e1 City : LHR State : Punjab Country : PK Address m_pAddress name : e1 City : LHR State : Punjab Country : PK Address m_pAddress name : e2 City : LHR State : Punjab Country : PK Address m_pAddress name : e2 InitiallyShallow CopyDeep Copy EmployeeEmployee Employee Employee Employee

25 Example: Copy Constructor struct SAddress { char* city; char* city; char* state; char* state; char* country; char* country; SAddress() SAddress() { city = ""; city = ""; state = ""; state = ""; country = ""; country = ""; } void DisplayAddress() void DisplayAddress() { cout << city << ", " << state << ", " << country << endl; cout << city << ", " << state << ", " << country << endl; }};

26 Example: Copy Constructor (contd.) class Employee { private: private: SAddress* m_pAddress; SAddress* m_pAddress; char* m_Name; char* m_Name; public: public: Employee() Employee() { m_pAddress = 0; m_pAddress = 0; } ~Employee() ~Employee() { delete m_pAddress; delete m_pAddress; }

27 Example: Copy Constructor (contd.) void SetAddress(char* city, char* state, void SetAddress(char* city, char* state, char* country) char* country) { m_pAddress = new SAddress(); m_pAddress = new SAddress(); m_pAddress->city = city; m_pAddress->city = city; m_pAddress->state = state; m_pAddress->state = state; m_pAddress->country = country; m_pAddress->country = country; } SAddress* GetAddress() SAddress* GetAddress() { return m_pAddress; return m_pAddress; } void SetName(char* name) void SetName(char* name) { m_Name = name; m_Name = name; }

28 Example: Copy Constructor (contd.) char* GetName() char* GetName() { return m_Name; return m_Name; } void Display() void Display() { cout << m_Name << " -- "; cout << m_Name << " -- "; m_pAddress->DisplayAddress(); m_pAddress->DisplayAddress(); } // this copy constructor performs deep copy // this copy constructor performs deep copy Employee(Employee& e) Employee(Employee& e){ m_pAddress = new SAddress(); m_pAddress = new SAddress(); m_pAddress->city = e.GetAddress()->city; m_pAddress->city = e.GetAddress()->city; m_pAddress->state = e.GetAddress()->state; m_pAddress->state = e.GetAddress()->state; m_pAddress->country = e.GetAddress()->country; m_pAddress->country = e.GetAddress()->country; }};

29 Example: Copy Constructor (contd.) void main() { Employee e1;// no-arg constructor is called Employee e1;// no-arg constructor is called e1.SetName("E1"); e1.SetName("E1"); e1.SetAddress("LHR", "Punjab", "PK"); e1.SetAddress("LHR", "Punjab", "PK"); e1.Display(); e1.Display(); Employee e2 = e1; // copy constructor is called Employee e2 = e1; // copy constructor is called e2.SetName("E2"); e2.SetName("E2"); e2.GetAddress()->city = ”ISB";// change city for e2 e2.GetAddress()->city = ”ISB";// change city for e2 e1.Display(); e1.Display(); e2.Display(); e2.Display();}

30 Destructors Called when an instance of the class is destroyed Called when an instance of the class is destroyed Destructor is a class method starting with tilde (~) and have exactly the same name as class name Destructor is a class method starting with tilde (~) and have exactly the same name as class name Destructors are used to release resources held by the instance (object) Destructors are used to release resources held by the instance (object) Destructor does not accept any argument Destructor does not accept any argument

31 Destructors (contd.) Destructor does not return anything, not even void Destructor does not return anything, not even void Destructors can not be overloaded Destructors can not be overloaded Memory allocated by the instance is released after calling the destructor Memory allocated by the instance is released after calling the destructor Automatically called when the object goes out of scope Automatically called when the object goes out of scope

32 Example: Destructors #include #include class MyFileHandler { private: private: ofstream m_file; ofstream m_file; public: public: MyFileHandler() MyFileHandler() { m_file.open(“myFile.txt”, ios::binary); m_file.open(“myFile.txt”, ios::binary); } ~MyFileHandler() ~MyFileHandler() { m_file.close; m_file.close; }

33 Example: Destructors (contd.) void WriteToFile(int buff[1000]) void WriteToFile(int buff[1000]) { m_file.write((char*)buff, 1000*sizeof(int)); m_file.write((char*)buff, 1000*sizeof(int)); }}; void main() { int myData[1000]; int myData[1000]; MyFileHandler myFileHandler; MyFileHandler myFileHandler; for(int j=0; j < 1000; j++) for(int j=0; j < 1000; j++) { myData[j] = j; myData[j] = j; } myFilehandler.WriteToFile(myData); myFilehandler.WriteToFile(myData);} // as myFileHandler goes out of scope, destructor is called // and the file is closed

34 Member Functions Member functions are methods of a class, could be: Member functions are methods of a class, could be: private, private, protected or protected or public public Can be declared and defined together or separately Can be declared and defined together or separately Inline function’s code is expanded where it is called and differ in normal function calling mechanism Inline function’s code is expanded where it is called and differ in normal function calling mechanism

35 Example: Member Functions #include #include class Employee // Specify a class { private: private: int m_EmployeeId; // Class data int m_EmployeeId; // Class data int m_EmployeeAge; int m_EmployeeAge; protected: protected: // implicit inline function // implicit inline function void ShowAge() // Member function to show void ShowAge() // Member function to show { // Employee age { // Employee age cout << “\nAge is “ << m_EmployeeAge; cout << “\nAge is “ << m_EmployeeAge; }

36 Example: Member Functions (contd.) public: public: void SetId(int id) // Member function to set id void SetId(int id) // Member function to set id { m_EmployeeId = id; m_EmployeeId = id; } void ShowId(); // Declaration only void ShowId(); // Declaration only void SetAge(int age); // Declaration only void SetAge(int age); // Declaration only}; void Employee::ShowId() { cout << “\nId is “ << m_EmployeeId; cout << “\nId is “ << m_EmployeeId;} // explicit inline function inline void Employee::SetAge(int age) { m_EmployeeAge = age; m_EmployeeAge = age;}

37 Example: Member Functions (contd.) void main() { Employee manager, worker; // defining two objects Employee manager, worker; // defining two objects manager.SetId(1001); // calling member function to manager.SetId(1001); // calling member function to // set Id // set Id worker.SetId(5001); // calling member function to worker.SetId(5001); // calling member function to // set Id // set Id manager.SetAge(45); // calling member function to set manager.SetAge(45); // calling member function to set // manger’s age // manger’s age wroker.SetAge(45); // calling member function to set wroker.SetAge(45); // calling member function to set // worker’s age // worker’s age}

38 Static Data Static data could be any type Static data could be any type Also called class data Also called class data Lifetime is the entire program Lifetime is the entire program Shared among all objects of the same class Shared among all objects of the same class Static data members are accessed using class name and :: operator Static data members are accessed using class name and :: operator In C++ static data is declared inside the class and defined outside the class In C++ static data is declared inside the class and defined outside the class

39 Example: Static Data class Counter // Specify a class { private: private: int m_Count; // class data int m_Count; // class data public: public: void IncrementCount() void IncrementCount() { m_Count++; m_Count++; } int GetTotalCount() int GetTotalCount() { return m_Count; return m_Count; } Counter() Counter() { m_Count = 0; m_Count = 0; } }; };

40 Example: Static Data (contd.) class Student { private: private: int m_StudentId; // class data int m_StudentId; // class data int m_StudentClass; int m_StudentClass; static Counter sm_Counter; // static counter to hold // count for all students static Counter sm_Counter; // static counter to hold // count for all students public: public: void AddStudent(int id,int studentClass) void AddStudent(int id,int studentClass) { m_StudentId = id; m_StudentId = id; m_StudentClass= studentClass; m_StudentClass= studentClass; sm_Counter.IncrementCount(); // increment count sm_Counter.IncrementCount(); // increment count } int GetStudentsCount() // getting class id int GetStudentsCount() // getting class id { return sm_Counter.GetTotalCount(); return sm_Counter.GetTotalCount(); }

41 Example: Static Data (contd.) }; Counter Student::sm_Counter; // if we don’t write this line // compiler will give error void main() { Student newStudent; Student newStudent; Student oldStudent; Student oldStudent; newStudent.AddStudent(100,10); newStudent.AddStudent(100,10); oldStudent.AddStudent(23,10); oldStudent.AddStudent(23,10); cout << “\nTotal = “ << oldStudent.GetStudentsCount(); cout << “\nTotal = “ << oldStudent.GetStudentsCount();}

42 Difference Between Structures and Classes The default access specifier in class is private, whereas in structure it is public The default access specifier in class is private, whereas in structure it is public Structures are inherited as public by default Structures are inherited as public by default Classes are inherited as private by default Classes are inherited as private by default

43 References Available only in C++, not in C Available only in C++, not in C One way to pass parameters to function is by reference One way to pass parameters to function is by reference The variable passed by reference, if altered, changes the actual variable value The variable passed by reference, if altered, changes the actual variable value & is used to denote a reference, e.g. &p (where p is any data type) & is used to denote a reference, e.g. &p (where p is any data type) References could be constant, called constant references, e.g. const &p References could be constant, called constant references, e.g. const &p

44 Example: References // Passing by reference example #include #include void main() { void IntFrac(const float&, float&, float&); void IntFrac(const float&, float&, float&); float number,intPart,fracPart; float number,intPart,fracPart; do do { cout << “\nEnter a real number:”; cout << “\nEnter a real number:”; cin >> number; cin >> number; Intfrac(number,intPart,fracPart); Intfrac(number,intPart,fracPart); cout << “Integer part is “ << intPart cout << “Integer part is “ << intPart << “, fraction part is “ << fracPart; << “, fraction part is “ << fracPart; }while (number != 0) }while (number != 0)}

45 Example: References (contd.) //IntFrac() // finds integer and fractional part of the real number void IntFrac(const float& n, float& intp, float& fracp) { intp = float(long(n)); intp = float(long(n)); fracp = n - intp; fracp = n - intp; // n can not be changed inside the function as it is // constant reference }

46 Friend Classes and Functions There are two types of friend modifiers: There are two types of friend modifiers: Class Level Class Level Function Level Function Level A friend function can access the private data of that class A friend function can access the private data of that class A friend function does not belong to a class, and A friend function does not belong to a class, and Its definition occurs outside the class without specifying the class name Its definition occurs outside the class without specifying the class name

47 Example: Friend Function / Class // friend function example #include #include class Beta;// forward declaration of class Beta class Alpha { private: private: int m_Data; // class private data int m_Data; // class private data public; public; Alpha() Alpha(){ m_Data = 3; m_Data = 3; } // assigning class data } // assigning class data friend int FriFunc(Alpha,Beta); // declaring friend friend int FriFunc(Alpha,Beta); // declaring friend // function // function friend Beta; // Beta is friend of // Alpha // Alpha};

48 Example: Friend Function / Class (contd.) class Beta { private private int m_Data; // class private data int m_Data; // class private data public: public: Beta() Beta() { m_Data = 7; m_Data = 7; } // assigning data } // assigning data void ChangeAlpha(Alpha& a); void ChangeAlpha(Alpha& a); friend int FriFunc(Alpha,Beta); // declaraing friend friend int FriFunc(Alpha,Beta); // declaraing friend }; // function int FriFunc(Alpha a, Beta b)// friend function { return (a.m_Data + b.m_Data); return (a.m_Data + b.m_Data);}

49 Example: Friend Function / Class (contd.) void Beta::ChangeAlpha(Alpha& a) { a.m_Data = 100;// change Alpha’s private data // as Beta is a friend of Alpha a.m_Data = 100;// change Alpha’s private data // as Beta is a friend of Alpha} void main() { Alpha aa;// define aa Alpha aa;// define aa Beta bb;// define bb Beta bb;// define bb cout << FriFunc(aa,bb);// calling friend function cout << FriFunc(aa,bb);// calling friend function bb.ChangeAlpha(aa);// change Alpha’s private data // through bb bb.ChangeAlpha(aa);// change Alpha’s private data // through bb cout << FriFunc(aa,bb);// calling friend function cout << FriFunc(aa,bb);// calling friend function}

50 Unit Summary In this unit you have covered … In this unit you have covered … Structures Structures Classes Classes Static data and member functions Static data and member functions References References Friend functions and classes Friend functions and classes


Download ppt "Structures, Classes and Objects Handling data and objects Unit - 03."

Similar presentations


Ads by Google