SlideShare a Scribd company logo
Lecture-2Chapter-12
Abu Saleh Musa Miah
M.Sc. Engg(On going)
University of Rajshahi
Lecturer
Rajshahi Engineering Science and Technology college.
Friend class Example:
It is possible for one class to be a friend of another class. When this is the case, the
friend class and all of its member functions have access to the private members
defined within the other class.
include <iostream>
using namespace std;
class TwoValues {
int a;
Int b;
public:
TwoValues(int i, int j) { a = i; b =
j; }
friend class Min; };
class Min {
public:
int min(TwoValues x);
};
int min(TwoValues x)
{
return x.a < x.b ? x.a : x.b;
}
int main()
{
TwoValues ob(10, 20);
Min m;
cout << m.min(ob);
return 0;
}
Inline function:
There is an important feature in C++, called an inline function, that is commonly used with
classes. you can create short functions that are not actually called; rather, their code is
expanded in line at the point of each invocation. This process is similar to using a function-like
macro. To cause a function to be expanded in line rather than called,precede its definition with
the inline keyword.
inline void myclass::show()
{
cout << a << " " << b << "n";
}
int main()
{
myclass x;
x.init(10, 20);
x.show();
return 0;
#include <iostream>
using namespace std;
class myclass {
int a, b;
public:
void init(int i, int j);
void show();
};
// Create an inline function.
inline void myclass::init(int i, int j)
{
a = i;
b = j;
Parameterized Constructor:
It is possible to pass arguments to constructor functions. Typically, these arguments help initialize an
object when it is created. To create a parameterized constructor, simply add parameters to it the way
you would to any other function. When you define the constructor's body, use the parameters to
initialize the object.
int main()
{
myclass ob(3, 5);
ob.show();
return 0;
}
#include <iostream>
using namespace std;
class myclass {
int a, b;
public:
myclass(int i, int j) {a=i; b=j;}
void show() {cout << a << " " << b;}
};
Static Class
Member:
Both function and data members
of a class can be made static. This
section explains the
consequences of each.
Static data member:
When you precede a member variable's declaration with static, you are telling the compiler that only
one copy of that variable will exist and that all objects of the class will share that variable All static
variables are initialized to zero before the first object
int shared::a; // define a
void shared::show()
{
cout << "This is static a: " << a;
cout << "nThis is non-static b: " << b;
cout << "n";
}
#include <iostream>
using namespace std;
class shared {
static int a;
int b;
public:
void set(int i, int j)
{a=i;
b=j;}
void show();
} ;
Static data member:
static a: 1
non-static b: 1
static a: 2
non-static b: 2
static a: 2
non-static b: 1
int main()
{
shared x, y;
x.set(1, 1); // set a to 1
x.show();
y.set(2, 2); // change a to
2
y.show();
x.show(); /* Here, a has
been changed for both x
and y
because a is shared by
both objects. */
return 0;
}
Static member function:Member functions may also be declared as static. There are several
restrictions placed on static member functions. They may only directly refer
to other static members of the class.
int main()
{
cl ob1, ob2;
if(cl::get_resource()) cout << "ob1 has
resourcen";
if(!cl::get_resource()) cout << "ob2
denied resourcen";
ob1.free_resource();
if(ob2.get_resource())
cout << "ob2 can now use resourcen";
return 0;
}
#include <iostream>
using namespace std;
class cl {
static int resource;
public:
static int get_resourc ();
void free_resource() { resource = 0;
} };
int cl::resource; // define resource
int cl::get_resource()
{
if(resource) return 0;
else {
resource = 1;
return 1;
} }
WhenConstructoranddestructorareExecution:
As a general rule, an object's constructor is called when the object comes
into existence, and an object's destructor is called when the object is
destroyed. Precisely when these events occur is discussed here.
int main()
{
myclass local_ob1(3);
cout << "This will not be first line
displayed.n";
myclass local_ob2(4);
return 0;
}
It displays this output:
Initializing 1
Initializing 2
Initializing 3
This will not be first line displayed.
Initializing 4
#include <iostream>
using namespace std;
class myclass {
public:
int who;
myclass(int id);
~myclass();
} glob_ob1(1), glob_ob2(2);
myclass::myclass(int id)
{
cout << "Initializing " << id <<
"n";
who = id;
}
myclass::~myclass()
{
cout << "Destructing " << who
The scope resolution operator:
The :: operator links a class name with a member name in order to tell the
compiler what class the member belongs to. However, the scope resolution
operator has another related use: it can allow access to a name in an
enclosing scope that is "hidden" by a local declaration of the same name.
int main()
{
myclass local_ob1(3);
cout << "This will not be first line
displayed.n";
myclass local_ob2(4);
return 0;
}
It displays this output:
Initializing 1
Initializing 2
Initializing 3
This will not be first line displayed.
Initializing 4
#include <iostream>
using namespace std;
class myclass {
public:
int who;
myclass(int id);
~myclass();
} glob_ob1(1), glob_ob2(2);
myclass::myclass(int id)
{
cout << "Initializing " << id <<
"n";
who = id;
}
myclass::~myclass()
{
The scope resolution operator:
The :: operator links a class name with a member name
in order to tell the compiler
 what class the member belongs to.
 the scope resolution operator has another related use:
it can allow access to a name in an enclosing scope
that is "hidden" by a local declaration of the same
name.
Nested class
 It is possible to define one class within another.
 Doing so creates a nested class.
 Since a class declaration does, in fact, define a scope,
a nested class is valid only within the scope of the
enclosing class.
 Frankly, nested classes are seldom used.
Nested classMember functions may also be declared as static. There are several
restrictions placed on static member functions. They may only directly refer
to other static members of the class.
void f()
{
class myclass {
int i;
public:
void put_i(int n) { i=n; }
int get_i() { return i; }
} ob;
ob.put_i(10);
cout << ob.get_i();
}
#include <iostream>
using namespace std;
void f();
int main()
{
f();
// myclass not known here
return 0;
}
Passing object to function
Objects may be passed to functions in just the same way that any other type
of variable can. Objects are passed to functions through the use of the
standard call-by value mechanism. This means that a copy of an object is
made when it is passed to a function.
myclass::myclass(int n)
{
i = n;
cout << "Constructing " << i <<
"n";
}
myclass::~myclass()
{
cout << "Destroying " << i << "n";
}
class myclass {
int i;
public:
myclass(int n);
~myclass();
void set_i(int n) { i=n; }
int get_i() { return i; }
};

More Related Content

What's hot

Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3
Shaili Choudhary
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
Asfand Hassan
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
Tran Khoa
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
Princess Sam
 

What's hot (20)

Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
What's New In Python 2.6
What's New In Python 2.6What's New In Python 2.6
What's New In Python 2.6
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic Javascript
 
constructor and destructor
constructor and destructorconstructor and destructor
constructor and destructor
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
Unit ii
Unit   iiUnit   ii
Unit ii
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Constructor&method
Constructor&methodConstructor&method
Constructor&method
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
C questions
C questionsC questions
C questions
 

Viewers also liked

Vbmca204821311240
Vbmca204821311240Vbmca204821311240
Vbmca204821311240
Ayushi Jain
 
Gulabs Ppt On Data Warehousing And Mining
Gulabs Ppt On Data Warehousing And MiningGulabs Ppt On Data Warehousing And Mining
Gulabs Ppt On Data Warehousing And Mining
gulab sharma
 
Data warehouse and data mining
Data warehouse and data miningData warehouse and data mining
Data warehouse and data mining
Rohit Kumar
 

Viewers also liked (16)

Vbmca204821311240
Vbmca204821311240Vbmca204821311240
Vbmca204821311240
 
Lecture 5, c++(complete reference,herbet sheidt)chapter-15
Lecture 5, c++(complete reference,herbet sheidt)chapter-15Lecture 5, c++(complete reference,herbet sheidt)chapter-15
Lecture 5, c++(complete reference,herbet sheidt)chapter-15
 
Data warehousing
Data warehousingData warehousing
Data warehousing
 
Data warehousing
Data warehousingData warehousing
Data warehousing
 
Data mining: Concepts and Techniques, Chapter12 outlier Analysis
Data mining: Concepts and Techniques, Chapter12 outlier Analysis Data mining: Concepts and Techniques, Chapter12 outlier Analysis
Data mining: Concepts and Techniques, Chapter12 outlier Analysis
 
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic ConceptsData Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
Data Mining:Concepts and Techniques, Chapter 8. Classification: Basic Concepts
 
Data Mining: Classification and analysis
Data Mining: Classification and analysisData Mining: Classification and analysis
Data Mining: Classification and analysis
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Lecture 4, c++(complete reference,herbet sheidt)chapter-14Lecture 4, c++(complete reference,herbet sheidt)chapter-14
Lecture 4, c++(complete reference,herbet sheidt)chapter-14
 
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
 
C++ Complete Reference
C++ Complete ReferenceC++ Complete Reference
C++ Complete Reference
 
Gulabs Ppt On Data Warehousing And Mining
Gulabs Ppt On Data Warehousing And MiningGulabs Ppt On Data Warehousing And Mining
Gulabs Ppt On Data Warehousing And Mining
 
Data warehouse and data mining
Data warehouse and data miningData warehouse and data mining
Data warehouse and data mining
 
2.1 Data Mining-classification Basic concepts
2.1 Data Mining-classification Basic concepts2.1 Data Mining-classification Basic concepts
2.1 Data Mining-classification Basic concepts
 
Data Mining and Data Warehousing
Data Mining and Data WarehousingData Mining and Data Warehousing
Data Mining and Data Warehousing
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similar to Lecture 2, c++(complete reference,herbet sheidt)chapter-12

Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
Swarup Kumar Boro
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
Deepak Singh
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
lykado0dles
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
 

Similar to Lecture 2, c++(complete reference,herbet sheidt)chapter-12 (20)

Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
Class and object
Class and objectClass and object
Class and object
 
ccc
cccccc
ccc
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
class and objects
class and objectsclass and objects
class and objects
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Inheritance
InheritanceInheritance
Inheritance
 
7 class objects
7 class objects7 class objects
7 class objects
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 

Recently uploaded

plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
parmarsneha2
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
YibeltalNibretu
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Recently uploaded (20)

How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 

Lecture 2, c++(complete reference,herbet sheidt)chapter-12

  • 1. Lecture-2Chapter-12 Abu Saleh Musa Miah M.Sc. Engg(On going) University of Rajshahi Lecturer Rajshahi Engineering Science and Technology college.
  • 2. Friend class Example: It is possible for one class to be a friend of another class. When this is the case, the friend class and all of its member functions have access to the private members defined within the other class. include <iostream> using namespace std; class TwoValues { int a; Int b; public: TwoValues(int i, int j) { a = i; b = j; } friend class Min; }; class Min { public: int min(TwoValues x); }; int min(TwoValues x) { return x.a < x.b ? x.a : x.b; } int main() { TwoValues ob(10, 20); Min m; cout << m.min(ob); return 0; }
  • 3. Inline function: There is an important feature in C++, called an inline function, that is commonly used with classes. you can create short functions that are not actually called; rather, their code is expanded in line at the point of each invocation. This process is similar to using a function-like macro. To cause a function to be expanded in line rather than called,precede its definition with the inline keyword. inline void myclass::show() { cout << a << " " << b << "n"; } int main() { myclass x; x.init(10, 20); x.show(); return 0; #include <iostream> using namespace std; class myclass { int a, b; public: void init(int i, int j); void show(); }; // Create an inline function. inline void myclass::init(int i, int j) { a = i; b = j;
  • 4. Parameterized Constructor: It is possible to pass arguments to constructor functions. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor's body, use the parameters to initialize the object. int main() { myclass ob(3, 5); ob.show(); return 0; } #include <iostream> using namespace std; class myclass { int a, b; public: myclass(int i, int j) {a=i; b=j;} void show() {cout << a << " " << b;} };
  • 5. Static Class Member: Both function and data members of a class can be made static. This section explains the consequences of each.
  • 6. Static data member: When you precede a member variable's declaration with static, you are telling the compiler that only one copy of that variable will exist and that all objects of the class will share that variable All static variables are initialized to zero before the first object int shared::a; // define a void shared::show() { cout << "This is static a: " << a; cout << "nThis is non-static b: " << b; cout << "n"; } #include <iostream> using namespace std; class shared { static int a; int b; public: void set(int i, int j) {a=i; b=j;} void show(); } ;
  • 7. Static data member: static a: 1 non-static b: 1 static a: 2 non-static b: 2 static a: 2 non-static b: 1 int main() { shared x, y; x.set(1, 1); // set a to 1 x.show(); y.set(2, 2); // change a to 2 y.show(); x.show(); /* Here, a has been changed for both x and y because a is shared by both objects. */ return 0; }
  • 8. Static member function:Member functions may also be declared as static. There are several restrictions placed on static member functions. They may only directly refer to other static members of the class. int main() { cl ob1, ob2; if(cl::get_resource()) cout << "ob1 has resourcen"; if(!cl::get_resource()) cout << "ob2 denied resourcen"; ob1.free_resource(); if(ob2.get_resource()) cout << "ob2 can now use resourcen"; return 0; } #include <iostream> using namespace std; class cl { static int resource; public: static int get_resourc (); void free_resource() { resource = 0; } }; int cl::resource; // define resource int cl::get_resource() { if(resource) return 0; else { resource = 1; return 1; } }
  • 9. WhenConstructoranddestructorareExecution: As a general rule, an object's constructor is called when the object comes into existence, and an object's destructor is called when the object is destroyed. Precisely when these events occur is discussed here. int main() { myclass local_ob1(3); cout << "This will not be first line displayed.n"; myclass local_ob2(4); return 0; } It displays this output: Initializing 1 Initializing 2 Initializing 3 This will not be first line displayed. Initializing 4 #include <iostream> using namespace std; class myclass { public: int who; myclass(int id); ~myclass(); } glob_ob1(1), glob_ob2(2); myclass::myclass(int id) { cout << "Initializing " << id << "n"; who = id; } myclass::~myclass() { cout << "Destructing " << who
  • 10. The scope resolution operator: The :: operator links a class name with a member name in order to tell the compiler what class the member belongs to. However, the scope resolution operator has another related use: it can allow access to a name in an enclosing scope that is "hidden" by a local declaration of the same name. int main() { myclass local_ob1(3); cout << "This will not be first line displayed.n"; myclass local_ob2(4); return 0; } It displays this output: Initializing 1 Initializing 2 Initializing 3 This will not be first line displayed. Initializing 4 #include <iostream> using namespace std; class myclass { public: int who; myclass(int id); ~myclass(); } glob_ob1(1), glob_ob2(2); myclass::myclass(int id) { cout << "Initializing " << id << "n"; who = id; } myclass::~myclass() {
  • 11. The scope resolution operator: The :: operator links a class name with a member name in order to tell the compiler  what class the member belongs to.  the scope resolution operator has another related use: it can allow access to a name in an enclosing scope that is "hidden" by a local declaration of the same name.
  • 12. Nested class  It is possible to define one class within another.  Doing so creates a nested class.  Since a class declaration does, in fact, define a scope, a nested class is valid only within the scope of the enclosing class.  Frankly, nested classes are seldom used.
  • 13. Nested classMember functions may also be declared as static. There are several restrictions placed on static member functions. They may only directly refer to other static members of the class. void f() { class myclass { int i; public: void put_i(int n) { i=n; } int get_i() { return i; } } ob; ob.put_i(10); cout << ob.get_i(); } #include <iostream> using namespace std; void f(); int main() { f(); // myclass not known here return 0; }
  • 14. Passing object to function Objects may be passed to functions in just the same way that any other type of variable can. Objects are passed to functions through the use of the standard call-by value mechanism. This means that a copy of an object is made when it is passed to a function. myclass::myclass(int n) { i = n; cout << "Constructing " << i << "n"; } myclass::~myclass() { cout << "Destroying " << i << "n"; } class myclass { int i; public: myclass(int n); ~myclass(); void set_i(int n) { i=n; } int get_i() { return i; } };