Classes and Objects in C++
Classes are logical abstractions while Objects are instances of the classes that physical existence in the memory.
Syntax for class declaration –
class <classname>{
private date & functions
access specifier (private, public, protected - could be switched back from one to another)
data & function
....
access specifier
data & functions
}
We could use any number of access specifiers i.e, public, private, protected. But usually it is visually ordered if they are all grouped under only 3 headings.
A class is instantiated as a object as shown below
<ClassName> objectname = <ClassName>(); or simply
<ClassName> objectname;
Classes and Objects are the basic concepts in C++ which supports the concept of encapsulation. A class is similar to a structure in C. A simple class – ‘stack’ – representing the general class structure is shown below –
class stack{
int stk[MAXSIZE];
int stktop;
public:
void init();
void push(int i);
int pop();
} stack1, stack2;
i.e,
class <class_name>{
private variables & functions
public:
public variables & functions
}[<objectname1>,<objectname2> .. ]
The class has two parts – private & public. Private variables & functions could only be accessed by the functions in private / public of a particular class. If any outside code needs to access the private parts of a class, they could do so only via the public functions of that class. Now, to access the public part (variables/functions) of a class, we must use the variable / function (member of class) in conjunction with the object name as <objectname>.class_member.
The class actually creates a datatype as ‘stack’ which could be used to create objects which are various instances of that datatype and the class. Thus class is a logical abstraction while object is real and exists inside the memory of the computer.
Now, to define a member function of a class, use –
void <classname>::<functionname>{
function body;
}
It is usual in C++ to code the class and class member functions before the main().
Below is a simple implementation of stack and its operation in C++ –
//Simple stack program in C++
#include <iostream>
using namespace std;
# define MAXSIZE 100
class stack{
int stk[MAXSIZE];
int stktop;
public:
void init();
void push(int i);
int pop();
}stack1,stack2;//could also be defined in main
void stack::init()
{
stktop = 0;
}
void stack::push(int i){
if(stktop == MAXSIZE){
cout << "\nStack overflow";
return;
}
stk[stktop] = i;
stktop++;
}
int stack::pop(){
if(stktop == 0){
cout << "\nStack underflow";
return 0;
}
stktop--;
return stk[stktop];
}
main(){
stack1.init();
stack2.init();
stack1.push(3);
stack2.push(4);
stack1.push(5);
stack2.push(6);
cout << stack1.pop() << "\n";
cout << stack2.pop() << "\n";
cout << stack1.pop() << "\n";
cout << stack2.pop() << "\n";
return 0;
}
Output:
5
6
3
4
Nested Classes
Nested Classes are also possible in ++. But with the powerful tool of inheritance, nested classes are seldom used.
Local Classes
Sometime we use Local Classes which are classes defined within a function. A simple implementation is shown below –
#include <iostream>
using namespace std;
void f();
main(){
f();
return 0;
}
void f(){
class myclass{
int i;
public:
void set_i(int n){ i = n; }
int get_i(){cout << i; }
}ob;
ob.set_i(10);
ob.get_i();
}
Output:
10
Local class may not access local variables of that function. It could access static local variables. It could not declare static variables inside a local class. Local classes are not common in C++.
Passing Object to functions
When objects are passed to functions, a copy of the object is generated and the objects constructor function is not called, and when the copy of the object is destroyed, the destructor function is called. Take a look at a simple C++ program below that passes object to a function –
#include <iostream>
using namespace std;
class myclass {
int i;
public:
myclass(int n);
~myclass();
void set_i(int n){ i = n; }
int get_i() {return i; }
};
myclass::myclass(int n){
i = n;
cout << "Constructing " << i << "\n";
}
myclass::~myclass(){
cout << "Destructing " << i << "\n";
}
void fclass(myclass ob);
main(){
myclass ob(1);//constructor function will be called only by this inst. obj
fclass(ob); // passes object ob as arguement
cout << "This is i in main:";
cout << ob.get_i() << "\n";
return 0;
}
void fclass(myclass ob){
ob.set_i(2);
cout << "This is local i:";
cout << ob.get_i() << "\n";
}
Output:
Constructing 1
This is local i:2
Destructing 2
This is i in main:1
Destructing 1
Returning Objects
Below C++ program implements a simple scenario that shows how an object can be returned from a function –
myclass freturnobj(); // return type of type myclass
main(){
myclass ob;
ob = freturnobj(o);
cout << ob.get_i() << "\n";
return 0;
}
Output:
99