0% found this document useful (0 votes)
62 views

Concept of Classes and Objects

Class is a user-defined data type in C++ that encapsulates data members and member functions that can be accessed and used together. An object is an instance of a class that allocates memory dynamically. Classes support data hiding through private, public and protected access specifiers. Member functions are defined to perform operations on the data members of a class.

Uploaded by

Er Ashish Baheti
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Concept of Classes and Objects

Class is a user-defined data type in C++ that encapsulates data members and member functions that can be accessed and used together. An object is an instance of a class that allocates memory dynamically. Classes support data hiding through private, public and protected access specifiers. Member functions are defined to perform operations on the data members of a class.

Uploaded by

Er Ashish Baheti
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

CONCEPT OF CLASSES AND OBJECTS

Class

A class is an organization of data and functions which operate on them. Data structures are called data members and the functions are called member functions. The combination of data members and member functions constitute a data object or simply an object.

Object

Instantiation of a class. In terms of variables, class would be the type and an object would be a variable.

General Structure of a class


Class name or name of class Data Members Member functions Access Specifiers Declaring objects

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. Serves as a name specifier for the 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. We can say that variables in C and data members in C++. E.g. int rn;

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();

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.

Classes in C++
class class_name { private: public: };

private members or methods

Public members or methods

Private: only members of that class have accessibility can be accessed only through member functions of that class.

Private members and methods are for internal use only.

Public: Accessible from outside the class can be accessed through member function of any class in the same program.

Protected: Stage between private and public access. They cannot be accessed from outside the class, but can be accessed from the derived class.(inheritance)

Class Example

This class example shows how we can encapsulate (gather) a circle information into one package (unit or class)
class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); };
No need for others classes to access and retrieve its value directly. The class methods are responsible for that only.

They are accessible from outside the class, and they can access the member (radius)

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

Declaring objects: A class declaration only uses to build the structure of an object. Declaration of an object is same as declaration of class. Defining objects of class data type is known as class instantiation(instances of class) When we create objects during that moment , memory is allocated to them.

Ex- Circle c;

class { void { cout } void { cout } };

Customer accept() << Accepting Customer Details << endl;

display()
<< Displaying Customer Details << endl;

Void main() { Customer C1; C1.accept(); C1.display(); getch();

Program to illustrate concept of class with one object

class student { Private: Int rn; Char name[10]; Public: Void readdata() { Cout<<enter rollno; Cin>>rn; Cout<<enter name; Cin>>name; }

Void writedata() { Cout<<rn; Cout<<name; } }; Void main() { class student st; St.readdata(); St.writedatta(); }

Program to illustrate concept of class with two object

class student { Private: Int rn; Char name[10]; Public: Void readdata() { Cout<<enter rollno; Cin>>rn; Cout<<enter name; Cin>>name; }

Void writedata() { Cout<<rn; Cout<<name; } }; Void main() { class student st1,st2; St1.readdata(); St1.writedatta(); St2.readdata(); St2.writedatta();

Class sample { Private: int rollno; Public: Void readdata(int rn) { Rollno=rn; } Void display() { Cout<<rollno; }

Void main() { Class sample d1,d2; d1.readdata(121); d1.display(); d2.readdata(132); d2.display(); }

Memory management in c++

All the objects in the class use the same member function. when the members functions are defined in the class they are provided the memory, but only once. It means member functions are not duplicated whenever another objects of the class are created On the other hand, data members of the class hold different values, they are given memory every time an object is defined.

Memory management in c++

But when data member in a class is defined as static,the memory to such type of member is provided only once for all the defined objects

Difference between structure and class


1) The keyword struct is used 2) it contains heterogeneous data members 3) All the members are public by default 4) The variable is used to access the members 5) Data hidding is not available 6) structure members can be directly accessed by the structure variables

1) The keyword class is used 2) it contains heterogeneous data members and member function 3) All the members are private by default 4) An object is used to access the members 5) Data hidding is available 6) class members can not be accessed directly,these can be accessed through member function.

Defination of member function in the class

1) inside the class defination 2) outside the class defination Member function defined inside the class are by default inline function. Inline function tells the compiler to insert the code for body of the function at the place where it is calledso for overcoming this situation we declare member function outside the class

Scope Resolution Operator (::)

C++ is a block structured language where program consist of several block. a variable declared in a block can only be accessed within that block. such variables are called local variables and that are declared outside the block are known as global variables. There r many situations when the name of local and global variables are same. so if programmer wants to access global variable in block then scope resolution operator is used.

Example
#include<iostream.h> #include<conio.h> int a=40; void main() { int a=10; cout<<value of local variable is<<a<<endl; cout<<value of global variable is<<::a; getch(); }

Scope Resolution Operator (::)

Is used to define member functions outside the class definition therefore making the class definition more readable Example:
class calculator { public: void input(); };

void calculator::input () {}

void rectangle::setdata(int x,int #include<iostream.h> y) #include<conio.h> { Class rectangle a=x; { b=y; private: } int a,b; void rectangle::area() Public: { void setdata(int,int); int ar=a*b; void area(); cout<<area is<<ar; }; }

Example

void main() { rectangle r1; r1.setdata(5,10); r1.area(); getch(); }

Program to define the member function outside the class


Class account {

Void account::write() { Cout<<accno<<name<<balance;

Private:
Int accno; Char name[20]; Float balance;

}
Void main() { class account ac; ac.read(); ac.write(); }

public:
Void read(); Void write(); };

Void account::read()
{ Cout<<enter accno name and balance; Cin>>accno>>name>>balance;

You might also like