Introduction
A structurein C is a collection of similar &
dissimilar data types. C++ extends the reach of
structures by allowing the inclusion of even
functions within structures
Placing data & functions together into a single
entity is the central idea in object oriented
programming.
3.
Classes & ObjectsIn C++
There is almost no difference in the syntax of a
structure and a class, hence at least in principle they
can be used interchangeably.
But most C++ programmers use structures to
exclusively hold data and classes to hold both data &
functions.
4.
Object
An objectis an instance of a class, and the process
of creating an object is called instantiation.
rectangle r1,r2,r3 //known as instance variables.
In simplest terms a class is like a data type, where as
an object is like a variable of that data type.
5.
Specifying a Class
Generally, a class specification has two parts,
Class Declaration
Class function definitions.
class class_name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
}
6.
Defining member functions
Member functions can be defined in two places.
Outside the class definition
Inside the class definition
Outside the class definition
Return-type class-name:: function-name (argument declaration)
{
function body
}
From example
Classdeclaration. // class rectangle
Class is delimited by braces and terminated by a
semicolon.
The body of the class contains two unfamiliar
keywords:
private & public, they are used in C++ to implement a
concept called data hiding.
In the class the data items len & br follow the
keyword private, so they can be accessed from
within the class, but not from outside it.
10.
From example
Afunction declared within the definition of a class is
called a “member function” or “method”. The two
terms are interchangeable; member function is a C+
+ term & method is a general OOP’s term.
Usually the data within a class is private and the
functions are public.
11.
From example
Thefirst statement in main()
Rectangle r1,r2,r3;
Defining an object is similar to defining a variable of
any data type: space is set aside for it in memory.
To use a member function, the dot operator connects
the object name and the member function.
r1.setdata(10,20);
The dot operator is also called “class member access
operator”.
12.
Static Data Members
A static member variable has certain special characteristics. These are
It is initialized to zero when the first object of its class is created.
Only one copy of the member is created for the entire class and is shared
by all the objects of that class, no matter how many objects are created.
It is visible only within the class, but its lifetime is the entire program.
int item :: count; //definition of static data member
13.
Static Member Functions
A member function that is declared static has the following properties
A static function can have access to only other static members (functions or
variables) declared in the same class.
A static member function can be called using the class name (instead of its
objects) as follows
class name:: function-name;
14.
Arrays of Objects
We can have arrays of variables that are of the type class. Such
variables are called arrays of objects
class employee
{
char name[30];
float age;
public:
void getdata(void);
void putdata(void);
};
employee manager[3]; //array of manager
employee worker[75]; //array of workers
15.
Objects as FunctionArguments
Like any other data type, an object may be used as a function
argument.
Example
16.
Friendly functions
Privatemembers cannot be accessed from outside the class.
That is, a non-member function cannot have an access to the private
data of a class.
In such situations, C++ allows the function to be friend with the both
classes, thereby allowing the function to have access to the private
data of these classes.
17.
Friendly functions
Generalform to write friend function
Class ABC
{
…….
…….
public:
…….
…….
friend void xyz(void); //declaration
};
18.
Friendly function
Afriend function possesses certain special characteristics.
It is not in the scope of the class to which it has been declared as friend.
Since it is not in the scope of the class, it cannot be called using the object
of that class
It can be invoked like a normal function without the help of any object.
Unlike member functions, it cannot access the member names directly and
has to use an object name and dot membership operator with each member
name.
It can be declared either in the public or the private part of a class without
affecting its meaning.
Usually, it has the objects as arguments.