Unit-5.1 Notes, Fin
Unit-5.1 Notes, Fin
For Example: Consider the Class of Cars. There may be many cars with di erent
names and brand but all of them will share some common properties like all of
them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class
and wheels, speed limits, mileage are their properties.
• A Class is a user de ned data-type which has data members and member
functions.
•
• Data members are the data variables and member functions are the functions
used to manipulate these variables and together these data members and
member functions de nes the properties and behavior of the objects in a
Class.
•
• In the above example of class Car, the data member will be speed limit,
mileage etc and member functions can be apply brakes, increase speed etc.
• This access control is given by Access modi ers in C++. There are three
access modi ers : public, private and protected.
•
• EXAMPLE:
• #include <iostream.h>
• using namespace std;
• class student
• {
• // Access speci er
• public:
•
• // Data Members
• string stuname;
•
• // Member Functions()
• void printname()
• {
• cout << “student name is: " << stuname;
• }
• };
• int main() {
•
fi
fi
fi
fi
fi
fi
fi
• // Declare an object of class geeks
• Student obj1;
•
• // accessing data member
• obj1.stuname = "Abhi";
•
• // accessing member function
• obj1.printname();
• return 0;
• }
CONSTRUCTORS
•
A constructor is a special type of member function of a class which initializes
objects of a class.
• In C++, Constructor is automatically called when object(instance of class)
create.
• It is special member function of the class with the same name as that of class
and it does not have any return type.
•
• A constructor is di erent from normal functions in following ways:
• Constructor has same name as the class itself
• Constructors don’t have return type
• A constructor is automatically called when an object is created.
• It must be placed in public section of class.
ff
• If we do not specify a constructor, C++ compiler generates a default constructor
for object (expects no parameters and has an empty body).
•
fi
fi
fi
fi
fi
fi
fi
• DESTRUCTORS
• What is a destructor?
Destructor is an instance member function which is invoked automatically
whenever an object is going to be destroyed. Meaning, a destructor is the last
function that is going to be called before an object is destroyed.
•
• Syntax:
• ~constructor-name();
•
• Properties of Destructor:
• Destructor function is automatically invoked when the objects are destroyed.
• It cannot be declared static or const.
• The destructor does not have arguments.
• It has no return type not even void.
• An object of a class with a Destructor cannot become a member of the union.
• A destructor should be declared in the public section of the class.
• The programmer cannot access the address of destructor.
•
• When is destructor called?
A destructor function is called automatically when the object goes out of
scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
(4) a delete operator is called
•
• How are destructors di erent from a normal member function?
Destructors have same name as the class preceded by a tilde (~)
Destructors don’t take any argument and don’t return anything
•
EXAMPLE:
• class String {
• private:
• char* s;
• int size;
•
• public:
• String(char*); // constructor
• ~String(); // destructor
• };
•
• String::String(char* c)
• {
• size = strlen(c);
ff
• s = new char[size + 1];
• strcpy(s, c);
• }
• String::~String() { delete[] s; }