0% found this document useful (0 votes)
21 views

C++ Classes, Objects, Data Members

This document discusses object oriented programming concepts in C++ including classes, objects, data members, member functions, access specifiers, and the dot and arrow operators. It explains that a class defines a new user-defined data type by collecting together data members and member functions. Objects are variables of a class type that allocate separate memory for data members. The dot operator is used to access public members of an object and the arrow operator for pointer to object access.

Uploaded by

Muhammad Talha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

C++ Classes, Objects, Data Members

This document discusses object oriented programming concepts in C++ including classes, objects, data members, member functions, access specifiers, and the dot and arrow operators. It explains that a class defines a new user-defined data type by collecting together data members and member functions. Objects are variables of a class type that allocate separate memory for data members. The dot operator is used to access public members of an object and the arrow operator for pointer to object access.

Uploaded by

Muhammad Talha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Object Oriented Programming

C++ Class and Object


CS(217) Object Oriented Programming
Abeeda Akram

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

C++ Class (class is reserve word in C++)


• Class is used to only define new data types.
• It is collection of
• Data called data members or attributes
• Functions called member functions, methods or behaviors

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

class myTime class myDate class Student


class Point
{ { {
{
int sec; int day; int rollNum;
int x;
int min; int month; int courses;
int y;
int hour; int year; float marks;
};
}; }; char name[20];
};

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

class myTime class myDate class Student


class Point
{ { {
{
int sec; int day; int rollNum;
int x;
int min; int month; int courses;
int y;
int hour; int year; float marks;
};
}; }; char name[20];
};
06/09/2021 4
class Point

Class Member access specifiers {


private:
int x;
private:(reserve word in C++) int y;
• Class members accessible only to member functions of class };
• Not accessible outside class (user defined functions)
public:(reserve word in C++) class Point
{
• Class members accessible to member functions of class
public:
• Also accessible outside class (user defined functions) int x;
protected:(reserve word in C++) int y;
• Class members Accessible to member functions and derived };
classes (will use and discuss later on)
By default class member access is private, if no class Point
{
access specifier is mentioned int x;
int y;
};
06/09/2021 5
Object Member access Operator (.)
• Class members are accessed outside class by name using dot operator
• Only if access specifier is public.
?
void main(){ X class Point
Point p; y ?
{
cout << p.x; public:
//object variable name dot member name int x;
cout << p.y; int y;
//object variable name dot member name
};
p.x = 100;
cin>>p.y;
//initialize members
}

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;

delete p; //Deallocate memory


}
06/09/2021 9
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 ?
private:
//Compiler Error cannot access private members int x;
cout << (*p).x ; int y;
cout << p->y;
p->x = 100;
};
cin >> p->y;

delete p; //Deallocate memory


}

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

//Aggregate data assignment


p2 = p; //Same as 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

//Aggregate data assignment


*(p2) = p; //Same as member wise data assignment
delete p2; //Deallocate memory
}
06/09/2021 12
Object Relational Operators ( ==, !=, <=, >=, <,
>)
• Always compare data member wise.
• Only if member access specifier is public.
void main(){
Point p; class Point
p.x = 100; p.y = 50; {
X 100
y 50
public:
Point * p2 = new Point;
p2->x = 30; p2->y = 50; int x;
int y;
cout << p2->x != p.x; };
X 30
cout << p2->y < p.y;
y 50
//Compare member wise

cout << *(p2) == p;


//Compile time error Operation not defined
delete p2; //Deallocate memory
06/09/2021} 13
Object Arithmetic Operators ( +, -, /, *, %)
• Operations depends on data members built in data type operation.
• Only if member access specifier is public.
void main(){
Point p; p.x = 100; p.y = 50; class Point
{
X 100
Point * p2 = new Point; public:
p2->x = 30; p2->y = 50; y 50
int x;
p.x = p2->x + 100; int y;
p2->y = p2->y + p.y; };
X 30
//member wise
y 50
p = *(p2) + p;
//Compile time error Operation not defined
delete p2; //Deallocate memory
}
06/09/2021 14
Objects and functions (Pass by value)
• Functions can access class members only if member access specifier is public.

void main(){ 100


X
Point p1; p1.x = 100; p1.y = 50;
y 50 class Point
Point p2; p2.x = 10; p2.y = -30;
{
cout << equal (p1, p2); 10
X public:
} y -30 int x;
//objects pass by value or copy int y;
bool equal(Point p, Point q){
};
if((p.x == q.x)&&(p.y == q.y))
return true;
else
return false;
}

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

You might also like