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

Module 2 - 1 - Classes and Objects

Uploaded by

kalyani777suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Module 2 - 1 - Classes and Objects

Uploaded by

kalyani777suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Classes & Objects

C Structure revisited
Structure – Method of packing together data of different types
a group of logically related data items
a user-defined data type with a template that serves to define its data properties
can create variables of that type using declarations similar to built in types.
struct student
{
char name[20]; /*the keyword struct declares student as a new data type that holds three
int roll_number; fields. These elements inside the structure body are called the structure
char prog[20]; elements or members. The identifier student is called structure name and
}; is used to create variables of type student*/

struct student std; // std is a variable of type student


std.roll_number=101; // member variables can be accessed using dot operator
Limitations of C structure
• It does not allow the struct data type to be treated like built in types
Ie.,
struct complex
{
float x;
float y;
}c1,c2,c3;

We cannot add two complex numbers like


c1=c2+c3

• They do not allow data hiding. Structure members can be directly accessed by
structure variables by any function anywhere in their scope.
Extensions to structures in C++
• It brings user defined types as close as possible to built in data types
• It provides facility to hide the data.
• Inheritance is also supported

• In C++a structure can have both variables and functions as members


• It can also declare some of its member as ‘private’, so that they cannot be accessed direcctly by
the external dunctions
• In C++ structure names are stand alone and can be used like any other type names. Ie. the
keyword struct can be omitted in the declaration of structure variables

student std;

C++ incorporates all these extensions in another user defined type known as class
Ie., there is only slight difference between structure and class in C++
• The only difference is that by default, the members of a class are private and in a structure it is
public
Specifying a class
• Class is a way to bind data and functions together.
• It allows data and functions to be hidden
• Class specification has two parts: -
1. Class declaration
2. Class function definition

General form of Class declaration


class class_name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
};
The body of the declaration can contain members that can be either data or function declaration, and
optionally access specifier.
• The variable declared inside the class is known as data member and function are known as
member functions.
• Access specifier are keyword in object oriented language that set the accessibility of classes,
method and other member.
• Access specifier is one of the following keyword: public, private, protected.
Access Specifiers
These specifier modify the access rights that the member
following them acquire:
 private members of class are accessible only from within
other member of same class or from their friends.
 protected members are accessible form members of their
same class and from their friends but also from members of
their derived classes.
 public members are accessible from anywhere the object is
visible.
• By default, all members of class declared with the class
keyword have private access for all its member. Therefore,
any member that is declared before one other class specifier
automatically has private access.
• Only member function can have access to private data
member and private function of that data.
Class Example
Class item
{
int number;
float cost;
public:
int qty;
void getdata(int a, float b);
{
}
void putdata()
{
}
};
Objects
• Once a class has been created, we can create variable of that type(class type) by using
following syntax which is called object.
Object - Object is an instance of a class.
• Syntax:

class_name variable_name;

Example:
student s;

• we can create any number of objects belonging to that class by declaring more than one
object in one statement.
This statement are written in main().
• The objects can also be defined by placing their name immediately after the closing brace of
the class.
Accessing class members
The private data of a class can be accessed only through the member functions of that class

Syntax: -
Objectname.functionname(actual arguments)

Eg. x.getdata(10,20);
But if the data is declared public

x.qty=10; is possible
Thank You

You might also like