Professional Documents
Culture Documents
Object Oriented Programming And: Class
Object Oriented Programming And: Class
06/09/2021 1
Object Oriented Programming
Implementation of Abstract Data Type (ADT)
1. Select a concrete data representation using built-in data types
2. Implement all relevant functions
06/09/2021 2
C++ Class
class class-name
{
//declaration statements here
//data members defined only not initialized
//add member functions prototype or complete implementation
};
// Class is simply definition no memory is reserved
06/09/2021 3
Creating Class Objects
• Objects are variables of class
• Separate data members memory is allocated only when object is created
• For member functions only one copy is created that is used by all objects
void main(){
Point p; X ? sec ? day ? ?
rollNum
myTime t; y ? min ? month ? courses ?
myDate d; hour ? year ? marks ?
Student s; name ?
} //Objects created but not initialized
06/09/2021 6
Object Member access Operator (.)
• Class members are accessed outside by name using dot operator
• Only if access specifier is public.
X ?
void main(){ y ?
class Point
Point p; {
cout << p.x;
//Compiler Error cannot access private member private:
outside int x;
cout << p.y; int y;
//Compiler Error cannot access private member
outside };
p.x = 100;
cin>>p.y;
//Compiler Error cannot access private member
outside
} 7
06/09/2021
Object Member access Operator (.)
• Class members are accessed outside by name using dot operator
• Only if access specifier is public.
X ?
void main(){ y ?
class Point
Point p; {
cout << p.x;
//Compiler Error cannot access private member int x;
outside int y;
cout << p.y; };
//Compiler Error cannot access private member
outside
p.x = 100;
cin>>p.y;
//Compiler Error cannot access private member
outside
} 8
06/09/2021
Object Member access Operator (->)
Dynamic objects and Pointers
• Class members are accessed outside by name using arrow operator
• Only if access specifier is public.
void main(){ class Point
?
Point * p = new Point; X {
//Allocate memory y ?
public:
//Dereference pointer dot member name int x;
cout << (*p).x ; int y;
//pointer name arrow member name
};
cout << p->y;
p->x = 100;
cin >> p->y;
06/09/2021 10
Object Assignment Operator (=)
• Member wise assignment of data.
• Only if member access specifier is public.
void main(){
100
class Point
Point p; X
y 50
{
p.x = 100; p.y = 50;
public:
Point p2; int x;
p2.x = p.x; X int y;
p2.y = p.y; y
//Member wise data assignment };
}
06/09/2021 11
Object Assignment Operator (=)
Dynamic objects and Pointers
• Member wise assignment of data.
• Only if member access specifier is public.
void main(){
class Point
Point p; {
X 100
p.x = 100; p.y = 50;
y 50
public:
Point * p2 = new Point;
int x;
p2->x = p.x; int y;
p2->y = p.y;
X
};
//Member wise data assignment
y
06/09/2021 15
Objects and functions (Pass by Reference)
• Functions can access class members only if member access specifier is public.
void main(){
Point p1; p1.x = 100; p1.y = 50; X 100
Point p2; p2.x = 10; p2.y = -30; y 50 class Point
{
update(p1, p2);
} X 10 public:
y -30 int x;
//object pass by reference int y;
void update(Point & p, Point q){
p.x = p.x + q.x; };
p.y = p.y + q.y;
}
06/09/2021 16
Object and functions (Return by value)
• Functions can access class members only if member access specifier is public.
void main(){
Point p1; p1.x = 100; p1.y = 50; X 100
Point p2; p2.x = 10; p2.y = -30; y 50 class Point
Point n = create(p1, p2); {
X 10 public:
}
y -30
int x;
//returns object’s copy
Point create(Point p, Point q){ int y;
Point n; };
n.x = p.x + q.x;
n.y = p.y + q.y;
return n;
}
06/09/2021 17
Dynamic Objects and functions
• Pass objects pointer in functions by value
• Functions can access class members only if member access specifier is public.
void main(){
Point * p1 = new Point;
p1->x = 100; p1->y = 50; 100
class Point
X
Point * p2 = new Point; y 50
{
p2->x = 10; p2->y = -30; public:
cout << equal (p1, p2); int x;
X 10
delete p1; delete p2; y
int y;
-30
} };
//Object pointer pass by value or copy
bool equal(Point * p, Point * q){ //Object’s data is always
if((p->x == q->x)&&(p->y == q->y)) pass by reference through
return true; pointers
else
return false;
06/09/2021} 18
Dynamic Objects and functions
• Pass objects pointer in functions by value
• Functions can access class members only if member access specifier is public.
void main(){
Point * p1 = new Point;
p1->x = 100; p1->y = 50; 100
class Point
X
Point * p2 = new Point; y 50
{
p2->x = 10; p2->y = -30; public:
cout << update (p1, p2); int x;
X 10
delete p1; delete p2; y
int y;
-30
} };
//Object pointer pass by value or copy
void update(Point * p, Point * q){ //Object’s data is always
p->x = p->x + q->x; pass by reference through
p->y = p->y + q->y; pointers
}
06/09/2021 19
Dynamic Objects and functions
• Pass objects pointer in functions by reference
• Functions can access class members only if member access specifier is public.
void main(){
Point * p1 = new Point;
p1->x = 100; p1->y = 50; 100
class Point
X
y 50
{
Point * p2; public:
createCopy(p2, p1);
int x;
delete p1; delete p2; int y;
} };
// Object pointer pass by reference
void createCopy(Point *& n, const Point * q){ //Object’s data is always
n = new Point; pass by reference through
n->x = q->x + 100; X pointers
n->y = q->y + 50; y
}
06/09/2021 20
Dynamic Objects and functions
• Return objects pointer from function
• Functions can access class members only if member access specifier is public.
void main(){
Point * p1 = new Point;
p1->x = 100; p1->y = 50; 100
class Point
X
y 50
{
Point * p2 = createCopy(p1); public:
delete p1; delete p2; int x;
} int y;
// Object pointer return from function };
Point * createCopy(const Point * q){ X
Point * n = new Point; y //Object’s data is always
n->x = q->x + 100; pass by reference through
n->y = q->y + 50; pointers
return n;
}
06/09/2021 21