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

2_Class Objects_access Specifier_ C++ (1)

The document provides an overview of classes in C++, including syntax for defining classes, accessing public and private members, and creating objects. It explains the use of inline functions, the scope resolution operator, and provides examples of accessing and manipulating class members. Additionally, it includes practice exercises for creating classes and implementing member functions.
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)
12 views

2_Class Objects_access Specifier_ C++ (1)

The document provides an overview of classes in C++, including syntax for defining classes, accessing public and private members, and creating objects. It explains the use of inline functions, the scope resolution operator, and provides examples of accessing and manipulating class members. Additionally, it includes practice exercises for creating classes and implementing member functions.
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/ 37

Class in C++

Defining Class
Syntax
/* class with only data members */
class Box
{
public: // Access specifiers
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
Class with data members and member functions
class Box cout<< "Enter height”;
{ // begin of class cin>>height;
public: // Acess specifiers }
double length; // Length of a box
double breadth; // Breadth of a box public:
double height; // Height of a box
public: double volume()
{
void getdata() return length*breadth*height;
{ }
cout<<"Enter length"; }; // end of class definition
cin>> length;
cout<<"Enter breadth";
cin>>breadth;
Declaring Objects
Syntax:

ClassName ObjectName;

Example:
Class Box
{ b1
Data members…
Member functions…
};
Box b1; // object creation/ Instantiation
NOTE:
By default all members of a class are “Private”
Comment on the accessibility of the
members of the class “ sample”

#include<iostream>
using namespace std;
class sample
{
int i,j;
int k;
void fun1();
};
Comment on the accessibility of the
members of the class “ sample”
#include<iostream>
using namespace std;
class sample
{
private:
int i,j;
int k;
void fun1();
Public:
int p;
void fun2();
};
#include<iostream>
using namespace std; Identify the private and
class sample
{
public members of the class
int i,j; sample
int k;
void fun1();
public:
int a;
int b;
private:
void fun2()
int g;
public:
int d;
private:
float s,f;
void fun3(); }; // end of class
Accessing members of a class

The data members and member functions


of class can be accessed using the dot(‘.’)
operator with the object.

Dot operator--- Member selection


operator
Accessing Public members
All the class members declared under public will be available
to everyone. The public members of a class can be accessed
from anywhere in the program using the direct member
access operator (.) with the object of that class.

Syntax:
Objectname.membername
Accessing Public members: example 1
#include <iostream> b1.height = 4.0; // accessing public
using namespace std; members
b1.length = 6.0;
class Box b1.breadth = 3.0;
{
public: // volume of b1
double length; // Length of a box
double breadth; // Breadth of a box volume = b1.height * b1.length *
double height; // Height of a box b1.breadth;
};
cout << "Volume of Box1 : " << volume
int main( ) <<endl;
{ return 0;
Box b1; // Declare b1 of type Box } b1
double volume = 0.0; // Store the
volume of a box here double length=4.0
double breadth=6.0
double height=3.0
// box 1 specification
Accessing Public members : example 2
#include <iostream> b1.breadth = 3.0;
using namespace std; // box 2 specification

class Box b2.height = 10.0;


{ b2.length = 12.0;
public: b2.breadth = 12.0;
// volume of b1
double length; // Length of a box volume = b1.height * b1.length *
double breadth; // Breadth of a box b1.breadth;
double height; // Height of a box cout << "Volume of Box1 : " << volume
}; <<endl;
volume = b2.height * b2.length *
int main( ) b2.breadth;
{ cout << "Volume of Box2 : " << volume
Box b1; // Declare b1 of type Box <<endl;
Box b2; // Declare b2 of type Box
double volume = 0.0; // Store the volume of a box return 0;
here
// box 1 specification }
b1.height = 4.0;
b1.length = 6.0;
Class Box Box b1; // Declare b1 of type Box
double length Box b2; // Declare b2 of type Box
double breadth // box 1 specification

double height b1.height = 4.0;


b1.length = 6.0;
b1.breadth = 3.0;
// box 2 specification

b1 b2.height = 10.0;
b2
length=4.0 length=10.0
b2.length = 12.0;
breadth=6.0 breadth=12.0 b2.breadth = 12.0;
height=3.0 height=12.0
Accessing Public members : example 3
#include <iostream> public:
using namespace std; double volume()
class Box {
{ // begin of class return length*breadth*height;
public: // Acess specifiers }
double length; // Length of a box }; // end of class definition
double breadth; // Breadth of a box
double height; // Height of a box int main()
public: {
void getdata() Box b1;
{ cout<<"Enter length"; b1.getdata();
cin>> length; double vol;
cout<<"Enter breadth"; vol=b1.volume();
cin>>breadth; cout<<"Volume of Box 1 is
cout<< "Enter height"; "<<vol<<"\n";
cin>>height ; }
}
OUTPUT
Accessing Private members

Private:

The class members declared as private can be accessed only by the


functions inside the class.

They are not allowed to be accessed directly by any object or


function outside the class.

Only the member functions or the friend functions are allowed to


access the private data members of a class.
Accessing Private members
#include <iostream> b1.height = 4.0;
using namespace std; b1.length = 6.0;
b1.breadth = 3.0;
class Box
{ // volume of b1
private:
double length; // Length of a box volume = b1.height * b1.length *
double breadth; // Breadth of a box b1.breadth;
double height; // Height of a box
}; cout << "Volume of Box1 : " << volume
<<endl;
int main( ) return 0;
{ }
Box b1; // Declare b1 of type Box
double volume = 0.0; // Store the
volume of a box here

// box 1 specification
Accessing Private members: example
#include <iostream> }
using namespace std; public:
class Box { void details()
private: {
double length; double vol;
double breadth; vol=volume();
double height; cout<<"box details
public: are:"<<endl<<"length:"<<length<<"
void getdata() breadth:"<<breadth<<"
{ height"<<height<<" volume:"
cout<<"Enter length"; <<vol<<"\n";
cin>> length; }
cout<<"Enter breadth"; }; // end of class definition
cin>>breadth; int main() {
cout<< "Enter height"; Box b1;
cin>>height ; b1.getdata();
} b1.details();
private: }
double volume()
{
OUTPUT
Public Member Functions in Classes:
There are 2 ways to define a public member function:

1)Inside class definition

2)Outside class definition

To define a member function outside the class


definition we have to use the scope resolution ::
operator along with class name and function name.
Uses of scope resolution operator (::)

1) To define a function outside a class.

2) To access a global variable when there is a


local variable with same name:
/ C++ program to show that scope resolution operator :: is used
// to define a function outside a class
#include<iostream>
using namespace std;

class ABC
{
public:

// Only declaration
void fun();
};

// Definition outside class using ::


void ABC::fun()
{
cout << "fun called";
}

int main()
{
ABC a;
a.fun();
return 0;
}
// C++ program to show that we can access a global variable
// using scope resolution operator :: when there is a local
// variable with same name
#include<iostream>
using namespace std;

int x; // Global x

int main()
{
int x = 10; // Local x
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0;
}
Output:
Value of global x is 0
Value of local x is 10
Inline functions in C++
•Inline function is a function that is expanded in line when it
is called.

•When the inline function is called whole code of the inline


function gets inserted or substituted at the point of inline
function call.

•This substitution is performed by the C++ compiler at


compile time.

•Inline function may increase efficiency if it is small.

•C++ provides an inline functions to reduce the function call


overhead.
Inline functions in C++

The syntax for inline function:

inline return-type function-name(parameters)


{
// function code
}
Inline functions in C++
Working of inline functions in C++
Inline functions in C++
Inlining is only a request to the compiler, not a
command.
Compiler can ignore the request for inlining.

Compiler may not perform inlining in such circumstances


like:
1) If a function contains a loop. (for, while, do-while)
2) If a function contains static variables.
3) If a function is recursive.
4) If a function return type is other than void, and the
return statement doesn’t exist in function body.
5) If a function contains switch or goto statement.
Advantages of Inline functions
1) Function call overhead doesn’t occur.
2) It also saves the overhead of push/pop
variables on the stack when function is
called.
3) It also saves overhead of a return call
from a function.
Implicit and explicit inline functions in c++

Implicit inline functions:

The inline member function is defined within the


class definition.

Explicit inline functions:

The member function defintion is defined outside


the class definition, and is also preceded by the
keyword inline.
Implicit inline Member Functions
#include<iostream> public:
using namespace std; inline int add ( )
class sample {
{ return i+j;
public: }
int i,j; }; // end of class defintion
void get()
{ int main()
cin>>i; {
cin>>j; sample s;
} s.get();
cout<<s.add();
}
Explicit inline Member Functions
#include<iostream> inline int sample :: add ( )
using namespace std; {
class sample return i+j;
{ }
public:
int i,j; int main()
void get() {
{ sample s;
cin>>i; s.get();
cin>>j; cout<<s.add();
} }
public:
inline int add ( );
}; // end of class
Discussion and Activities

Discussion and Activities.ppt


sample programs- Practice
1)Create a class point with following members:
Data members:
public: x and y coordinate
Member functions:
Public- getdata() to get the x, y coordinate details
public – checkquadrant () to find the quadrant in which
the given coordinate lie.
Write a c++ program that reads x, y coordinate value
from the user and display the quadrant of the given point
sample programs- Practice
2)Create a class complex with following members:
Data members:
public: real_part and imag _part
Member functions:
Public- getdata() to get real part and imaginary part
public – add () to add two complex numbers.
public – sub () to subtract two complex numbers.
public – mul () to multiply two complex numbers.

Write a c++ program to add , subtract, multiply two


complex numbers . It is a menu driven program in
which a user will have to enter his/her choice to
perform an operation.

You might also like