SlideShare une entreprise Scribd logo
1  sur  14
Lecture-4
Abu Saleh Musa Miah
M.Sc. Engg(On going)
University of Rajshahi
●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;
} }
When Constructor and destructor are Execution:
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()
{
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.
 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 class
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.
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; }
};

Contenu connexe

Tendances

Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objectsMahmoud Alfarra
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsVineeta Garg
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctorSomnath Kulkarni
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++aleenaguen
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic JavascriptBunlong Van
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic courseTran Khoa
 
Constructor&method
Constructor&methodConstructor&method
Constructor&methodJani Harsh
 
البرمجة الهدفية بلغة جافا - مصفوفة الكائنات
البرمجة الهدفية  بلغة جافا - مصفوفة الكائناتالبرمجة الهدفية  بلغة جافا - مصفوفة الكائنات
البرمجة الهدفية بلغة جافا - مصفوفة الكائناتMahmoud Alfarra
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
 
CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++Prof Ansari
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructorDa Mystic Sadi
 

Tendances (20)

Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objects
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
Constructors destructors
Constructors destructorsConstructors destructors
Constructors destructors
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Unit ii
Unit   iiUnit   ii
Unit ii
 
Constructor and desturctor
Constructor and desturctorConstructor and desturctor
Constructor and desturctor
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 
Basic Javascript
Basic JavascriptBasic Javascript
Basic Javascript
 
Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic course
 
Constructor&method
Constructor&methodConstructor&method
Constructor&method
 
البرمجة الهدفية بلغة جافا - مصفوفة الكائنات
البرمجة الهدفية  بلغة جافا - مصفوفة الكائناتالبرمجة الهدفية  بلغة جافا - مصفوفة الكائنات
البرمجة الهدفية بلغة جافا - مصفوفة الكائنات
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
Core Java
Core JavaCore Java
Core Java
 
CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 

En vedette

Affordable Taiwan Travel
Affordable Taiwan TravelAffordable Taiwan Travel
Affordable Taiwan TravelMUSTHoover
 
Keynote "Kommunikation im Gesundheitssektor" vom 25. April 2013, Wien
Keynote "Kommunikation im Gesundheitssektor" vom 25. April 2013, WienKeynote "Kommunikation im Gesundheitssektor" vom 25. April 2013, Wien
Keynote "Kommunikation im Gesundheitssektor" vom 25. April 2013, Wienbeeq
 
DevOps Boston - Heartbleed at Acquia
DevOps Boston - Heartbleed at AcquiaDevOps Boston - Heartbleed at Acquia
DevOps Boston - Heartbleed at AcquiaMarc Seeger
 
การประกอบ GEARBOX-MOTOR
การประกอบ GEARBOX-MOTORการประกอบ GEARBOX-MOTOR
การประกอบ GEARBOX-MOTORkroowissanu
 
JRuby on Rails Deployment: What They Didn't Tell You
JRuby on Rails Deployment: What They Didn't Tell YouJRuby on Rails Deployment: What They Didn't Tell You
JRuby on Rails Deployment: What They Didn't Tell Youelliando dias
 
รูปพื้นที่ผิว
รูปพื้นที่ผิวรูปพื้นที่ผิว
รูปพื้นที่ผิวKrueed Huaybong
 
Verden lige nu
Verden lige nuVerden lige nu
Verden lige nupersloth
 
презентация
презентацияпрезентация
презентацияmetodkopilka
 
Evangelio Ilutsrado, 4º Domingo de Pascua
Evangelio Ilutsrado, 4º Domingo de PascuaEvangelio Ilutsrado, 4º Domingo de Pascua
Evangelio Ilutsrado, 4º Domingo de Pascuacristinamoreubi
 
Must taitung team
Must taitung teamMust taitung team
Must taitung teamMUSTHoover
 
Vancouver Rebels of Recruiting Roadshow | Ami Price from ATB Financial
Vancouver Rebels of Recruiting Roadshow | Ami Price from ATB FinancialVancouver Rebels of Recruiting Roadshow | Ami Price from ATB Financial
Vancouver Rebels of Recruiting Roadshow | Ami Price from ATB FinancialGlassdoor
 
¿Quién controla los medios de comunicación en el perú?
¿Quién controla los medios de comunicación en el perú?¿Quién controla los medios de comunicación en el perú?
¿Quién controla los medios de comunicación en el perú?Franck Campos
 
FIGURAS GEOMETRICAS
FIGURAS GEOMETRICASFIGURAS GEOMETRICAS
FIGURAS GEOMETRICASsaraycreek
 
How Amazon Echo can be helpful for the healthcare industry
How Amazon Echo can be helpful for the healthcare industryHow Amazon Echo can be helpful for the healthcare industry
How Amazon Echo can be helpful for the healthcare industrySoftweb Solutions
 
Affordable travel: Kenting Taiwan
Affordable travel: Kenting TaiwanAffordable travel: Kenting Taiwan
Affordable travel: Kenting TaiwanMUSTHoover
 

En vedette (20)

Affordable Taiwan Travel
Affordable Taiwan TravelAffordable Taiwan Travel
Affordable Taiwan Travel
 
Webmail
WebmailWebmail
Webmail
 
Keynote "Kommunikation im Gesundheitssektor" vom 25. April 2013, Wien
Keynote "Kommunikation im Gesundheitssektor" vom 25. April 2013, WienKeynote "Kommunikation im Gesundheitssektor" vom 25. April 2013, Wien
Keynote "Kommunikation im Gesundheitssektor" vom 25. April 2013, Wien
 
DevOps Boston - Heartbleed at Acquia
DevOps Boston - Heartbleed at AcquiaDevOps Boston - Heartbleed at Acquia
DevOps Boston - Heartbleed at Acquia
 
การประกอบ GEARBOX-MOTOR
การประกอบ GEARBOX-MOTORการประกอบ GEARBOX-MOTOR
การประกอบ GEARBOX-MOTOR
 
JRuby on Rails Deployment: What They Didn't Tell You
JRuby on Rails Deployment: What They Didn't Tell YouJRuby on Rails Deployment: What They Didn't Tell You
JRuby on Rails Deployment: What They Didn't Tell You
 
รูปพื้นที่ผิว
รูปพื้นที่ผิวรูปพื้นที่ผิว
รูปพื้นที่ผิว
 
Verden lige nu
Verden lige nuVerden lige nu
Verden lige nu
 
презентация
презентацияпрезентация
презентация
 
Evangelio Ilutsrado, 4º Domingo de Pascua
Evangelio Ilutsrado, 4º Domingo de PascuaEvangelio Ilutsrado, 4º Domingo de Pascua
Evangelio Ilutsrado, 4º Domingo de Pascua
 
06 regression
06 regression06 regression
06 regression
 
Arquitetura de informação
Arquitetura de informaçãoArquitetura de informação
Arquitetura de informação
 
Must taitung team
Must taitung teamMust taitung team
Must taitung team
 
Vancouver Rebels of Recruiting Roadshow | Ami Price from ATB Financial
Vancouver Rebels of Recruiting Roadshow | Ami Price from ATB FinancialVancouver Rebels of Recruiting Roadshow | Ami Price from ATB Financial
Vancouver Rebels of Recruiting Roadshow | Ami Price from ATB Financial
 
¿Quién controla los medios de comunicación en el perú?
¿Quién controla los medios de comunicación en el perú?¿Quién controla los medios de comunicación en el perú?
¿Quién controla los medios de comunicación en el perú?
 
FIGURAS GEOMETRICAS
FIGURAS GEOMETRICASFIGURAS GEOMETRICAS
FIGURAS GEOMETRICAS
 
Ch15
Ch15Ch15
Ch15
 
Resume of Lenin Babu
Resume of Lenin BabuResume of Lenin Babu
Resume of Lenin Babu
 
How Amazon Echo can be helpful for the healthcare industry
How Amazon Echo can be helpful for the healthcare industryHow Amazon Echo can be helpful for the healthcare industry
How Amazon Echo can be helpful for the healthcare industry
 
Affordable travel: Kenting Taiwan
Affordable travel: Kenting TaiwanAffordable travel: Kenting Taiwan
Affordable travel: Kenting Taiwan
 

Similaire à Lecture 4.2 c++(comlete reference book)

Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)Durga Devi
 
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-13Abu Saleh
 
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 Boro
 
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 & objectsRai University
 
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 & objectsRai University
 
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
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and DestructorsKeyur Vadodariya
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-exampleDeepak Singh
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentationnafisa rahman
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 

Similaire à Lecture 4.2 c++(comlete reference book) (20)

OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
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
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Class and object
Class and objectClass and object
Class and object
 
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
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
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
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
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
 
Intake 37 4
Intake 37 4Intake 37 4
Intake 37 4
 
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
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
Java class
Java classJava class
Java class
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 

Plus de Abu Saleh

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.Abu Saleh
 
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-16Abu Saleh
 
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-15Abu Saleh
 
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-14Abu Saleh
 
Computer fundamental 1.introduction
Computer fundamental 1.introductionComputer fundamental 1.introduction
Computer fundamental 1.introductionAbu Saleh
 
13.computer buses
13.computer buses13.computer buses
13.computer busesAbu Saleh
 

Plus de Abu Saleh (6)

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.
 
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
 
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
 
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
 
Computer fundamental 1.introduction
Computer fundamental 1.introductionComputer fundamental 1.introduction
Computer fundamental 1.introduction
 
13.computer buses
13.computer buses13.computer buses
13.computer buses
 

Dernier

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Dernier (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

Lecture 4.2 c++(comlete reference book)

  • 1. Lecture-4 Abu Saleh Musa Miah M.Sc. Engg(On going) University of Rajshahi
  • 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. When Constructor and destructor are Execution: 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() { cout << "Destructing " << who
  • 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 class 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. 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; } };