C++ Classes, Member Functions (Getters-Setters, Accessors-Mutators)
C++ Classes, Member Functions (Getters-Setters, Accessors-Mutators)
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
class Point
void main(){
Point p; ?
X class Point
y ?
cout << p.x;
{
//object variable name dot member name public:
cout << p.y; int x;
//object variable name dot member name
int y;
p.x = 100; };
cin>>p.y;
//initialize members
}
06/09/2021 4
Object Encapsulation
• Information (Data) and implementation hiding
class Point
void main(){ X ? {
Point p; y ? int x;
cout << p.x; int y;
//Compiler Error cannot access private member };
outside
cout << p.y; class Point
//Compiler Error cannot access private member {
outside private:
int x;
p.x = 100; cin>>p.y; int y;
//Compiler Error cannot access private member };
outside
} How to store and
process the data
of objects 5
06/09/2021
Class Member Functions
• Provide Information hiding and safe processing
• Can directly access all other members of class
1. Data members / no need to pass in parameters
2. Implement all member functions before use
• For all member functions only one copy is created at class level
• That copy is used by all objects.
• Member access specifier can be public or private
• Should be public to access and call them from outside.
• Should be private to make some hidden functions.
Class Member Functions (TYPES)
1. Setters
2. Getters
3. Mutators or Transformers
4. Accessors or Observers
5. Constructors
6. Destructors
7. Operators
8. Iterators
C++ Class
class class-name
{
//declaration statements here
//data members defined only not initialized
//add member functions prototype or complete implementation
};
06/09/2021 8
Member functions (Setters)
• Used to set or update values of individual data members or complete Object
class Point {
int x; void main(){
int y; Point p;
public: //Objects created but not initialized
void setX(int xi){ x = xi;}
void setY(int yi){ y = yi;} p.setX(5); X ?
void setPoint(int xi, int yi){ p.setY(5); y ?
x = xi; p.setPoint(5,8);
y = yi;
} Point p2;
void setPoint(Point p){ p2.setPoint(p);
x = p.x;
y = p.y; }
}
//add member functions complete
implementation
06/09/2021}; 9
Member functions (Getters)
• Used to fetch values of individual data members or complete Object
class Point {
int x; void main(){
int y; Point p;
public:
//setters p.setPoint(5,8);
X ?
void setX(int xi){ x = xi;}
cout << p.getX(); y ?
void setY(int yi){ y = yi;}
//getters cout << p.getY();
int getX(){ return x;}
int getY(){ return Y;} int a, b;
void getPoint(int & xi, int & yi){ cout << p. getPoint(a, b);
xi = x; cout << a << b;
yi = y;
} }
//add member functions complete implementation
};
06/09/2021 10
Member functions (Setters and this
pointer)
• Used to set or update values of individual data members or complete Object
class Point
void main(){
{
Point p;
int x;
int y;
p.setX(5); X ?
public:
p.setY(5); y ?
void setX(int x){ this->x = x;}
p.setPoint(5,8);
void setY(int y){ this->y = y;}
}
void setPoint(int x, int y){
this->x = x; this is a pointer
this->y = y; • Single copy of this pointer is maintained
}
void setPoint(Point p){
at class level accessible in member
x = p.x; functions only.
y = p.y; 1. Used when to keep same member
} functions parameters name as data
//add member functions complete members.
06/09/2021 implementation 11
};
Member functions (Getters and this
pointer)
• Used to fetch values of individual data members or complete Object
class Point void main(){
{ Point p; X 5
int x; p.setPoint(5,8); y 8
int y;
public: cout << p.getX();
//setters cout << p.getY();
void setX(int xi){ x = xi;} Point p2 = p.getPoint();
void setY(int yi){ y = yi;} }
//getters this is a pointer
int getX(){ return x;} • Single copy of this pointer is maintained
int getY(){ return Y;}
Point getPoint(){return *this;}
at class level accessible in member
//add member functions complete implementation functions only.
}; 2. Used when to return complete
object from member functions.
06/09/2021 12
Member functions (Setters)
• Implement outside the class
//implement outside class definition
class Point void setX(int x){ this->x = x;}
{ //Compiler Error undefined variable this
int x;
int y; void setY(int y){ this->y = y;}
public: //Compiler Error undefined variable this
int setX();
int setY(); void setPoint(int x, int y){
void setPoint(int x, int y); this->x = x;
void setPoint(Point p); this->y = y;
//add member functions prototype }
//Compiler Error undefined variable this
}; void setPoint(Point p){
x = p.x;
y = p.y;
}
06/09/2021 //How to resolve membership issue?
13
Member functions (Getters)
• Implement outside the class
class Point //implement outside class definition
{ int getX(){ return x;}
int x; //Compiler Error undefined variable x
int y;
public: int getY(){ return Y;}
int getX(); //Compiler Error undefined variable Y
int getY();
Point getPoint(); Point getPoint(){return *this;}
//add member functions prototype //Compiler Error undefined variable this
06/09/2021 14
Class Members Scope resolution
• Use Scope resolution operator (::)
Class name :: class member name
class Point //implement outside class definition
{ void Point:: setX(int x){ this->x = x;}
int x;
int y; void Point:: setY(int y){ this->y = y;}
public:
int setX(); void Point:: setPoint(int x, int y){
int setY(); this->x = x;
void setPoint(int x, int y); this->y = y;
void setPoint(Point p); }
06/09/2021 15
Class Members Scope resolution
• Use Scope resolution operator (::)
Class name :: class member name
class Point //implement outside class definition
{ int Point::getX(){ return x;}
int x;
int y; int Point::getY(){ return y;}
public:
int getX(); Point Point::getPoint(){
int getY(); return *this;
Point getPoint(); }
//add member functions prototype
};
06/09/2021 16
Member functions (Mutators)
• Mutator functions can
• read data members
• write into class data members
• Example:
• Setters are also Mutators
06/09/2021 17
Member functions (Accessors)
• Accessor functions can only
• read data members
• Cannot write into class data members
• Constant functions are used to make accessors
returntype functionName (parameters list) const;
• Example:
• Getters should be accessors as they only read data
06/09/2021 18
Member functions (Mutators and
Accessors)
class Point
{
int x;
int y;
public:
//getters
int getX() const { return x;} //Accessor
int getY() const { return Y;} //Accessor
Point getPoint() const {return *this;} //Accessor
//setters
void setX(int x){ this->x = x;} //Mutator
void setY(int y){ this->y = y;} //Mutator
void setPoint(int x, int y){ this->x = x; this->y = y;} //Mutator
void setPoint(Point p){//Mutator
this->x = p.x;
this->y = p.y;
}
//add member functions complete implementation inside class definition
};
06/09/2021 19
Member functions (Setters and Getters)
class Point
{
int x;
int y;
public:
//getters
int getX() const; //Accessor
int getY() const; //Accessor
Point getPoint() const; //Accessor
//setters
void setX(int x); //Mutator
void setY(int y); //Mutator
void setPoint(int x, int y); //Mutator
void setPoint(Point p); //Mutator
//add member functions Prototype inside class definition
};
06/09/2021 20
Member functions (Mutators and
Accessors)
//implement outside class definition int Point::getX() const { return x;}
void Point::setX(int x){
this->x = x; int Point::getY() const { return y;}
}
Point Point::getPoint() const
void Point::setY(int y){
this->y = y; {
} return *this;
}
void Point::setPoint(int x, int y){
this->x = x;
this->y = y; void main(){
} Point p; X 5
p.setPoint(5,8); y 8
void Point::setPoint(Point p){ cout << p.getX();
x = p.x; cout << p.getY();
y = p.y;
} }
06/09/2021 21
Member functions (Other functions)
void Point::printPoint() const {
class Point {
cout<< " X: " << this->x;
int x;
cout<< " Y: " << this->y;
int y;
}
public:
//getters
void main(){
int getX(); X 5
int getY(); y
Point p; 8
Point getPoint();
p.setPoint(5,8);
//setters
p.printPoint();
void setX(int x);
X 100
void setY(int y);
Point p2; y 85
void setPoint(int x, int y);
p2.setPoint(100,85);
void setPoint(Point p);
p2.printPoint();
//Other functions
void printPoint() const;
}
//Accessor
};
06/09/2021 22
Member functions (Other functions)
class Point {
int x;
int y;
float calculateDistance(Point &p) const;
public:
//getters
int getX();
int getY();
Point getPoint();
//setters
void setX(int x);
void setY(int y);
void setPoint(int x, int y);
void setPoint(Point p);
//Other functions //Accessors
void printPoint() const;
Point find_closest (Point &p1, Point &p2) const;
};
06/09/2021 23
Member functions (Other functions)
X 5
float Point::calculateDistance(Point &p) void main(){
const y 8
Point p; p.setPoint(5,8);
{ p.printPoint();
int d1 = p.x - x;
int d2 = p.y - y; Point p2; X 100
float temp = ( (d1*d1) + (d2*d2) ); p2.setPoint(100,85); y 85
return sqrt(temp); p2.printPoint();
}
Point p3;
X 10
Point Point::(Point &p1, Point &p2)const p3.setPoint(10,89);
{ y 89
p3.printPoint();
float d1 = Calculate_Distance(p1);
float d2 = Calculate_Distance(p2); cout<< p.calculateDistance(p2);
if(d1<=d2)
return p1; p.calculateDistance(p2,p3).printPoint();
else }
return p2;
}
06/09/2021 24