Download Object-Oriented Programming using C++: Lecture 6 - Classes and Objects and more Slides Object Oriented Programming in PDF only on Docsity! 3/7/2011 1 Object Oriented Programming using C++ Lecture – 6 Classes and Objects Classes Classes are similar to structures, i.e., used to group data and functionality in a single entity. Classes are defined for a group of objects who all are identical in their properties Similar to structures, as many objects of a class can be instantiated, as required There is very less difference between a structure Usman Younis and a class in C++, only that the Default visibility of a structure members is public Default visibility of a class members is private 3/7/2011 2 Class Declaration class my_Class { private: 1) Declaration of private members 2) Visible to all the member functions with in this class 3) Enforce the concept of data Hiding, in order to prevent unintentional mistakes public: Usman Younis 1) Declaration of public members 2) Visible in all sections of the program in which the objects of this class can be accessed }; Classes (contd..) Similar to structures, a class declaration acts as a blue-print of the objects which will be instantiated Similar to default data types, there is no limit on the number of object instantiated for a class type Data Member Functions Data D Usman Younis Member Functions Member Functions Data Member Functions ata Class Objects 3/7/2011 5 Constructor Car() : fuel(0.0), mileage(0.0), distance(0.0) { } //???????? Constructor function Aim is to initialize the data when the object is instantiated Same name as the name of the class T ll h il h I Usman Younis e s t e comp er t at am a constructor No return type Can be used to allocate dynamic memory for an object Destructor ~Car() { } //???????? Destructor function Aim is to de-allocate the memory of an object when it is destroyed at the program termination, or a function call Same name as the name of the class, with a “tilde” sign at the start T ll h il h I d Usman Younis e s t e comp er t at am a estructor No return type Must de-allocate dynamic memory, if any allocated for an Object 3/7/2011 6 Member Functions void Car::re_fuel(float a) { f l f l :: Scope resolution operator ue = a / ue _price; } void Car::check_fuel() { cout<<"Your car has : "<<fuel<<" litres left"<<endl; } Usman Younis void Car::set_mileage(float b) { mileage = b; } Member Functions (contd..) void Car::drive(float dist) { if((dist/mileage) > fuel) { cout<<"You need to re-fuel your car"<<endl; return; } fuel -= (dist/mileage); distance += dist; Usman Younis } void Car::check_distance() { cout<<"Your car has travelled : "<<distance<<" km"<<endl; } 3/7/2011 7 Main function int main (void) { Maserati.drive(100); Toyota.drive(100); Car Maserati, Toyota; Maserati.set_mileage(5.3); Toyota.set_mileage(13.4); Maserati.re_fuel(5000); T t f l(5000) Maserati.check_fuel(); Toyota.check_fuel(); Maserati.drive(200); Toyota.drive(200); M ti h k f l() Usman Younis oyo a.re_ ue ; Maserati.check_fuel(); Toyota.check_fuel(); asera .c ec _ ue ; Toyota.check_fuel(); return 0; } More on Constructors Both will work C () f l(0 0) il (0 0) di t (0 0) { }ar : ue . , m eage . , s ance . Car() { fuel = 0.0; il 0 0 Usman Younis m eage = . ; distance = 0.0; } 3/7/2011 10 Objects as function arguments void add_time(Time, Time); // declaration in class Time // following is the definition, outside the class d ddvoi Time::a _time(Time t1, Time t2) { min = t1.min + t2.min; hr = t1.hr + t2.hr; if (min > 60) { i 60 class Time { private: int min; int hr; public: Usman Younis m n -= ; hr++; } } Time() : min(0), hr(0) { } ~Time(); void add_time(Time, Time); Time add_time(Time); }; Returning Objects Time add_time(Time); // declaration in class Time // function definition, outside the class ddTime Time::a _time(Time t1) { Time temp; temp.min = min + t1.min; temp.hr = hr + t1.hr; if (temp.min > 60) { Usman Younis temp.min -= 60; temp.hr++; } return temp; } 3/7/2011 11 Classes, Objects, and Memory Each object will be allocated its memory when instantiated However, this only applies to the object’s data The member functions are same for all the objects of a class, and are allocated memory Usman Younis only once Classes, Objects, and Memory Data Data Data Data Data Data Object1 Object2 Object3 Usman Younis Function1() Function2() 3/7/2011 12 Static Class Data If some data is shared among all the objects: Make it Global Or, define it as static This preserves the data’s privacy and is still visible to all the objects instantiated from that class E.g., class abc { private: Usman Younis static int count; public: abc() {} int getcount() {return count++;} }; Static Class Data (contd..) int abc::count = 0; //static data is initialized outside the //class, before the main function int main() { abc a1, a2, a3; cout<<a1.getcount()<<endl; cout<<a2.getcount()<<endl; 0 1 Usman Younis cout<<a3.getcount()<<endl; return 0; } 2