Docsity

Object-Oriented Programming using C++: Lecture 6 - Classes and Objects, Slides of Object Oriented Programming

Toggle sidebar
bg1
3/7/2011
1
Ob
j
ect Oriented
j
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
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
There is very less difference between a structure
and a class in C++, only that the
Default visibility of a structure members is public
Default visibility of a class members is private
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Object-Oriented Programming using C++: Lecture 6 - Classes and Objects and more Slides Object Oriented Programming in PDF only on Docsity!

Object Orientedj

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 entitydata 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

There is very less difference between a structure

and a class in C++, only that the

‰ Default visibility of a structure members is public ‰ Default visibility of a class members is private

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

public:

  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 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

Class

Objects

Example (contd..)

Car

„ Functions:

‰ 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

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

‰ Tells the compiler that I 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

‰ Tells the compiler that I am a destructor

„ No return type

„ Must de-allocate dynamic memory, if any allocated

for an Object

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

CCar() : fuel(0.0), mileage(0.0), distance(0.0) { } () f l(0 0) il (0 0) di t (0 0) { }

Car()

fuel = 0.0; il 0 0

Usman Younis

mileage = 0.0; distance = 0.0;

More on Constructors (contd..)

„ Similar to overloaded functions, a class constructor

can also be overloaded

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

Data Data Data

Data Data Data

Object1 Object2 Object

Usman Younis

Function1()

Function2()

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.,

‰ cout<<abc::count; //if public

‰ cout<<a1.count; //if public

‰ cout<<a2.getcount(); //if private

Usman Younis