Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Unit Introduction
This unit covers structures, classes and objects
Unit Objectives
After covering this unit you will understand Structures Classes Static data and member functions Differences between structures and classes References Friend functions and classes
Structures
Declaring Structures
struct keyword is used for declaration A structure is collection of variables and methods In a structure, variables can have different data types Can have private, protected and public access specifiers The default access specifier is public Semicolon ; comes in the end to complete structure declaration
{
private: int partType; public: // type of part, by default public
float cost;
}
}; // semicolon indicates end of declaration
Defining Structures
With declaration
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 Structures can be defined separately after declaration
Without declaration
// declaring and defining separately struct SMachine { int machineType; float cost; }; SMachine machine; // type of machine // cost of machine // declared only // defined now // tag is required to define it separately
Using Structures
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
struct SPart
{ int partType; float cost; // type of part // cost of part
};
void main() { SPart part1 = {12,23.56}; // Initializing variables
SPart part2;
part2 = part1; part2.cost = 13.5f; } // cost variable accessed
Structures
struct SDistance
{ int feet; float inches;
};
struct SRoom { SDistance length;
SDistance width;
};
study.length.feet
= 10;
// Assigning values
study.length.inches = 120;
study.width.feet study.width.inches } = 10; = 120;
Classes in C++
Classes are the infrastructures while objects are runtime utilisation of those infrastructures A class has functions and data Member functions and data can be of type private, public or protected Default access specifier is private Variables cannot be initialised during declaration (as you can in Java)
s1.SetData(1234); // calling member function to set data s2.SetData(5677); s1.ShowData(); s2.ShowData(); } // calling member function to display // data
Constructors
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 They are called when an instance of the class is created A default constructor does not accept any argument
Constructors (contd.)
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 In constructors, variables can be initialised to a default value A copy constructor is used when objects are copied
Example: Constructors
#include <iostream.h> class Customer // Specify a class name { private: int m_CustomerId; // Class data public: Customer() // no-arg or default constructor { m_CustomerId = 0; // default customer ID } Customer(int newCustomerId) // one-arg constructor { m_CustomerId = newCustomerId; } };
Example: Constructors
void main() { Customer ordinaryCustomer; // creating instance using // default constructor Customer registeredCustomer(49); // creating instance // using one-arg constructor
Copy Constructor
Copy constructor is used when objects are copied Default copy constructor performs shallow copy Explicit copy constructor can be provided to perform deep copy
Shallow Copy
Employee
m_pAddress name : e1 Employee
Deep Copy
m_pAddress name : e1
Address
City : LHR State : Punjab Country : PK
Address
City : LHR State : Punjab Country : PK
Destructors
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 Destructors are used to release resources held by the instance (object) Destructor does not accept any argument
Destructors (contd.)
Destructor does not return anything, not even void Destructors can not be overloaded Memory allocated by the instance is released after calling the destructor Automatically called when the object goes out of scope
Example: Destructors
#include <fstream.h> class MyFileHandler { private: ofstream m_file; public: MyFileHandler() { m_file.open(myFile.txt, ios::binary); } ~MyFileHandler() { m_file.close; }
Member Functions
Can be declared and defined together or separately Inline functions code is expanded where it is called and differ in normal function calling mechanism
// Class data
protected: // implicit inline function void ShowAge() // Member function to show { // Employee age cout << \nAge is << m_EmployeeAge; }
}; void Employee::ShowId() { cout << \nId is << m_EmployeeId; } // explicit inline function inline void Employee::SetAge(int age) { m_EmployeeAge = age; }
Static Data
Static data could be any type Also called class data Lifetime is the entire program Shared among all objects of the same class Static data members are accessed using class name and :: operator In C++ static data is declared inside the class and defined outside the class
The default access specifier in class is private, whereas in structure it is public Structures are inherited as public by default Classes are inherited as private by default
References
Available only in C++, not in C One way to pass parameters to function is by reference 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) References could be constant, called constant references, e.g. const &p
Example: References
// Passing by reference example #include <iostream.h> void main() { void IntFrac(const float&, float&, float&); float number,intPart,fracPart; do { cout << \nEnter a real number:; cin >> number; Intfrac(number,intPart,fracPart); cout << Integer part is << intPart << , fraction part is << fracPart; }while (number != 0) }
A friend function can access the private data of that class A friend function does not belong to a class, and Its definition occurs outside the class without specifying the class name
// // // // // //
define aa define bb calling friend function change Alphas private data through bb calling friend function
Unit Summary
In this unit you have covered Structures Classes Static data and member functions References Friend functions and classes