Notes OOPS-1
Notes OOPS-1
Topic :ObjectOrientedProgramming-1
Data members are the data-type properties that describe the characteristics of
the class.
Member functions are the set of operations that may be applied to objects
of that class, i.e., they represent the behavioral aspect of the object. They are
usually referred as class interface.
Example code :
class Student {
public :
int rollno;
char name[20];
};
int main( ){
Dynamically
Example code :
class Student {
public :
int rollno;
char name[20];
};
int main( ){
Access Modifiers
The class body contains the declaration of its members (data and functions).
They are generally two types of members in a class: private and public,
which are correspondingly grouped under two sections namely private: and
public: .
The private members can be accessed only from within the class. These
members are hidden from the outside world. Hence they can be used only
by member functions of the class in which it is declared.
The public members can be accessed outside the class also. These can be
directly accessed by any function, whether member function of the class or
non-member function. These are basically the public interface of the class.
By default, members of a class are private if no access specifier is provided.
Example code
: class A{
int x, y;
int sqr(){
return x * x;
}
public :
int z;
int twice(){
return 2 * y;
}
The private members of the class are not accessible outside the class,
although sometimes there is a necessity to provide access even to private
members, in these cases we need to create functions called getters and
setters. Getters are those functions that allow us to access data members of
the object. However, these functions do not change the value of data
members. These are also called accessor function.
Setters are the member functions that allow us to change the data members of
an object. These are also called mutator function.
Example code :
class Student
{
int rollno;
char name[20];
float marks;
char grade;
public :
int getRollno( ){
return rollno;
}
int getMarks( ){
return marks;
}
void setGrade( ){
We can also define member functions outside the class using scope
resolution operator :: .
For example lets move the definition of the two functions defined in student
class above outside the class.
class Student {
int rollno;
char name[20];
float marks;
char grade;
public :
int getRollno();
int getMarks( );
void setGrade( ){
int
Student::getMarks(){
return marks;
}
int
Student::getRollNo(){
return rollNo;
}
Constructors
A constructor in a class is means of initializing or creating objects of a class.
A constructor allocates memory to the data members when an object is
created. It may also initialize the object with legal initial value.
A constructor has following characteristics:
Constructor is a member function of a class and has same name as that
of the class.
Constructor functions are invoked automatically when the objects
are created.
Constructor functions obey the usual access rules. That is, private
constructors are available only for member functions, however,
public constructors are available for all functions
Constructor has no return type, not even void.
Default constructor
Example code
: class
Sum {
int a, b;
public:
int getSum(){
return a + b;
}
};
int main() {
Example code
: class
Sum {
int a, b;
public:
Sum( ) { // user-defined default
constructor cout << "constructor invoked";
a = 10;
b = 20;
}
int getSum(){
return a + b;
}
};
int main() {
constructor invoked
In this case, user-defined default constructor will be invoked when an object obj
of person class is created and the data members of that object, a and
b, are initialized to default values 10 and 20.
Parameterized constructor
The constructors that can take arguments are called parameterized constructor.
Example code :
class Sum
{ int a,
b;
public
:
Sum(int num1, int num2 ) { // parameterized
constructor a = num1;
b = num2;
}
int getSum(){
return a + b;
}
};
int main() {
Destructors
Just as the objects are created, so are they destroyed. If a class can have
constructor to set things up, it should have a destructor to destruct the objects.
A destructor as the name itself suggests, is used to destroy the objects that
have been created by a constructor. A destructor is also a member function
whose name is the same as the class name but preceded by tilde ('~'). For
instance, destructor for class Sum will be ~Sum( ).
A destructor takes no arguments, and no return types can be specified for it,
not even void. It is automatically called by the compiler when an object is
destroyed. A destructor frees up the memory area of the object that is no
longer accessible.
Example code:
class Sum
{ int a,
b;
public
:
Sum(int num1, int num2 ) { // parameterized
constructor cout << "Constructor at work" << endl;
a=
num1;
b=
num2;
}
~Sum( ){ //destructor
cout<< "Destructor at work" << endl;
}
int getSum(){
return a + b;
}
}
int main() {
NOTE:
If we fail to define a destructor for a class, the compiler
automatically generates one for static allocations.
Destructors are invoked in the reverse order in which the constructors
were called.
Only the function having access to the constructor and destructor of a
class, can define objects of this class types otherwise compiler reports
an error.
this keyword
C++ uses a unique keyword called this to represent an object that invokes
a member function. this is a pointer that points to the object for which this
function was called. For example, the function call for obj.getSum()
will set the
pointer this to the address of the object obj. This unique pointer is
automatically passed to a member function when it is called. The pointer this
acts as an implicit argument to all the member functions.
Example
code:
classSum
{
int a, b;
public:
Sum(int a, int b ) {
this->a =
a; this-
>b = b;
}
int getSum(){
return a + b;
}
}
In the constructor of the Sum class, since the data members and data
members have the same name, this keyword is used to differentiate
between the two. Here, this->a refers to the data member a of the object
obj.