Sunteți pe pagina 1din 26

Classes and Objects

Class
• A class is a user define data type which holds both data and function.
• The data included in the class i.e the internal data is called the
internal data or data member and the functions included is called the
member function.
• These member functions can manipulate the internal data of the class
Object
• Is an instant of a class.
• In terms of variables, class would be the type and an object would be
a variable.
Classes in C++
• A class definition begins with the keyword class.
• The body of the class is contained within a set of braces, { } ; (notice
the semi-colon).

class class_name
{ Any valid identifier
….
….
…. Class body (data member +
}; methods)
class classname
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
protected:
variable declarations;
function declarations;
} obj1, obj2,…..objN;
Class name
• Name given to a particular class (any user define name). It can also be
called as tag name of the class that act as the type specifier for class
using which we can create objects.
• The class is specified by keyword “class”
Data Members
• Data type properties that describe the characteristics of a class.
• We can declare any number of data members of any type in a class.
• E.g. int x;
Member functions

• Various operations that can be performed to data members of that


class.
• We can declare any number of member functions of any type in a
class.
• E.g. void read();
Example:
Class A
{
public:
int a;
void get();
};
Creating object for a class
• After class definition (globally)
• Inside main function (locally)
Class A
{
public:
int a;
void get();
}obj; // obj is global and can be used anywhere in program
main()
{
A obj1; // obj1 is local to main function only and hence, cannot be used outside main
}
Accessing public class members and function
• With the help of objects in main or any other function(not a part of class)
• Direct inside class
Class A
{
public:
int a;
void get() { cin>>a; } // directly accessing ‘a’ as get() is also a part of the same class
}obj;
main()
{ A obj1;
obj1.get(); // calling class function – syntax – object_name.fn_name();
cin>>obj1.a; // accessing class data member outside class – syntax – object_name.var_name;
}
Methods definition
• The member function of the class can be defined in two different ways:
1) Inside the class definition:- The member functions are simple defined inside
the class only i.e the body of the function resides inside the range of class only.
2) Outside the class definition: by using scope resolution operator, which specifies
that the scope of the function is restricted to the class class_name.
Syntax:- return type class_name:: function_name()
{ //definition }
Note: when defining function outside the class, mention its prototype inside the
class
/**************************** Defining function inside class *******************************/
class abc
{ public:
int rollno;
char name[20];
void getdata() // defining function inside class
{ Cout<<“name=“;
Cin>>name;
Cout<<“rollno=“;
Cin>>rollno; }
void display() // defining function inside class
{ Cout<<“name=“<<name;
Cout<<“rollno=“<<rollno; }
};
main()
{
abc obj; // creating local object;
obj.getdata(); // calling getdata using object of the same class
obj.display();
}
/****************************** Defining function outside the class *******************************/
class abc
{ public:
int rollno;
char name[20];
void getdata(); // prototype inside class
void display();
};
void abc::getdata() // defining function outside class
{ Cout<<“name=“;
Cin>>name; // accessing ‘name’ data member directly(without any object) because
getdata also belongs to the same class
Cout<<“rollno=“;
Cin>>rollno; }
void abc::display() // defining function outside class
{ Cout<<“name=“<<name;
Cout<<“rollno=“<<rollno; }
main()
{
abc obj; // creating local object;
obj.getdata(); // calling getdata using object of the same class
obj.display();
}
Object definition using array
• Instead of declaring each object individually an array can be
implemented in declaration of objects.
• Class data
{
--------- ;
--------- ;
};
Class data obj[n];
/************************ creating object as an array *****************************/
class book
{ public:
int p;
char n [30];
void getdata()
{
Cout<<“enter name and price”;
Cin>>p>>n; }
void display()
{
Cout<<“name and price=“<<n<<p;
} };
main()
{
book obj[5];
for(i=0;i<5;i++)
{
obj[i].getdata();
}
Cout<<“enterd records are”;
for(i=0;i<50;i++)
{
obj[i].display(); } }
Access Specifiers
• Used to specify access rights for the data members and member
functions of the class.
• Depending upon the access level of a class member, access to it is
allowed or denied.
• Within the body, the keywords private: and public: specify the access
level of the members of the class.
• the default is private.

• Usually, the data members of a class are declared in the private:


section of the class and the member functions are in public: section.
C++ supports three access specifiers:
public
private
protected
The public access specifier allows a class to subject its member
variables and member functions to other functions and objects
The private access specifier allows a class to hide its member
variables and member functions from other class objects and functions
The protected access specifier allows a class to hide its member
variables and member functions from other class objects and functions
just like private access specifier - is used while implementing inheritance
Classes in C++

class class_name
{
private: private members or methods



public:
… Public members or methods


};
Class Example
• This class example shows how we can encapsulate (gather) a circle
information into one package (unit or class)

No need for others classes to access


class Circle and retrieve its value directly. The
{ class methods are responsible for
that only.
private:
double radius;
public:
void setRadius(double r); double They are accessible from outside
getDiameter(); the class, and they can access the
double getArea(); member (radius)
double getCircumference();
};
/****************** Accessing private data outside the class ************************/
#include<iostream>
using namespace std;
class A
{
int a;
void get();
};
void A::get()
{
cout<<"hi";
}
main()
{
A obj;
obj.get(); // error ( accessing private member outside the class )
cin>>obj.a; // error ( accessing private member outside the class )
}
Accessing private members outside the class
Using public function (way-1)
class A
{ int a; // private data
void get(); // private member function
public:
void fn(); // public function
};
void A::get()
{ cout<<"hi"; }
void A::fn()
{ cin>>a; //accessing private data through public function
get(); } // calling private function through public function
main()
{ A obj;
obj.fn(); } // calling public function
Static data members and static member
functions
Static data members are used to maintain a single copy throughout the
program.

For theory – please refer class notes


/****************************** program-1 : example of static data member *******************************/
class A
{ public:
int p=5;
static int q; //static variable declaration
void get()
{ p++;
q++; }
void dis()
{ cout<<p<<" "<<q<<endl; }
};
int A::q; //static variable definition(syntax: datatype class_name::variable_name;)
main()
{
A obj; // by default q is initialized to 0
obj.get();
A obj1;
obj1.get();
obj.dis();
obj1.dis();
}
Output: 6 2
6 2
/******************** program 2: static member function **************************/
class A
{ public:
static int q;
static int r;
static void get() //static member function
{
q++; //only static variables can be initialized
r++;
}
void dis()
{
cout<<q<<" “<<r;
}
};
int A::q;
main()
{ A obj;
A::get(); //syntax to call a static function without class object(class_name::fn_name())
obj.dis();
}
Output : 1 1

S-ar putea să vă placă și