







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Usman Younis
Default visibility of a structure members is public Default visibility of a class members is private
Class Declaration
class my_Class { private:
public:
Usman Younis
public:
Classes (contd..)
Similar to structures, a class declaration acts as a blue-print of the objects which will be instantiatedj 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
Data
Example (contd..)
Car
Re-fuel (add more fuel into the car for a given value of money) Set mileage (the fuel consumption, how many kms per
Usman Younis
Set mileage (the fuel consumption, how many kms per litre) Check the distance a car has traveled so far Check the fuel in car’s tank Drive!
Example (contd..)
class Car { private:p float fuel; float mileage; float distance; public: Car() : fuel(0.0), mileage(0.0), distance(0.0) { } ~Car() { }
Usman Younis
void re_fuel(float a); void check_fuel(); void set_mileage(float b); void drive(float dist); void check_distance(); };
Constructor
T ll h il h I
Usman Younis
Tells the compiler that I am a constructor
Destructor
T ll h il h I d
Usman Younis
Tells the compiler that I am a destructor
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
Toyota.re_fuel(5000);
Maserati.check_fuel(); Toyota.check_fuel();
Maserati.check_fuel(); Toyota.check_fuel();
return 0; }
More on Constructors
Both will work
fuel = 0.0; il 0 0
Usman Younis
mileage = 0.0; distance = 0.0;
More on Constructors (contd..)
Car() : fuel(0.0), mileage(0.0), distance(0.0) { cout<<“I am default constructor”<<endl; }
Car(float f, float m) : fuel(f), mileage(m), distance(0.0) { cout<<“I am overloaded constructor with two arguments”<<endl; }
Usman Younis
Car(float f, float m, float d) : fuel(f), mileage(m), distance(d) { cout<<“I am overloaded constructor with three arguments”<<endl; }
More on Constructors (contd..)
Calling overloaded constructor
void main() { Car Toyota; //default constructor is called Car Honda(500, 14); //overloaded constructor is called C Alf R (1000 5 0) // l d d t t i
Usman Younis
Car Alfa_Romeo(1000, 5, 0); //overloaded constructor is //called }
Objects as function arguments
void add_time(Time, Time); // declaration in class Time // following is the definition, outside the class void Time::add_time(Time t1, Time t2)d dd { 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
min -= 60; hr++; } }
public: 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 Time Time::add_time(Time t1)dd { 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; }
Classes, Objects, and Memory
Each object will be allocated its memory when instantiatedwhen 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
Usman Younis
Static Class Data (contd..)
Static variables are accessible through any object of the classany object of the class, or class name (if or class name (if public), e.g.,
Usman Younis