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

Class: A class in C++ is the building block, that leads to Object-Oriented programming.

It is a
user-defned data type, which holds its own data members and member funcions, which can
be accessed and used by creaing an instance of that class. A C++ class is like a blueprint for an
object.
A class is defned in C++ using keyword class followed by the name of class. hhe body of class is
defned inside the curly brackets and terminated by a semicolon at the end.

Declaring
Objects: When a class is defned, only the specifcaion for the object is defned; no memory or
storage is allocated. ho use the data and access funcions defned in the class, you need to
create objects.
Syntax:
ClassName ObjectName;
Accessing data members and member functins: hhe data members and member funcions of
class can be accessed using the dot(‘.’) operator with the object. For example if the name of
object is obj and you want to access the member funcion with the name printName() then you
will have to write obj.printName() .
Accessing Data Members
hhe public data members are also accessed in the same way given however the private data
members are not allowed to be accessed directly by the object. Accessing a data member
depends solely on the access control of that data member.
hhis access control is given by Access modifers in C++. hhere are three access
modifers : public, private and pritected.
// C++ program to demonstrate accessing of data members
#include <bits/stdc++.h>
using namespace std;
class Geeks
{
// Access specifer
public:

// Data Members
string geekname;
// Member Funcions()
void printname()
{
cout << "Geekname is: " << geekname;
}
};

int main() {

// Declare an object of class geeks


Geeks obj1;

// accessing data member


obj1.geekname = "Abhi";

// accessing member funcion


obj1.printname();
return 0;
}
Member Functin:

A member funcion of a class is a funcion that has its defniion or its prototype within the
class defniion like any other variable. If the member funcion is defned inside the class
defniion it can be defned directly, but if it is defned outside the class, then we have to use
the scope resoluion :: operator along with class name along with funcion name.
1) Member funcions defned within the class
Syntax

class className
{
returnhype MemberFuncion(arguments)
{
//funcion body
}
....
};
2) Member funcions defned outside the class
syntax

class className
{
returnhype MemberFuncion(arguments);
....
};

returnhype className :: funcionnName(arg1,arg2,.....argN)


{
funcion body
}

Types if Member Functins

1. Simple funcions
2. Staic funcions
3. Const funcions
4. Inline funcions
5. Friend funcions

1) Simple funcions: hhese are the basic member funcion, which doesn’t have any special
keyword like staic etc as a prefx.

returnntype funcionName(parameternlist)
{
funcion body;
}

2) Staic funcions: Staic is a keyword which can be used with data members as well as the
member funcions. A funcion is made staic by using the staic keyword with the funcion
name. hhese funcions work for the class as whole rather than for a paricular object of a
class. It can be called using the object and the direct member access operator. But, its more
typical to call a staic member funcion by itself, using class name and scope resoluion ::
operator.
Example:

class X
{
public:
staic void f(){};
};

int main()
{
X::f(); // calling member funcion directly with class name
}

3) Const funcions: A funcion becomes const when const keyword is used in funcion’s
declaraion. hhe idea of const funcions is not to allow them to modify the object on which
they are called.
class hest

{
int value;
public:
hest(int v = 0) {value = v;} // We get compiler error if we add a line like "value = 100;" in
this funcion.
int getValue() const {return value;}
};
int main()
{
hest t(20);
cout<<t.getValue();
return 0;
}
4) Inline funcions:
C++ provides inline funcions to reduce the funcion call overhead. An inline funcion is a
funcion that is expanded in line when it is called. When the inline funcion is called whole
code of the inline funcion gets inserted or subsituted at the point of inline funcion call.
hhis subsituion is performed by the C++ compiler at compile ime. Inline funcion may
increase efciency if it is small.

inline return-type funcion-name(parameters)


{
// funcion code
}

5) Friend funcion:
If a funcion is defned as a friend funcion then, the private and protected data of a class
can be accessed using the funcion.hhe compiler knows a given funcion is a friend funcion
by the use of the keyword friend. For accessing the data, the declaraion of a friend funcion
should be made inside the body of the class staring with keyword friend.

Access Modifers in C++

Access Modifers or Access Specifers in a class are used to set the accessibility of the class
members. hhat is, it sets some restricions on the class members not to get directly accessed by
the outside funcions.
hhere are 3 types of access modifers available in C++:
1. Public
2. Private
3. Pritected

I. Public: All the class members declared under public will be available to everyone. hhe
data members and member funcions declared public can be accessed by other classes
too. hhe public members of a class can be accessed from anywhere in the program using
the direct member access operator (.) with the object of that class.

II. Private: hhe class members declared as private can be accessed only by the funcions
inside the class. hhey are not allowed to be accessed directly by any object or funcion
outside the class. Only the member funcions or the friend funcions are allowed to access
the private data members of a class.
III. Pritected: Protected access modifer is similar to that of private access modifers, the
difference is that the class member declared as Protected are inaccessible outside the
class but they can be accessed by any subclass(derived class) of that class.

Array if ibject C++ Array of Objects


An object of class represents a single record in memory, if we want more than one record of
class type, we have to create an array of class or object. As we know, an array is a collecion of
similar type, therefore an array can be a collecion of class type.

Syntax fir Array if ibject

class class-name
{
datatype var1;
datatype var2;
----------
datatype varN;

method1();
method2();
----------
methodN();
};

class-name obj[ size ];

What is cinstructir?
A constructor is a member funcion of a class which iniializes objects of a class. In C++,
Constructor is automaically called when object(instance of class) create. It is special member
funcion of the class.
 Constructor has same name as the class itself
 Constructors don’t have return type
 A constructor is automaically called when an object is created.
 If we do not specify a constructor, C++ compiler generates a default constructor for us
(expects no parameters and has an empty body).

Types if Cinstructirs
1. Default Cinstructirs: Default constructor is the constructor which doesn’t take any
argument. It has no parameters.
// Cpp program to illustrate the concept of Constructors
#include <iostream>
using namespace std;

class construct {
public:
int a, b;

// Default Constructor
construct()
{
a = 10;
b = 20;
}
};

int main()
{
// Default constructor called automaically
// when the object is created
construct c;
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 1;
}
2. Parameterized Cinstructirs: It is possible to pass arguments to constructors. hypically,
these arguments help iniialize an object when it is created. ho create a parameterized
constructor, simply add parameters to it the way you would to any other funcion. When
you defne the constructor’s body, use the parameters to iniialize the object.
// CPP program to illustrate parameterized constructors
#include <iostream>
using namespace std;

class Point {
private:
int x, y;

public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}

int getX()
{
return x;
}
int getY()
{
return y;
}
};

int main()
{
// Constructor called
Point p1(10, 15);

// Access values assigned by constructor


cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();

return 0;
}
Cipy Cinstructir: A copy constructor is a member funcion which iniializes an object using
another object of the same class.
// Illustraion
#include "iostream"
using namespace std;

class point {
private:
double x, y;

public:
// Non-default Constructor & default Constructor
point (double px, double py) {
x = px, y = py;
}
};
int main(void) {
// Defne an array of size 10 & of type point
// hhis line will cause error
point a[10];

// Remove above line and program will compile without error


point b = point(5, 6);
}

Destructirs in C+

A destructor is a special member funcion that works just opposite to constructor,


unlike constructors that are used for iniializing an object, destructors destroy (or delete) the
object.

Syntax if Destructir

~classnname()
{
//Some code
}
Similar to constructor, the destructor name should exactly match with the class name. A
destructor declaraion should always begin with the ilde(~) symbol as shown in the syntax
above.

When dies the destructir get called?

A destructor is autimatcally called when:


1) hhe program fnished execuion.
2) When a scope (the { } parenthesis) containing local variable ends.
3) When you call the delete operator

Destructir Example

#include <iostream>
using namespace std;
class HelloWorld{
public:
//Constructor
HelloWorld(){
cout<<"Constructor is called"<<endl;
}
//Destructor
~HelloWorld(){
cout<<"Destructor is called"<<endl;
}
//Member funcion
void display(){
cout<<"Hello World!"<<endl;
}
};
int main(){
//Object created
HelloWorld obj;
//Member funcion called
obj.display();
return 0;
}
Output:

Constructor is called
Hello World!
Destructor is called
Destructir rules

1) Name should begin with ilde sign(~) and must match class name.
2) hhere cannot be more than one destructor in a class.
3) Unlike constructors that can have parameters, destructors do not allow any parameter.
4) hhey do not have any return type, just like constructors.
5) When you do not specify any destructor in a class, compiler generates a default destructor
and inserts it into your code.

You might also like