Professional Documents
Culture Documents
Classes and Objects-2
Classes and Objects-2
Classes and Objects-2
Introduction
Classes in C++
A class in C++ is defined using the keyword class followed by class name.
The body of the class is defined inside curly braces and terminated using a
semicolon.
Class Example
class Rectangle{
public:
int length, breadth;
int area(){
return length * breadth;
}
};
Objects in C++
2 private
3 protected
Assignment
static object
The static keyword can be used to initialize all class data member
variables to zero.
Declaring object itself as static can do this. Thus all its associated
members get initialized to zero.
Friend Function
The private members of the class are accessed only from member
functions of that class.
C++ allows a mechanism, in which a non-member function has access
permission to the private members of the class.
This can be done by declaring a non-member function friend to the class
whose private data is to be accessed.
class ac
{
private:
char name [15];
int acno;
float bal;
public:
void read();
friend void showbal();
}
Classes and Objects September 26, 2022 20 / 21
Friend Function