Class and Objects
Class and Objects
Physical objects…
Result Bank
Account
Logical objects…
Bank Account
Class: Car
Properties (Describe)
Company Methods (Functions)
Model Start
Color Drive
Mfg. Year Park
Price On_break
Fuel Type On_lock
Mileage On_turn
Gear Type
Power Steering
Anti-Lock braking system
Example:
class car
{
// data members and member functions
}car1;
▪ In above example class name is car, and car1 is object of that class.
Class
class car
{ Car
private:
int price; Attributes
float mileage; Price
public: Mileage
void start(); Methods
void drive(); Start
}; Drive
Class
class car
{
Object
private:
int main()
int price;
{
float mileage;
public: car c1;
void start(); c1.start();
void drive(); }
};
Class Object
class car
{ int main()
private: {
int price; car c1;
float mileage; c1.start();
public: c1.drive();
void start(); }
void drive();
};
• Write a C++ program to create class Test having data members mark
and spi.
• Create member functions SetData() and DisplayData()to
demonstrate class and objects.
Private Public
▪ByPrivate
defaultmembers of the class
all the members of can be
class
Class accessed within the class and from
are private
member functions of the class.
▪ A private member variable or
class car
function cannot be accessed, or
{ Private: even viewed from outside the class.
long int price;
float mileage;
void setdata() This feature in OOP is known as
{ Data hiding / Encapsulation
price = 700000;
mileage = 18.5;
}
};
• Private members of the class can be accessed within the class and
from member functions of the class.
• They cannot be accessed outside the class or from other programs,
not even from inherited class.
• If you try to access private data from outside of the class, compiler
throws error.
• This feature in OOP is known as Data hiding / Encapsulation.
• If any other access modifier is not specified then member default
acts as Private member.
Public Area
Entry allowed
to Data
Public area Functions
#include<iostream>
using namespace std;
class Time
{
private :
int hour, minute, second;
public :
void setTime(int h, int m, int s);
void print();
};
t1.setTime(h,m,s);
t1.print();
return 0;
}
raj.setData(27,1800);
raj. displaydata();
return 0;
}
• The member functions are created and placed in the memory space
only once at the time they are defined as part of a class specification.
• No separate space is allocated for member functions when the
objects are created.
• Only space for member variable is allocated separately for each
object because, the member variables will hold different data values
for different objects.
Member function 2
1) Default constructor
2) Parameterized constructor
3) Copy constructor
int main()
{
rectangle r1; // Invokes default constructor
rectangle r2(10,20); // Invokes parameterized
constructor
rectangle r3(r2); // Invokes copy constructor
}
};
Destructor
▪ never takes any argument nor it returns any value nor it has
return type.
▪ is invoked automatically by the complier upon exit from the
program.
▪ should be declared in the public section.
result = obj1.display();
void display()
{
cout<<"Real="<<real;
cout<<"Imaginary="<<imag;
}