class and objects n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Class and Objects PowerPoint Presentation
Download Presentation
Class and Objects

Loading in 2 Seconds...

play fullscreen
1 / 17

Class and Objects - PowerPoint PPT Presentation


  • 181 Views
  • Uploaded on

Class and Objects. Topics to be Discussed…. Class Classes in C++ Class Example Object of a Class Accessing Class Members Defining Member Functions Memory Allocation for Objects. Class. A Class is a user defined datatype which holds both data members and member functions.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Class and Objects' - hope


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
topics to be discussed
Topics to be Discussed…
  • Class
  • Classes in C++
  • Class Example
  • Object of a Class
  • Accessing Class Members
  • Defining Member Functions
  • Memory Allocation for Objects
class
Class
  • A Class is a user defined datatype which holds both data members and member functions.
  • Data members are data items related to Class and Member Functions are functions related to class which indicate different operations that can be performed on data members.

BACK

classes in c
Classes in C++
  • A class definition begins with the keyword class.
  • The body of the class is contained within a set of braces, { } ;

class class_name

{

….

….

….

};

Any valid identifier

Class body (data member + methods)

classes in c1
Classes in C++
  • 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 c2
Classes in C++

class class_name

{

private:

public:

};

private members or methods

Public members or methods

classes in c3
Classes in C++
  • Member access specifiers
    • public:
      • can be accessed inside as well as outside the class.
    • private:
      • Accessible only to member functions of class
      • Private members and methods are for internaluse only.

BACK

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

{

private:

int roll;

char name[25];

public:

void getdata();

void display();

};

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

Data members

BACK

object of a class
Object of a Class
  • Declaring a variable of a class type creates an object. You can have many variables of the same type (class).
    • Instantiation
  • Once an object of a certain class is instantiated, a new memory location is created for it to store its data members and code
  • Syntax:

classname listof objects;

student x,y;

slide10

Objects can also be created when a class is defined by placing their names immediately after the closing brace.

Class student

{

private:

int roll;

char name[25];

public:

void getdata();

void display();

}x,y;

BACK

accessing class members
Accessing Class Members
  • Any Public Member of the class can be accessed with the help of following syntax:

a) object name.membername;

b) objectname.functionname(actual arguments);

Class Abc

{

private:

int a;

int b;

public:

int c;

void sum();

};

Void main()

{

Abc x;

x.c=101; // accessing the data member

x.a=101; // error,access denied because a is a private member

x.sum(); // accessing the member function

}

BACK

defining member functions
Defining Member Functions
  • Member functions can be defined in two ways:
    • Member functions defined outside class
      • Using scope resolution operator (::)
      • “Ties” member name to class name
      • Uniquely identify functions of particular class
      • Different classes can have member functions with same name
    • Format for defining member functions

ReturnType ClassName::MemberFunctionName( )

{

}

member function define outside the class definition
Member function define outside the class definition

Class Abc

{

private:

int a;

int b;

public:

int c;

void sum();

};

Void Abc :: sum()

{

Cout<<“sum is =“<<a+b;

}

member function define inside the class definition
Member function define inside the class definition
  • Member functions defined inside the class
    • Can be defined within the class at the time of declaration
    • Without Using scope resolution operator (::)

Class Abc

{

private:

int a;

int b;

public:

int c;

void sum()

{

Cout<<“sum is =“<<a+b;

}

};

slide15

#include<iostream.h>

#include<conio.h>

Class abc

{

int a,b,c;

Public:

Void getdata(int,int);

Void putdata();

Void sum();

};

Void abc::getdata(int I,int j)

{

a=i;

b=j;

}

Void abc::putdata()

{

Cout<<“sum is =“<<c;

}

Void abc::sum()

{

C=a+b;

}};

Void main()

{

Abc x;

x.getdata(1,2);

x.sum();

x.putdata();

}

BACK

memory allocation for objects
Memory Allocation for Objects
  • Memory is allocated to each of the object for data members.data members of each object are stored seperately but member functions are stored only once.
  • Space is allocated to member functions when class is declared.
  • Space is allocated for data members when objects are created.

BACK