2_Class Objects_access Specifier_ C++ (1)
2_Class Objects_access Specifier_ C++ (1)
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
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
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:
// 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:
class ABC
{
public:
// Only declaration
void fun();
};
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.