Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Classes

User defined data types


Data types are means to identify the type of data and associated operations of handling it. There are
three types of data types.
1. Pre-defined data types
2. Derived data types
3. User defined data types

The data types that are defined by the user are called derived data types of user defined data types.
These types include: Class, Structure, Union and Enumeration.
Class is the building block of C++ that leads to Object Oriented programming. It is a user defined data
type, which holds its own data members and member functions, which can be accessed and used by
creating an instance of that class. A class is like a blue print for an object.
Class is a data type defined by the programmer, consisting of variables and functions. Here is the general
format of a class declaration:

class ClassName
{
private:
// Declarations of private
// members appear here.
public:
// Declarations of public
// members appear here.
};

A Simple Class
Our first program contains a class and object of that class. Although it’s simple, the program
demonstrates the syntax and general features of classes in C++.
The class firstclass defined in this program contains one data item and one-member function. The
member function provides the only access to the data item from outside the class. The function displays
the data of the class. Placing data and functions together into a single entity is a central idea in object-
oriented programming.

The definition starts with the keyword class, followed by the class name— firstclass in this example. The
body of the class is delimited by braces and terminated by a semicolon.

private and public

The body of the class contains two unfamiliar keywords: private and public. What is their purpose? A key
feature of object-oriented programming is data hiding.

It means that data is hidden within a class so that it cannot be accessed mistakenly by functions outside
the class. The primary mechanism for hiding data is to put it in a class and make it private. Private data
or functions can only be accessed from within the class. Public data or functions, on the other hand, are
accessible from outside the class.

Don’t confuse data hiding with the security techniques used to protect computer databases. To provide
a security measure you might, for example, require a user to supply a password before granting access
to a database. The password is meant to keep unauthorized or malevolent users from altering (or often
even reading) the data. Data hiding, on the other hand, means hiding data from parts of the program
that don’t need to access it. More specifically, one class’s data is hidden from other classes. Data hiding
is designed to protect well-intentioned programmers from honest mistakes. Programmers who really
want to can figure out a way to access private data, but they will find it hard to do so by accident.

The data items within a class are called data members (or sometimes member data). There can be any
number of data members in a class. int x; in the above example is data member of the class. Member
functions are functions that are included within a class. The function bodies of these functions have
been written on the same line as the braces that delimit them.

Usually the data within a class is private and the functions are public. This is a result of the way classes
are used. The data is hidden so it will be safe from accidental manipulation, while the functions that
operate on the data are public so they can be accessed from outside the class. However, there is no rule
that says data must be private and functions public; in some circumstances you may find you’ll need to
use private functions and public data.

Defining Objects

The definition of a class does not create any objects. It only describes how they will look when they are
created. Defining an object is similar to defining a variable of any data type: Space is set aside for it in
memory. Defining objects in this way means creating them. This is also called instantiating them. The
term instantiating arises because an instance of the class is created. An object is an instance (that is, a
specific example) of a class. Objects are sometimes called instance variables. Class name followed by
object name and semicolon is the syntax.
firstclass obj;

Calling Member Functions

The next two statements in main() call the member function function1(int x):

Obj.function1(x);

This statement don’t look like normal function calls. Because object name obj is connected to the
function names with a period. This syntax is used to call a member function that is associated with a
specific object. Because function1 is is a member function of the firstclass class, it must always be called
in connection with an object of this class.

Attempting to access the class this way would be like trying to drive the blueprint of a car. Not only does
this statement not make sense, but the compiler will issue an error message if you attempt it. Member
functions of a class can be accessed only by an object of that class. To use a member function, the dot
operator (the period) connects the object name and the member function.

Scope Resolution Operator


Scope resolution operator :: in C++ is used to access a global variable when there is a local variable with
the same name and to define a function outside the class.
#include<iostream>
using namespace std;
class A{
public:
void function();
};
void A::function(){
cout<<"Function defined outside class"<<endl;
}
int main(){
A a;
a.function();
return 0;
}
Constructors and Destructors

Sometimes, it’s convenient if an object can initialize itself when it’s first created, without requiring a
separate call to a member function. Automatic initialization is carried out using a special member
function called a constructor. A constructor is a member function that is executed automatically
whenever an object is created. On the other hand destructors are used to destroy objects when they are
no longer required.

Characteristics of constructors are:


 They should be declared in public section
 The do not have any return type, even void
 They get automatically invoked when they are created
 They cannot be inherited, though the derived class can call the base class constructor
 You cannot refer to their address

Types of constructors

C++ offers four types of constructors

1. Do nothing constructor
Type of constructors which does not contain any statements, no argument and no return type.
2. Default constructor
The default constructor takes no argument but can be initialized.
Example:
#include<iostream>
using namespace std;
class calculator{
private:
int x;
public:
calculator(){
x=5;
cout<<"Data="<<x<<endl; }
};
int main(){
calculator c;
}
A default constructor is very important for initializing object members, that even if we do not
define a constructor explicitly, the compiler automatically provides a default constructor
implicitly.
3. Parametrized constructor
Parametrized constructors take parameters.
#include<iostream>
using namespace std;
class calculator{
private:
int x;
public:
calculator(int y){
x=y;
cout<<"Data="<<x<<endl;
}
};
int main(){
calculator c(5);
}
4. Copy constructor
A special type of constructor which takes an object as an argument and is used to copy values of
data members of one object in to another object.

#include<iostream>
using namespace std;
class copycon{
int x;
public:
copycon(int y){
x=y;
}
void display(){
cout<<x<<endl;
}
};
int main(){
copycon c(5);
copycon c2=c;
c.display();
c2.display();
}
Destructors

As the name implies destructors are used to destroy the objects that have been created by the
constructor within the C++ program. Destructor names are the same as class names, but they are
preceded by the tilde (~). Destructor neither takes an argument nor returns any value.

~ class destr(){

Inheritance

Inheritance is a way of defining interfaces, re-using classes and extending original functionality. It allows
a new class to inherit all the data members and member functions from a previously defined class.
Works from more general objects to more specific objects. Defines an “is-a” relationship.

Ex. Square is-a rectangle is-a shape


Square inherits from Rectangle which inherits from Shape

Derived classes inherit all data members and functions of base class. Derived classes inherit all data
members and functions of base class.

Private members of a base class cannot be accessed directly by a derived class member function. Base
class can declare variables with protected storage class, private to anyone not inheriting from the base
in which derived classes can access directly.
“This” Pointer+
The “this” pointer is used when local variable’s name is the same as member’s name.
#include<iostream>
using namespace std;
class Test{
public:
int x;
void display(){
int x=3;
cout<<this->x + x<<endl;
}
};
int main(){
Test t;
t.x=5;
t.display();
return 0;
}

You might also like