<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4094940111921452231</id><updated>2024-09-19T03:54:36.508+05:30</updated><category term="C++"/><category term="Classes and Objects"/><category term="Const"/><category term="Header files"/><category term="Low level const"/><category term="Namespaces in C++"/><category term="Top level and low level const"/><category term="Top level const"/><category term="access specifiers"/><category term="class templates in C++"/><category term="function templates in C++"/><category term="header files in C++"/><category term="templates"/><title type='text'>Let&#39;s Practice Programming</title><subtitle type='html'>A blog to share and gain the knowledge of programming.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://letspracticeprogramming.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='https://letspracticeprogramming.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Saurabh</name><uri>http://www.blogger.com/profile/03890363916503088023</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX0fmK91PXdZkLfX2Yqloq9Q01aP6rj7zRV84uOzbV0el--Ouu7Kn7yoGCc7GolJ2UAFSFu65I0rpoVZR4spxiVq45lkwWFp6B-NrKPfQRhAj2hgU4QSn1neTdtxXktq4/s220/Screenshot_20210111_231427.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4094940111921452231.post-6881958675990999243</id><published>2021-02-04T20:48:00.004+05:30</published><updated>2021-02-07T13:46:01.686+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="class templates in C++"/><title type='text'>Class Templates in C++</title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAvTET44iudd-5xM6lkP8y86DyHNMdjNQ0AYZJgF4gLr7-mbSqJy7byzRm8YCXLJszSiNggdTw-MaiHnGqeJ7KYMTI_SPgUijVN2mmNjRSBMK-OxosC6WsAxOksMXD3k1vS97NpwIxglc/s1033/Class+templates+in+C%252B%252B.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Class templates in C++&quot; border=&quot;0&quot; data-original-height=&quot;506&quot; data-original-width=&quot;1033&quot; height=&quot;262&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAvTET44iudd-5xM6lkP8y86DyHNMdjNQ0AYZJgF4gLr7-mbSqJy7byzRm8YCXLJszSiNggdTw-MaiHnGqeJ7KYMTI_SPgUijVN2mmNjRSBMK-OxosC6WsAxOksMXD3k1vS97NpwIxglc/w535-h262/Class+templates+in+C%252B%252B.png&quot; title=&quot;Class templates in C++&quot; width=&quot;535&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Templates in C++ are of two types&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;background-color: white; clear: both; font-family: Merriweather, Georgia, serif; font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;1) &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/02/function-templates.html&quot; target=&quot;_blank&quot;&gt;Function Templates&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;background-color: white; clear: both; font-family: Merriweather, Georgia, serif; font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;background-color: white; clear: both; font-family: Merriweather, Georgia, serif; font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;2) Class Templates.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;background-color: white; clear: both; font-family: Merriweather, Georgia, serif; font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;background-color: white; clear: both; font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In this article we will learn about class templates.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;background-color: white; clear: both; font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;background-color: white; clear: both; font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Class templates are a way through which we can use the same &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/classes-and-objects-in-cpp.html&quot; target=&quot;_blank&quot;&gt;class&lt;/a&gt; for different data types. You don&#39;t have to write classes with the same functionality again and again for different data types.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;background-color: white; clear: both; font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;background-color: white; clear: both; font-size: 16px;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;A difference between &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/02/function-templates.html&quot; target=&quot;_blank&quot;&gt;function templates&lt;/a&gt; and class templates is that the compiler cannot deduce the data types for the template parameters from the arguments provided by the user in the class template, so you have to explicitly specify the template arguments (data types to use) for the class template.&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;background-color: white; clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Defining a Class Template&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Following is a basic example of a class template.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T&amp;gt;    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// template with single parameter T&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;calci&lt;/span&gt; {
private:
	T first, second;  &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// two private variables of type T&lt;/span&gt;
public:
	calci(T var1, T var2) {      &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// a constructor with two parameters&lt;/span&gt;
		first = var1;        &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// assigning the argument values&lt;/span&gt;
		second = var2;	     &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// to the private variables&lt;/span&gt;
	}	
	T add() {			 &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// function definition which returns the&lt;/span&gt;
		&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; first + second;	 &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// the sum of the private variables&lt;/span&gt;
	}
	T subtract();		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// declaring a function&lt;/span&gt;
};

&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T&amp;gt;		 &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining function outside the class&lt;/span&gt;
T calci&amp;lt;T&amp;gt;::subtract() {	 &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// provide template parameter of class template&lt;/span&gt;
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; first - second;	 &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// function returns the difference of the private variables&lt;/span&gt;
}

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; main() {
	calci&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt;&amp;gt;obj1(&lt;span style=&quot;color: #009999;&quot;&gt;10&lt;/span&gt;, &lt;span style=&quot;color: #009999;&quot;&gt;20&lt;/span&gt;);     &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// creating an object of class with template argument as &quot;int&quot;&lt;/span&gt;
	cout &amp;lt;&amp;lt; obj1.add() &amp;lt;&amp;lt; endl;	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// calling function &quot;add&quot; through object&lt;/span&gt;
	cout &amp;lt;&amp;lt; obj1.subtract() &amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// calling function &quot;subtract&quot; through object&lt;/span&gt;
	
	calci&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt;&amp;gt;obj2(&lt;span style=&quot;color: #009999;&quot;&gt;10.285&lt;/span&gt;, &lt;span style=&quot;color: #009999;&quot;&gt;20.267&lt;/span&gt;); &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// another object with template argument as &quot;double&quot;&lt;/span&gt;
	cout &amp;lt;&amp;lt; obj2.add() &amp;lt;&amp;lt; endl;		   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// calling the functions from second object&lt;/span&gt;
	cout &amp;lt;&amp;lt; obj2.subtract() &amp;lt;&amp;lt; endl;
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above example, from line 4, we define the class template. The&amp;nbsp; definition starts with the keyword &lt;i&gt;template&lt;/i&gt;&amp;nbsp;and then in the angular brackets, another keyword &lt;i&gt;typename&lt;/i&gt;&amp;nbsp;(which can be substituted by keyword &lt;i&gt;class&lt;/i&gt;) and then a placeholder for a data type where we have written &lt;i&gt;T&lt;/i&gt;. You can anything instead of &lt;i&gt;T&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;We provide a single template parameter &lt;i&gt;T&lt;/i&gt;, this &lt;i&gt;T&lt;/i&gt;&amp;nbsp;can indicate any data type the user provides as a template argument while creating an object from the class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T&amp;gt;    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// template with single parameter T&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the class named &lt;i&gt;calci&lt;/i&gt;, we declare two &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/access-specifiers-in-cpp.html&quot; target=&quot;_blank&quot;&gt;private&lt;/a&gt; variables, &lt;i&gt;first&lt;/i&gt;&amp;nbsp;and &lt;i&gt;second&lt;/i&gt;, they are of&amp;nbsp;data type &lt;i&gt;T&lt;/i&gt;&amp;nbsp;(&lt;i&gt;T&lt;/i&gt;&amp;nbsp;indicates the data type specified by the user at the time of creating an object).&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;From line 9 we define a constructor (function with same name as of the class and which is executed automatically when an object is created) which is under public &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/access-specifiers-in-cpp.html&quot; target=&quot;_blank&quot;&gt;access specifier&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;calci(T var1, T var2) {      &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// a constructor which takes two parameters&lt;/span&gt;
	first = var1;            &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// assigning the argument values&lt;/span&gt;
	second = var2;		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// to the private variables&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;The constructor takes 2 arguments which will be passed when we create an object and assign the values of the arguments to the private variables.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Then from line 13, we define a function named&amp;nbsp;&lt;i&gt;add()&lt;/i&gt;&amp;nbsp;which returns the sum of the two private variables &lt;i&gt;first&lt;/i&gt;&amp;nbsp;and &lt;i&gt;second&lt;/i&gt;. The return type of the function is &lt;i&gt;T&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Here the arguments are supposed to be numerical types (eg. int) or the types which can be converted to a numerical&amp;nbsp;(eg. char).&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;T &lt;span style=&quot;color: #00aa00;&quot;&gt;add&lt;/span&gt;() {	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// function definition which returns the&lt;/span&gt;
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; first + second;	 &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// the sum of the private variables&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 16, we declare a function named &lt;i&gt;subtract()&lt;/i&gt;&amp;nbsp;which will be defined outside the class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;T &lt;span style=&quot;color: #00aa00;&quot;&gt;subtract&lt;/span&gt;();	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// declaring a function subtract()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Now outside the class, from line 19, we use the &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/02/function-templates.html&quot; target=&quot;_blank&quot;&gt;function template&lt;/a&gt; format&amp;nbsp;to define a function which was declared inside the class. We didn&#39;t use the template format for the function which was defined inside the class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T&amp;gt;		 &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a function outside the class&lt;/span&gt;
T calci&amp;lt;T&amp;gt;::subtract() {	 &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// provide template parameter of class template&lt;/span&gt;
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; first - second;	 &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// function returns the difference of the private variables&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;One thing to note in the second line of the above code is that we have to provide the template&amp;nbsp;parameter&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;just after the class name.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;div style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;First we write the return type of the function that is&amp;nbsp;&lt;i&gt;T&lt;/i&gt;, then the class name followed by the template parameter (in this case &amp;lt;&lt;i&gt;T&amp;gt;&lt;/i&gt;)&amp;nbsp;and then we use the scope operator (&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;::&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;) to access the member function of the class. The &lt;i&gt;substract()&lt;/i&gt;&amp;nbsp;function returns the difference of the two private variables &lt;i&gt;first&lt;/i&gt;&amp;nbsp;and &lt;i&gt;second&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Now in the &lt;i&gt;main()&lt;/i&gt;&amp;nbsp;function on line 25, we create an object named &lt;i&gt;obj1&lt;/i&gt;.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;calci&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt;&amp;gt;obj1(&lt;span style=&quot;color: #009999;&quot;&gt;10&lt;/span&gt;, &lt;span style=&quot;color: #009999;&quot;&gt;20&lt;/span&gt;);     &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// creating an object of class &quot;calci&quot; with template parameter as &quot;int&quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;As said, the compiler cannot deduce the data type for the template arguments from the arguments provided in a class template. Hence we have to explicitly&amp;nbsp;specify the data type in the angle brackets just after class name (in this case &lt;i&gt;&amp;lt;int&amp;gt;&lt;/i&gt;). And also we have to pass two arguments (int arguments) to the object (as the constructor of the class takes two arguments).&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Then on line 26 and 27 we call the &lt;i&gt;add()&lt;/i&gt; and &lt;i&gt;subtract()&lt;/i&gt;&amp;nbsp;function respectively through the object &lt;i&gt;obj1&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;cout &amp;lt;&amp;lt; obj1.add() &amp;lt;&amp;lt; endl;	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// calling functions through object&lt;/span&gt;
cout &amp;lt;&amp;lt; obj1.subtract() &amp;lt;&amp;lt; endl;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Likewise on line 29, we create another object named &lt;i&gt;obj2&lt;/i&gt; of class &lt;i&gt;calci&lt;/i&gt;. This time we specify the data type as &lt;i&gt;double&lt;/i&gt;&amp;nbsp;and pass two double arguments to the object.&amp;nbsp;Also we call the &lt;i&gt;add()&lt;/i&gt;&amp;nbsp;and &lt;i&gt;subtract()&lt;/i&gt;&amp;nbsp;function through the object.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;calci&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt;&amp;gt;obj2(&lt;span style=&quot;color: #009999;&quot;&gt;10.285&lt;/span&gt;, &lt;span style=&quot;color: #009999;&quot;&gt;20.267&lt;/span&gt;); &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// another object with template argument as &quot;double&quot;&lt;/span&gt;
cout &amp;lt;&amp;lt; obj2.add() &amp;lt;&amp;lt; endl;		   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// calling the functions from second object&lt;/span&gt;
cout &amp;lt;&amp;lt; obj2.subtract() &amp;lt;&amp;lt; endl;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Defining Class Template with multiple parameters&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Below is an example of the a class template with multiple parameters.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; U&amp;gt;    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// template with two parameters&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;test&lt;/span&gt; {
private:
	T first;	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// private member of type T&lt;/span&gt;
	U second;	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// private member of type U&lt;/span&gt;
public:
	test(T var1, U var2);   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// constructor declaration&lt;/span&gt;
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;show&lt;/span&gt;();		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// function declaration&lt;/span&gt;
};
&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; U&amp;gt;	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// multiple parameter template format&lt;/span&gt;
test&amp;lt;T, U&amp;gt;::test(T var1, U var2) {	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining the constructor with two different data type parameters&lt;/span&gt;
	first = var1;			&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// assigning the value of arguments to &lt;/span&gt;
	second = var2;			&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// the private variables&lt;/span&gt;
}
&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; U&amp;gt;	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining function outside the class&lt;/span&gt;
&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; test&amp;lt;T, U&amp;gt;::show() {
	cout  &amp;lt;&amp;lt; first &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot; and &quot;&lt;/span&gt; &amp;lt;&amp;lt; second &amp;lt;&amp;lt; endl;  &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// has access to private members&lt;/span&gt;
}
&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; main() {
	test&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt;, string&amp;gt; obj1(&lt;span style=&quot;color: #009999;&quot;&gt;10&lt;/span&gt;, &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;);  &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// specifying data types when creating object &lt;/span&gt;
				 &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// and also passing arguments&lt;/span&gt;
	obj1.show();		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// calling function through object&lt;/span&gt;
	
	test&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;char&lt;/span&gt;, &lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt;&amp;gt; obj2(&lt;span style=&quot;color: #aa5500;&quot;&gt;&#39;A&#39;&lt;/span&gt;, &lt;span style=&quot;color: #009999;&quot;&gt;2.3&lt;/span&gt;);	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// another object with different template arguments&lt;/span&gt;
	obj2.show();
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In above code, we define the class template from line 4, this time we have two template parameters &lt;i&gt;T&lt;/i&gt;&amp;nbsp;and &lt;i&gt;U&lt;/i&gt;&amp;nbsp;which indicate the data types specified by the user when we create an object of the class.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; U&amp;gt;   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// template with two parameters&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;The class is named as &lt;i&gt;test&lt;/i&gt;. It has two private variables&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;private:
	T first;		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// private member of type T&lt;/span&gt;
	U second;		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// private member of type U&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;variable&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;first&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;is of type &lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;T&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt; and variable &lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;second&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;is of type &lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;U&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Under the public access specifier, we declare a constructor which has two parameters of type &lt;i&gt;T&lt;/i&gt;&amp;nbsp;and &lt;i&gt;U&lt;/i&gt;&amp;nbsp;respectively.&amp;nbsp;We also declare a function named &lt;i&gt;show()&lt;/i&gt;. Both the constructor and the function are defined outside the class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Because the constructor is defined outside the class, we have to&amp;nbsp; write the&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;template format as from line 13.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; U&amp;gt;   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// multiple parameter template format&lt;/span&gt;
test&amp;lt;T, U&amp;gt;::test(T var1, U var2) {   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining the constructor with two parameters&lt;/span&gt;
	first = var1;		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// assigning the value of arguments to &lt;/span&gt;
	second = var2;		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// the private variables&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;And on the second line in the above code, we have to provide the template parameters after the class name (in this case &lt;i&gt;&amp;lt;T, U&amp;gt;&lt;/i&gt;). Then we use the scope operator to access the constructor of the class. Here the constructor takes two parameters -&amp;nbsp;&lt;i&gt;var1&lt;/i&gt;&amp;nbsp;is of type &lt;i&gt;T&lt;/i&gt;&amp;nbsp;and &lt;i&gt;var2&lt;/i&gt;&amp;nbsp;is of type &lt;i&gt;U&lt;/i&gt;. The values of both the parameters are then assigned to the private variables &lt;i&gt;first&lt;/i&gt;&amp;nbsp;and &lt;i&gt;second&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;To define the function &lt;i&gt;show()&lt;/i&gt; outside the class, again we write the &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/02/function-templates.html&quot; target=&quot;_blank&quot;&gt;function template&lt;/a&gt; format on line 18,&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; U&amp;gt;	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining constructor outside the class&lt;/span&gt;
&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; test&amp;lt;T, U&amp;gt;::show() {
	cout  &amp;lt;&amp;lt; first &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot; and &quot;&lt;/span&gt; &amp;lt;&amp;lt; second &amp;lt;&amp;lt; endl;  &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// has access to private members&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;After that on the next line, we write the return type (in this case - void), then comes the class name. As said, we have to provide the template parameters in the angle brackets after the class name and then the scope operator followed by the function name.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;This function prints the values of the private variables which were assigned the values of the arguments of the constructor.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Now in the &lt;i&gt;main()&lt;/i&gt;&amp;nbsp;function on line 23,&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;test&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt;, string&amp;gt; obj1(&lt;span style=&quot;color: #009999;&quot;&gt;10&lt;/span&gt;, &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;);  &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// specifying data types when creating object &lt;/span&gt;
  		                &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// and also passing arguments&lt;/span&gt;
obj1.show();			&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// calling function through object&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;We create an object named &lt;i&gt;obj1&lt;/i&gt;, of the class &lt;i&gt;test&lt;/i&gt;. For that we need to write the class name, then in angular brackets, we provided&amp;nbsp;the data types as template arguments of the class template (in this case &lt;i&gt;&amp;lt;int, string&amp;gt;&lt;/i&gt;) and then a name for the object to be created. We also provide arguments for parameters to the constructor of the class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Then on line 25, we call the function &lt;i&gt;show()&lt;/i&gt;&amp;nbsp;through the object &lt;i&gt;obj1&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Likewise we create another object named &lt;i&gt;obj2&lt;/i&gt; on line 27, this time we pass the data types as &lt;i&gt;&amp;lt;char, double&amp;gt;&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Default arguments in Class Templates&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;We shall modify the previous example a little bit to understand default parameters in class templates.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T=&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; U=string&amp;gt;   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// specifying default parameters&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;test&lt;/span&gt; {
private:	
	T first;
	U second;
public:
	test(T var1, U var2);
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;show&lt;/span&gt;();
};

&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; U&amp;gt;
test&amp;lt;T, U&amp;gt;::test(T var1, U var2) {
	first = var1;
	second = var2;
}

&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; U&amp;gt;
&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; test&amp;lt;T, U&amp;gt;::show() {
	cout  &amp;lt;&amp;lt; first &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot; and &quot;&lt;/span&gt; &amp;lt;&amp;lt; second &amp;lt;&amp;lt; endl;
}

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; main() {

	test&amp;lt;&amp;gt; obj1(&lt;span style=&quot;color: #009999;&quot;&gt;10&lt;/span&gt;, &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;);    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// empty angle brackets&lt;/span&gt;
	obj1.show();

	test&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;char&lt;/span&gt;, &lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt;&amp;gt; obj2(&lt;span style=&quot;color: #aa5500;&quot;&gt;&#39;A&#39;&lt;/span&gt;, &lt;span style=&quot;color: #009999;&quot;&gt;2.3&lt;/span&gt;);
	obj2.show();

	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above program on line 4, we specify the default template parameters,&amp;nbsp;&lt;i&gt;T&lt;/i&gt;&amp;nbsp;as&amp;nbsp;&lt;i&gt;int&lt;/i&gt; and&amp;nbsp;&lt;i&gt;U&lt;/i&gt;&amp;nbsp;as &lt;i&gt;double&lt;/i&gt;. If the template arguments passed are of the same type as the type of the default parameters respectively, the angular brackets can be empty while creating an object of the class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;And as you can notice on line 27, we kept the angular brackets empty because the types of the arguments passed are respectively the same as the default parameter types of the class template (in this case &lt;i&gt;&amp;lt;int, string&amp;gt;&lt;/i&gt;).&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;test&amp;lt;&amp;gt; obj1(&lt;span style=&quot;color: #009999;&quot;&gt;10&lt;/span&gt;, &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;);    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// empty angle brackets&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;



&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;But on line 30, while creating another object of the class, we specify the template arguments as they are different from the default arguments.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In this way we can use class templates in C++.&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://letspracticeprogramming.blogspot.com/feeds/6881958675990999243/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/02/class-templates-in-cpp.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/6881958675990999243'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/6881958675990999243'/><link rel='alternate' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/02/class-templates-in-cpp.html' title='Class Templates in C++'/><author><name>Saurabh</name><uri>http://www.blogger.com/profile/03890363916503088023</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX0fmK91PXdZkLfX2Yqloq9Q01aP6rj7zRV84uOzbV0el--Ouu7Kn7yoGCc7GolJ2UAFSFu65I0rpoVZR4spxiVq45lkwWFp6B-NrKPfQRhAj2hgU4QSn1neTdtxXktq4/s220/Screenshot_20210111_231427.JPG'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAvTET44iudd-5xM6lkP8y86DyHNMdjNQ0AYZJgF4gLr7-mbSqJy7byzRm8YCXLJszSiNggdTw-MaiHnGqeJ7KYMTI_SPgUijVN2mmNjRSBMK-OxosC6WsAxOksMXD3k1vS97NpwIxglc/s72-w535-h262-c/Class+templates+in+C%252B%252B.png" height="72" width="72"/><thr:total>0</thr:total><georss:featurename>Jaripatka, Nagpur, Maharashtra 440014, India</georss:featurename><georss:point>21.1820557 79.0859952</georss:point><georss:box>-7.1281781361788461 43.9297452 49.492289536178845 114.2422452</georss:box></entry><entry><id>tag:blogger.com,1999:blog-4094940111921452231.post-5715131945124633689</id><published>2021-02-01T17:48:00.037+05:30</published><updated>2021-02-05T01:39:02.235+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="function templates in C++"/><category scheme="http://www.blogger.com/atom/ns#" term="templates"/><title type='text'>Function Templates In C++</title><content type='html'>&lt;p&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB62tECJG153sNjXBAiUUlNTL2eh1m1CgJO_Y003lvnrdYbApywUCNqpiYRZCIP5HHYRZywYKZh9TLtk3A9WnOQd-lQZakLyFcMFkzoisttBpUSy7uNtpPKwYI9xMRtvb1TIVe_zuypI8/s1033/Function+Templates+In+C%252B%252B.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Function templates in C++&quot; border=&quot;0&quot; data-original-height=&quot;506&quot; data-original-width=&quot;1033&quot; height=&quot;275&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB62tECJG153sNjXBAiUUlNTL2eh1m1CgJO_Y003lvnrdYbApywUCNqpiYRZCIP5HHYRZywYKZh9TLtk3A9WnOQd-lQZakLyFcMFkzoisttBpUSy7uNtpPKwYI9xMRtvb1TIVe_zuypI8/w561-h275/Function+Templates+In+C%252B%252B.png&quot; title=&quot;Function templates in C++&quot; width=&quot;561&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Templates are of two types.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;1) Function Templates.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;2) &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/02/class-templates-in-cpp.html&quot; target=&quot;_blank&quot;&gt;Class Templates&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In this article we will study about function templates.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;h3 style=&quot;clear: both; font-family: &amp;quot;Times New Roman&amp;quot;; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Why Function Templates&lt;/span&gt;&lt;/h3&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Function templates are a simple and powerful feature of C++. They are a form of generic programming.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;They are generic because we don&#39;t need to specify the return type of the function and the data types of the variables and parameters used in the function at the time of defining it. We can use one function template for different data types.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Take a look at the following code.&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a function &quot;add&quot; which takes 2 integer parameters&lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// and returns their sum&lt;/span&gt;
&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;add&lt;/span&gt;(&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; var1, &lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; var2) {        
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt;  var1 + var2;
}

&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a function with the same name &quot;add&quot; which takes&lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// two double parameters and returns their sum&lt;/span&gt;
&lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;add&lt;/span&gt;(&lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt; var1, &lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt; var2) {
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; var1 + var2;
}
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above code, we see function overloading (multiple functions with same name but with different arguments). Except the function return type and parameter data type everything is same.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Both the functions perform the same tasks i.e. adding values of two parameters except that the first function adds two integers and the second function adds two doubles.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In this situation we can use templates instead.&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Creating a function template&lt;/span&gt;&lt;/h3&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a function template&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T&amp;gt;       &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// template takes one parameter for data type&lt;/span&gt;
T add(T var1, T var2) {    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// function take two parameters and&lt;/span&gt;
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; var1 + var2;    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// returns their sum&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;



&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above code we created a function template. It starts with the keyword &lt;i&gt;template&lt;/i&gt;&amp;nbsp;and then in the angle brackets, another keyword &lt;i&gt;typename&lt;/i&gt;&amp;nbsp;(alias word for &lt;i&gt;typename&lt;/i&gt;&amp;nbsp;is &lt;i&gt;class&lt;/i&gt;) and then a &quot;placeholder&quot; in which we have written &lt;i&gt;T&lt;/i&gt;. &lt;i&gt;T&lt;/i&gt;&amp;nbsp;indicates the data type. You can write anything at the place of &lt;i&gt;T&lt;/i&gt;. Here we have only one template parameter &lt;i&gt;T&lt;/i&gt;.(we will see a template with multiple template parameters down in this article.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T&amp;gt;       &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// template takes one parameter for data type&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;As you can see on the next line, the return type of the function and the data type of its parameters is &lt;i&gt;T&lt;/i&gt;.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Here&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;T&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;can be any data type.&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;T&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;is a placeholder for the data type which is either deduced by the compiler from the arguments passed or is specified by the user at the time of calling this function.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;We use the above template in the example below.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a function template&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T&amp;gt;
T add(T var1, T var2) {
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; var1 + var2;
}


&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; main() {
	
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; int1 = &lt;span style=&quot;color: #009999;&quot;&gt;3&lt;/span&gt;, int2 = &lt;span style=&quot;color: #009999;&quot;&gt;5&lt;/span&gt;;         &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining two integers&lt;/span&gt;
	cout &amp;lt;&amp;lt; add&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt;&amp;gt;(int1, int2) &amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// specifying data type while calling the function&lt;/span&gt;
	cout &amp;lt;&amp;lt; add(int1, int2) &amp;lt;&amp;lt; endl;      &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// data type deduced by the compiler&lt;/span&gt;

	&lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt; dou1 = &lt;span style=&quot;color: #009999;&quot;&gt;3.9&lt;/span&gt;, dou2 = &lt;span style=&quot;color: #009999;&quot;&gt;4.5&lt;/span&gt;;   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining 2 doubles&lt;/span&gt;
	cout &amp;lt;&amp;lt; add&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt;&amp;gt;(dou1, dou2) &amp;lt;&amp;lt; endl;   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// specifying data type while calling the function&lt;/span&gt;
	cout &amp;lt;&amp;lt; add(dou1, dou2) &amp;lt;&amp;lt; endl;        &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// data type deduced by the compiler&lt;/span&gt;

	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above example, from line 5, we create the function template. As said the return type of the function and the data type of the parameters of the function, which will be decided when we call the function&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;are indicated by&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;T.&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 13, we defined two integer variables and on line 14, we print the result of the call to the function by passing the two integers.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;cout &amp;lt;&amp;lt; add&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt;&amp;gt;(int1, int2) &amp;lt;&amp;lt; endl; &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// specifying data type while calling the function&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Here we explicitly specify the data type (&lt;i&gt;&amp;lt;int&amp;gt;&lt;/i&gt;) in the angular brackets while calling the function. This means that all the &lt;i&gt;T&lt;/i&gt;s&amp;nbsp;in the function template are replaced by&amp;nbsp;&lt;i&gt;int&lt;/i&gt;.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;And on line 15, again&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;we print the result of the call to the function by passing the two integers&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;but this time we do not specify the data type, hence it is deduced by the compiler from the arguments provided.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;cout &amp;lt;&amp;lt; add(int1, int2) &amp;lt;&amp;lt; endl;      &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// data type deduced by the compiler&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Then on line 17, we defined two double variables, and on line 18,&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;cout &amp;lt;&amp;lt; add&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt;&amp;gt;(dou1, dou2) &amp;lt;&amp;lt; endl;   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// specifying data type while calling the function&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;We specified the data type (&lt;i&gt;&amp;lt;double&amp;gt;&lt;/i&gt;), hence all the &lt;i&gt;T&lt;/i&gt;s&amp;nbsp;in the function will be replaced by &lt;i&gt;double&lt;/i&gt; when we call that function.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;And on line 19, we do not specify the data type, hence it is deduced by the compiler&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;from the arguments provided&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Here in this function both the parameters passed to the function must be of the same type.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above function template, we passed only one parameter &lt;i&gt;T&lt;/i&gt; for data type, we can pass multiple parameters.&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Function template with multiple template parameters&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the following example,&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a function template with multiple parameters for data types&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; U&amp;gt;
U add(T var1, U var2) {           &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// return type is U&lt;/span&gt;
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; var1 + var2;
}
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;the function template has two template parameters and on the next line, the function takes two parameters of two different data types and returns their sum.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;div style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Note that in the above code, the return type of the function and the data type of the second parameter is same. We executed the above function template in the below code.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a function template with multiple parameters for data types&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;template&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; T, &lt;span style=&quot;color: #0000aa;&quot;&gt;typename&lt;/span&gt; U&amp;gt;
U add(T var1, U var2) {           &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// return type is U&lt;/span&gt;
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; var1 + var2;
}

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; main() {
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining one integer and one double&lt;/span&gt;
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; int1 = &lt;span style=&quot;color: #009999;&quot;&gt;4&lt;/span&gt;;
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt; dou1 = &lt;span style=&quot;color: #009999;&quot;&gt;3.8&lt;/span&gt;;

	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// specifying both data types&lt;/span&gt;
	cout &amp;lt;&amp;lt; add&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt;&amp;gt;(int1, dou1) &amp;lt;&amp;lt; endl;
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// data type deduced by the compiler&lt;/span&gt;
	cout &amp;lt;&amp;lt; add(int1, dou1) &amp;lt;&amp;lt; endl;

	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the program above, from line 5, we create the function template.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 12 and 13, we define an integer and a double variable respectively.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 16,&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;cout &amp;lt;&amp;lt; add&amp;lt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt;, &lt;span style=&quot;color: #00aaaa;&quot;&gt;double&lt;/span&gt;&amp;gt;(int1, dou1) &amp;lt;&amp;lt; endl;
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;We call the function by passing the integer and double variable as arguments and explicitly specifying the data types (&lt;i&gt;&amp;lt;int, double&amp;gt;&lt;/i&gt;). Here we have to specify both the data types separated by a comma in the angular brackets.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 18, we do not specify the data types hence the compiler will deduce the data types from the arguments passed.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;So in this way we can use one function template for different data types.&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://letspracticeprogramming.blogspot.com/feeds/5715131945124633689/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/02/function-templates.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/5715131945124633689'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/5715131945124633689'/><link rel='alternate' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/02/function-templates.html' title='Function Templates In C++'/><author><name>Saurabh</name><uri>http://www.blogger.com/profile/03890363916503088023</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX0fmK91PXdZkLfX2Yqloq9Q01aP6rj7zRV84uOzbV0el--Ouu7Kn7yoGCc7GolJ2UAFSFu65I0rpoVZR4spxiVq45lkwWFp6B-NrKPfQRhAj2hgU4QSn1neTdtxXktq4/s220/Screenshot_20210111_231427.JPG'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB62tECJG153sNjXBAiUUlNTL2eh1m1CgJO_Y003lvnrdYbApywUCNqpiYRZCIP5HHYRZywYKZh9TLtk3A9WnOQd-lQZakLyFcMFkzoisttBpUSy7uNtpPKwYI9xMRtvb1TIVe_zuypI8/s72-w561-h275-c/Function+Templates+In+C%252B%252B.png" height="72" width="72"/><thr:total>0</thr:total><georss:featurename>Jaripatka, Nagpur, Maharashtra 440014, India</georss:featurename><georss:point>21.1820557 79.0859952</georss:point><georss:box>-7.1281781361788461 43.9297452 49.492289536178845 114.2422452</georss:box></entry><entry><id>tag:blogger.com,1999:blog-4094940111921452231.post-7534911912572649347</id><published>2021-01-29T13:56:00.012+05:30</published><updated>2021-02-07T13:51:18.257+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Classes and Objects"/><title type='text'>Classes &amp; Objects In C++</title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5mok6zzPEG5unw2je2KLEZE441NFRRrT8wTLkQOFFRU8THJxvzdwb2z5OnEaWbYlbBGsKLBJcMKhTDWsL_kwTIsfC4zXADCoEJEaCsjepCDepQ2dVV4eQu70cQymbNZsn_CslF_irmss/s1033/classes+%2526+objects+in+C%252B%252B.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Classes and objects in C++&quot; border=&quot;0&quot; data-original-height=&quot;506&quot; data-original-width=&quot;1033&quot; height=&quot;229&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5mok6zzPEG5unw2je2KLEZE441NFRRrT8wTLkQOFFRU8THJxvzdwb2z5OnEaWbYlbBGsKLBJcMKhTDWsL_kwTIsfC4zXADCoEJEaCsjepCDepQ2dVV4eQu70cQymbNZsn_CslF_irmss/w467-h229/classes+%2526+objects+in+C%252B%252B.png&quot; title=&quot;Classes and objects in C++&quot; width=&quot;467&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;C++ is an object oriented programming language. Classes and objects are the way through which it implements its &quot;object orientedness&quot;.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Classes are used to make user defined data types which we can use in our programs.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;A class generally contains data members and member functions.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;A class is a blueprint from which any number of objects (which have certain attributes and functions) can be made.&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;Data members (variables of class) are the attributes of the objects and member functions (functions declared / defined in the class) are the functions which define the behavior of the object.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Defining a Class&lt;/span&gt;&lt;/h3&gt;&lt;p&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;when a class is defined no memory is allocated. Memory is allocated for the object we create.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;A class is used to make an object which resembles to real life objects. Following is a basic example of a class.&lt;/span&gt;&lt;/p&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// person.h&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#pragma once&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;
&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;person&lt;/span&gt; {	    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a class named &quot;person&quot;&lt;/span&gt;
public:			&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// public access specifier&lt;/span&gt;
	string name;		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// two string variables &lt;/span&gt;
	string gender;		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// which are the attributes of the objects of this class.&lt;/span&gt;

	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;speak&lt;/span&gt;();	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// declaring the function which defines the behavior of the object&lt;/span&gt;
	
};

&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; person::speak() {	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining &quot;speak()&quot; function&lt;/span&gt;
		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// prints the name and gender of the person.&lt;/span&gt;
	cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;My name is &quot;&lt;/span&gt; &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot; and I am a &quot;&lt;/span&gt; &amp;lt;&amp;lt; gender &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;.&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Above is the &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/header-files-in-cpp.html&quot; style=&quot;font-weight: bold;&quot; target=&quot;_blank&quot;&gt;header file&lt;/a&gt;&amp;nbsp;in which, from line 4, we define a class named &lt;i&gt;person&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Class definition starts with the keyword &lt;i&gt;class&lt;/i&gt;. On line 5, we specify the &lt;i&gt;public&lt;/i&gt; &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/access-specifiers-in-cpp.html&quot; style=&quot;font-weight: bold;&quot; target=&quot;_blank&quot;&gt;access_specifier&lt;/a&gt;&amp;nbsp;and on line 6 and 7, (l&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;ike a person has a name and a gender)&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;we define two string variables, &lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;name&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;and &lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;gender&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;(attributes of the objects) which will respectively indicate the name and gender of the object created from the class &lt;i&gt;person&lt;/i&gt;, simply said, when we create a &lt;i&gt;person&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;And as a person can speak, from line 9, we declare a function named &lt;i&gt;speak()&lt;/i&gt;. This function&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;does not return anything (void function) and does not take any parameters.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;speak&lt;/span&gt;();	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// declaring the function which defines the behavior of the object&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;We define the &lt;i&gt;speak()&lt;/i&gt;&amp;nbsp;function outside the class, we did that by using the scope operator (::). On line 13, We first wrote the return type of the function, then the class name followed by the scope operator to access the member functions of the class, then comes the function body.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; person::speak() {		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a speak() function&lt;/span&gt;
		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// prints the name and gender of the person.&lt;/span&gt;
	cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;My name is &quot;&lt;/span&gt; &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot; and I am a &quot;&lt;/span&gt; &amp;lt;&amp;lt; gender &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;.&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
}
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;The function prints the value of the&amp;nbsp;&lt;i&gt;name&lt;/i&gt;&amp;nbsp;and &lt;i&gt;gender&lt;/i&gt;&amp;nbsp;variables as if a person tells his/her name and gender.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above code we declared the &lt;i&gt;speak()&lt;/i&gt;&amp;nbsp;function inside the class and defined that function outside the class. If we were to directly define the &lt;i&gt;speak()&lt;/i&gt;&amp;nbsp;function inside the class, the code would have been like below.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// person_1.h&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#pragma once&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;
&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;person&lt;/span&gt; {			&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a class named &quot;person&quot;&lt;/span&gt;
public:				&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// public access specifier&lt;/span&gt;
	string name;		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// two string variables &lt;/span&gt;
	string gender;		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// which are the attributes of the objects of this class.&lt;/span&gt;

	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;speak&lt;/span&gt;() {		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a speak() function&lt;/span&gt;
			&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// prints the name and gender of the person.&lt;/span&gt;
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;My name is &quot;&lt;/span&gt; &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot; and I am a &quot;&lt;/span&gt; &amp;lt;&amp;lt; gender &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;.&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
	}

};
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;



&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Above is the &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/header-files-in-cpp.html&quot; target=&quot;_blank&quot;&gt;header file&lt;/a&gt;&amp;nbsp;in which we have directly defined the &lt;i&gt;speak()&lt;/i&gt;&amp;nbsp;function in the class from line 9.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;speak&lt;/span&gt;() {		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a speak() function&lt;/span&gt;
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// prints the name and gender of the person.&lt;/span&gt;
	cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;My name is &quot;&lt;/span&gt; &amp;lt;&amp;lt; name &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot; and I am a &quot;&lt;/span&gt; &amp;lt;&amp;lt; gender &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;.&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
}
&lt;/pre&gt;&lt;/div&gt;


&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Here we do not need to write the class name and the scope operator (&lt;i&gt;::&lt;/i&gt;) before the function name.&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Creating Objects from Class&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Now, every time we create an object from a class, each object will have it&#39;s own copy of class members, in this case each object of the class &lt;i&gt;person&lt;/i&gt;&amp;nbsp;will have the&amp;nbsp;&lt;i&gt;name&lt;/i&gt;&amp;nbsp;and &lt;i&gt;gender&lt;/i&gt;&amp;nbsp;variables and also the &lt;i&gt;speak()&lt;/i&gt;&amp;nbsp;function.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// program.cpp&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include &quot;person.h&quot;&lt;/span&gt;

&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;main&lt;/span&gt;() {
	person obj1;           &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// creating a person&lt;/span&gt;
	obj1.name = &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Jack&quot;&lt;/span&gt;;   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// assigning value to string variable &quot;name&quot;&lt;/span&gt;
	obj1.gender = &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Male&quot;&lt;/span&gt;;	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// assigning value to string variable &quot;gender&quot; &lt;/span&gt;
	obj1.speak();		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// calling the speak function&lt;/span&gt;

	person obj2;		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// same process for another person&lt;/span&gt;
	obj2.name = &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Hallie&quot;&lt;/span&gt;;
	obj2.gender = &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Female&quot;&lt;/span&gt;;
	obj2.speak();

	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;To create an object, we specify the class name and the name for the object to be created. In the above code, on line 8, we created an object named &lt;i&gt;obj1&lt;/i&gt;&amp;nbsp;of the class &lt;i&gt;person&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;As in the class, the string variables are public, so we can access those string variables through the object &lt;i&gt;obj1&lt;/i&gt;&amp;nbsp;by using the dot operator (.).&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;obj1.name = &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Jack&quot;&lt;/span&gt;;        &lt;i&gt;&lt;span style=&quot;color: #999999;&quot;&gt;// assigning value to string variable &quot;name&quot;&lt;/span&gt;&lt;/i&gt;
obj1.gender = &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Male&quot;&lt;/span&gt;;      &lt;i&gt;&lt;span style=&quot;color: #999999;&quot;&gt;// assigning value to string variable &quot;gender&quot;&lt;/span&gt;&lt;/i&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;We assigned the value &quot;&lt;i&gt;Jack&lt;/i&gt;&quot; to &lt;i&gt;name&lt;/i&gt;&amp;nbsp;variable and &quot;&lt;i&gt;Male&lt;/i&gt;&quot; to &lt;i&gt;gender&lt;/i&gt;&amp;nbsp;variable and on line 11,&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;obj1.speak();	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// calling the speak function&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;we call the &lt;i&gt;speak()&lt;/i&gt;&amp;nbsp;function which prints the value of both the string variables of the class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Similarly on line 13, we create another object named &lt;i&gt;obj2&lt;/i&gt;, and also assign values to the &lt;i&gt;name&lt;/i&gt;&amp;nbsp;and &lt;i&gt;gender&lt;/i&gt;&amp;nbsp;variable and then call the &lt;i&gt;speak()&lt;/i&gt;&amp;nbsp;function for &lt;i&gt;obj2&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;We can create as many objects we want and all will have their own attributes and functions.&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://letspracticeprogramming.blogspot.com/feeds/7534911912572649347/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/01/classes-and-objects-in-cpp.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/7534911912572649347'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/7534911912572649347'/><link rel='alternate' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/01/classes-and-objects-in-cpp.html' title='Classes &amp; Objects In C++'/><author><name>Saurabh</name><uri>http://www.blogger.com/profile/03890363916503088023</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX0fmK91PXdZkLfX2Yqloq9Q01aP6rj7zRV84uOzbV0el--Ouu7Kn7yoGCc7GolJ2UAFSFu65I0rpoVZR4spxiVq45lkwWFp6B-NrKPfQRhAj2hgU4QSn1neTdtxXktq4/s220/Screenshot_20210111_231427.JPG'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5mok6zzPEG5unw2je2KLEZE441NFRRrT8wTLkQOFFRU8THJxvzdwb2z5OnEaWbYlbBGsKLBJcMKhTDWsL_kwTIsfC4zXADCoEJEaCsjepCDepQ2dVV4eQu70cQymbNZsn_CslF_irmss/s72-w467-h229-c/classes+%2526+objects+in+C%252B%252B.png" height="72" width="72"/><thr:total>0</thr:total><georss:featurename>Jaripatka, Nagpur, Maharashtra 440014, India</georss:featurename><georss:point>21.1820557 79.0859952</georss:point><georss:box>-7.1281781361788461 43.9297452 49.492289536178845 114.2422452</georss:box></entry><entry><id>tag:blogger.com,1999:blog-4094940111921452231.post-8237378060450113228</id><published>2021-01-23T05:07:00.013+05:30</published><updated>2021-02-06T14:59:27.798+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="access specifiers"/><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><title type='text'>Access Specifiers In C++</title><content type='html'>&lt;p&gt;&amp;nbsp;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizH2OXB5MgLll_jnOY9IsVxyX5kdOXytBKMGoUqHYJy1nGlQ8UueM2QCSRqJe4emLXLUOG0OKdNGMlpXI050XK2qu6zlN_Z2cvSqT1m_4-t9sQn3PgW5fxR-9xoiVwixemtNPSx3k1e2Q/s904/Access+specifiers+in+C%252B%252B.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Access Specifiers In C++&quot; border=&quot;0&quot; data-original-height=&quot;453&quot; data-original-width=&quot;904&quot; height=&quot;264&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizH2OXB5MgLll_jnOY9IsVxyX5kdOXytBKMGoUqHYJy1nGlQ8UueM2QCSRqJe4emLXLUOG0OKdNGMlpXI050XK2qu6zlN_Z2cvSqT1m_4-t9sQn3PgW5fxR-9xoiVwixemtNPSx3k1e2Q/w528-h264/Access+specifiers+in+C%252B%252B.png&quot; title=&quot;Access Specifiers In C++&quot; width=&quot;528&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;There are 3 access specifiers which determine the accessibility&amp;nbsp;/ visibility of the &lt;b&gt;&lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/classes-and-objects-in-cpp.html&quot; target=&quot;_blank&quot;&gt;class&lt;/a&gt;&lt;/b&gt; members.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Access Specifiers&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;b&gt;1) public&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;b&gt;2) private&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;b&gt;3) protected&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Let&#39;s take a look at an example.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// access_specifier.h&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#pragma once&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;
&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;base&lt;/span&gt; {
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// public members of the base class&lt;/span&gt;
public:
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; public_func() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Public member function in the base class.\n&quot;&lt;/span&gt;;
	}
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// protected members of the base class&lt;/span&gt;
protected:
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; protected_func() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Protected member function in the base class.\n&quot;&lt;/span&gt;;
	}
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// private members of the base class&lt;/span&gt;
private:
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; private_func() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Private member function in the base class.\n&quot;&lt;/span&gt;;
	}
};
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/header-files-in-cpp.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;header file&lt;/b&gt;&lt;/a&gt;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;, we have defined a &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/classes-and-objects-in-cpp.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;class&lt;/b&gt;&lt;/a&gt; named &lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;base.&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;on line 6, we specified the &quot;public&quot; access specifier and under it, we defined a public function named &lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;public_func()&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 11, we specified the &quot;protected&quot; access specifier and under it, we defined a protected function named &lt;i&gt;protected_func()&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 16, we specified the &quot;private&quot; access specifier and under it, we defined a private function named &lt;i&gt;private_func().&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;b&gt;Note :- &lt;/b&gt;If we do not specify any access specifier, then by default the members of the class are private.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;




&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// access_specifier.cpp&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// Header file which contains the base and derived class&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include &quot;access_specifier.h&quot;&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;main&lt;/span&gt;() {
	base obj_b;              &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// creating an object of the base class&lt;/span&gt;
	obj_b.public_func();     &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// ok: accessing the public member of the base class&lt;/span&gt;
	obj_b.protected_func();  &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// error: accessing the protected member of the base class&lt;/span&gt;
	obj_b.private_func();    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// error: accessing the private member of the base class&lt;/span&gt;
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above code, on line 8, we made an &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/classes-and-objects-in-cpp.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;object&lt;/b&gt;&lt;/a&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;named&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;obj_b&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;of the &lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;base&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;class&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;.&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;base obj_b;              &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// creating an object of the base class&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 9, we call the public function named&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;i&gt;public_func()&lt;/i&gt;&amp;nbsp;through the &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/classes-and-objects-in-cpp.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;object&lt;/b&gt;&lt;/a&gt; &lt;i&gt;obj_b&lt;/i&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;&quot;&gt;obj_b.public_func();     &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// ok: accessing the public member of the base class&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;But on line 10 and 11,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;obj_b.protected_func();  &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// error: accessing the protected member of the base class&lt;/span&gt;
&lt;/pre&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;obj_b.private_func();&lt;/span&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;  // error: accessing the private member of the base class&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;through the &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/classes-and-objects-in-cpp.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;object&lt;/b&gt;&lt;/a&gt; &lt;i&gt;obj_b&lt;/i&gt;, we try to access the functions &lt;i&gt;protected_func()&lt;/i&gt;&amp;nbsp;and &lt;i&gt;private_func()&lt;/i&gt;, which are defined in the protected and private access specifiers respectively in the &lt;i&gt;base&lt;/i&gt;&amp;nbsp;class, which is an error because the protected members (except by the derived class) and private members of the &lt;i&gt;base&lt;/i&gt;&amp;nbsp;class cannot be accessed by code outside the class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Although we can access the the protected and private members through a public function.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Lets look at an example.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// access_specifier.h&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#pragma once&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;
&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;base&lt;/span&gt; {
public:
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; public_func() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Public member function in the base class.\n&quot;&lt;/span&gt;;
	}
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// public function that has access to the protected and private members of the base class&lt;/span&gt;
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; pro_pri_mem() {
		protected_func();
		private_func();
	}
protected:
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; protected_func() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Protected member function in the base class.\n&quot;&lt;/span&gt;;
	}
private:
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; private_func() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Private member function in the base class.\n&quot;&lt;/span&gt;;
	}
};
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;The members under the class have access to the other members of the class whether they are public, protected or private. So the public members can access the protected and private members of the class. Thus we can call the &lt;i&gt;protected_func()&lt;/i&gt; and &lt;i&gt;private_func()&lt;/i&gt; functions in&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;pro_pri_mem()&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;which is&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;a public function.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// access_specifier.cpp&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// Header file which contains the base and derived class&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include &quot;access_specifier.h&quot;&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;main&lt;/span&gt;() {
	base obj_b;              &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// creating an object of the base class&lt;/span&gt;
	obj_b.public_func();     &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// ok: accessing the public member of the base class&lt;/span&gt;
	obj_b.pro_pri_mem();	 &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// accessing the public function&lt;/span&gt;
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above code on line 10, we call the &lt;i&gt;pro_pri_mem()&lt;/i&gt;&amp;nbsp;function which calls the protected and private functions of the class.&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Inheritance and Access control&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;The access specifiers also play a role in inheritance (a class&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;derived&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;from a base class). They decide&amp;nbsp;the visibility mode of the base class members for the derived class. A &quot;derived class&quot; can be inherited from &quot;public&quot;, &quot;protected&quot; or &quot;private&quot; base class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;When a derived class is inherited from a public base class, the public and protected members of the base class are respectively the public and protected members of the derived class. The derived class cannot access the private members of the base class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;When a derived class is inherited from a protected base class, the public members and the protected members of the base class, both are protected members of the derived class. Here also, private members of the base class are not accessible in the derived class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Finally if the derived class is inherited from a private base class, the public and protected members of the base class are private members of the derived class and the&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;private members of the base class are not accessible in the derived class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Let&#39;s see an example,&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// access_specifier.h&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#pragma once&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;
&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;base&lt;/span&gt; {
public:
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; public_func() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Public member function in the base class.\n&quot;&lt;/span&gt;;
	}
protected:
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; protected_func() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Protected member function in the base class.\n&quot;&lt;/span&gt;;
	}
private:
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; private_func() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Private member function in the base class.\n&quot;&lt;/span&gt;;
	}
};
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// inheriting a derived class from a public base class&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;derived_1&lt;/span&gt; : &lt;span style=&quot;color: #0000aa;&quot;&gt;public&lt;/span&gt; base {
public:
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; der_public_func_1() {    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining public function&lt;/span&gt;
		public_func();	      &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// derived class can access public members of the base class&lt;/span&gt;
		protected_func();     &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// derived class can access protected members of the base class&lt;/span&gt;
	        private_func();	      &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// error: derived class cannot access private members of the base class&lt;/span&gt;
	}
};
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// inheriting a derived class from a protected base class&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;derived_2&lt;/span&gt; : &lt;span style=&quot;color: #0000aa;&quot;&gt;protected&lt;/span&gt; base {
public:
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; der_public_func_2() {    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining public function&lt;/span&gt;
		public_func();	      &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// derived class can access public members of the base class&lt;/span&gt;
		protected_func();     &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// derived class can access protected members of the base class&lt;/span&gt;
	        private_func();	      &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// error: derived class cannot access private members of the base class&lt;/span&gt;
	}
};
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// inheriting a derived class from a private base class&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;derived_3&lt;/span&gt; : &lt;span style=&quot;color: #0000aa;&quot;&gt;private&lt;/span&gt; base {
public:
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; der_public_func_3() {    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining public function&lt;/span&gt;
		public_func();	     &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// derived class can access public members of the base class&lt;/span&gt;
		protected_func();    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// derived class can access protected members of the base class&lt;/span&gt;
	        private_func();	     &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// error: derived class cannot access private members of the base class&lt;/span&gt;
	}
};
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above code we inherited 3 derived classes, on line 19,&amp;nbsp;&lt;i&gt;derived_1&lt;/i&gt;&amp;nbsp;was inherited from the &lt;i&gt;public&lt;/i&gt;&amp;nbsp;base class,&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;derived_1&lt;/span&gt; : &lt;span style=&quot;color: #0000aa;&quot;&gt;public&lt;/span&gt; base {
&lt;/pre&gt;&lt;/div&gt;


&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;on line 28 &lt;i&gt;derived_2&lt;/i&gt;&amp;nbsp;was derived from the protected base class&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;derived_2&lt;/span&gt; : &lt;span style=&quot;color: #0000aa;&quot;&gt;protected&lt;/span&gt; base {
&lt;/pre&gt;&lt;/div&gt;


&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;and on line 37, &lt;i&gt;derived_3&lt;/i&gt;&amp;nbsp;was inherited from the private base class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #00aa00; text-decoration: underline;&quot;&gt;derived_3&lt;/span&gt; : &lt;span style=&quot;color: #0000aa;&quot;&gt;private&lt;/span&gt; base {
&lt;/pre&gt;&lt;/div&gt;


&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In each of the derived classes we define a public function in which we try to access the members inherited from the base class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Since class&amp;nbsp;&lt;i&gt;derived_1&lt;/i&gt;&amp;nbsp;is inherited from a public base class, &lt;i&gt;public_func()&lt;/i&gt; and &lt;i&gt;protected_func()&lt;/i&gt;&amp;nbsp;function members of the base class are also public and protected respectively in &lt;i&gt;derived_1&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;i&gt;derived_2&lt;/i&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;is inherited from a protected base class, so both&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;public_func()&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;protected_func()&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;function&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;members&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;of the base class are protected in&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;derived_2&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;And&amp;nbsp;&lt;i&gt;derived_3&lt;/i&gt;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;is inherited from a private base class, so both the&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;public_func()&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;and&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;protected_func()&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;function&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;members&amp;nbsp;of the base&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;class are private in&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;derived_3&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;The derived classes cannot access the &lt;i&gt;private_func()&lt;/i&gt; function member of the base class since it is a private member.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Let&#39;s see an example of this.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// access_specifier.cpp&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include &quot;access_specifier.h&quot;&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;main&lt;/span&gt;() {
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// object of the derived class inherited from public base class&lt;/span&gt;
	derived_1 derived_obj_1;		 
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// object of the derived class inherited from protected base class&lt;/span&gt;
	derived_2 derived_obj_2;		 
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// object of the derived class inherited from private base class&lt;/span&gt;
	derived_3 derived_obj_3;		 

	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// public_func() is public as the derived class is inherited from the public base class&lt;/span&gt;
	derived_obj_1.public_func();	 
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// error: public_func() is protected as the derived class is inherited from the protected base class&lt;/span&gt;
	derived_obj_2.public_func();	 
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// error: public_func() is private as the derived class is inherited from the private base class&lt;/span&gt;
	derived_obj_3.public_func();	 

	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above code, on line 8, we created an object of class&amp;nbsp;&lt;i&gt;derived_1&lt;/i&gt;&amp;nbsp;which is inherited from public base class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;derived_1 derived_obj_1;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 10,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;we created an object of class&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;derived_2&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;which is inherited from protected base class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;derived_2 derived_obj_2;		 
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;And on line 12,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;we created an object of class&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;derived_3&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;which is inherited from private base class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;derived_3 derived_obj_3;		 
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Now on line 15, we try to access &lt;i&gt;public_func()&lt;/i&gt;&amp;nbsp;through&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;i&gt;derived_obj_1&lt;/i&gt;&amp;nbsp;which is an object of class &lt;i&gt;derived_1&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;derived_obj_1.public_func();	 
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Here &lt;i&gt;public_func()&lt;/i&gt;&amp;nbsp;is a public member of the class &lt;i&gt;derived_1&lt;/i&gt;&amp;nbsp;as it is inherited from the public base class.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;But on line 17,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;we try to access&amp;nbsp;&lt;i&gt;public_func()&lt;/i&gt;&amp;nbsp;through&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;i&gt;derived_obj_2&lt;/i&gt;&amp;nbsp;which is an object of class&amp;nbsp;&lt;i&gt;derived_2&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;derived_obj_2.public_func();
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;It is an error because here &lt;i&gt;public_func()&lt;/i&gt;&amp;nbsp;is protected member of class &lt;i&gt;derived_2&lt;/i&gt;&amp;nbsp;as it is inherited from the protected base class.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Likewise on line 19, we try to access&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;&lt;i&gt;public_func()&lt;/i&gt;&amp;nbsp;through&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;i&gt;derived_obj_3&lt;/i&gt;&amp;nbsp;which is an object of class&amp;nbsp;&lt;i&gt;derived_3&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;derived_obj_3.public_func();
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;It is also an error because here&amp;nbsp;&lt;i&gt;public_func()&lt;/i&gt;&amp;nbsp;is private member of class&amp;nbsp;&lt;i&gt;derived_3&lt;/i&gt;&amp;nbsp;as it is inherited from the private base class.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://letspracticeprogramming.blogspot.com/feeds/8237378060450113228/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/01/access-specifiers-in-cpp.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/8237378060450113228'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/8237378060450113228'/><link rel='alternate' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/01/access-specifiers-in-cpp.html' title='Access Specifiers In C++'/><author><name>Saurabh</name><uri>http://www.blogger.com/profile/03890363916503088023</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX0fmK91PXdZkLfX2Yqloq9Q01aP6rj7zRV84uOzbV0el--Ouu7Kn7yoGCc7GolJ2UAFSFu65I0rpoVZR4spxiVq45lkwWFp6B-NrKPfQRhAj2hgU4QSn1neTdtxXktq4/s220/Screenshot_20210111_231427.JPG'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizH2OXB5MgLll_jnOY9IsVxyX5kdOXytBKMGoUqHYJy1nGlQ8UueM2QCSRqJe4emLXLUOG0OKdNGMlpXI050XK2qu6zlN_Z2cvSqT1m_4-t9sQn3PgW5fxR-9xoiVwixemtNPSx3k1e2Q/s72-w528-h264-c/Access+specifiers+in+C%252B%252B.png" height="72" width="72"/><thr:total>0</thr:total><georss:featurename>Jaripatka, Nagpur, Maharashtra 440014, India</georss:featurename><georss:point>21.1820557 79.0859952</georss:point><georss:box>-7.1281781361788461 43.9297452 49.492289536178845 114.2422452</georss:box></entry><entry><id>tag:blogger.com,1999:blog-4094940111921452231.post-3329621995157941907</id><published>2021-01-18T16:00:00.008+05:30</published><updated>2021-02-03T20:33:28.684+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Namespaces in C++"/><title type='text'>Namespaces in C++</title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRJwoaUPTUFwULZqlPrHmnSr1we3nm1m-iGIknv1PGiJwQf8rF3giVNV3MNTx_611ZLBG-f1Cj6KQV7XSAcnuv3kN7ZQwXFLuAnoK-YsWh3OWoAnfWIwNSbTIly_2iBGKPvH6bkNuqMvo/s791/Namespaces+in+C%252B%252B.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Namespaces in C++&quot; border=&quot;0&quot; data-original-height=&quot;367&quot; data-original-width=&quot;791&quot; height=&quot;296&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRJwoaUPTUFwULZqlPrHmnSr1we3nm1m-iGIknv1PGiJwQf8rF3giVNV3MNTx_611ZLBG-f1Cj6KQV7XSAcnuv3kN7ZQwXFLuAnoK-YsWh3OWoAnfWIwNSbTIly_2iBGKPvH6bkNuqMvo/w640-h296/Namespaces+in+C%252B%252B.png&quot; title=&quot;Namespaces in C++&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;A namespace provides a scope in which its members (variables, functions, &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/classes-and-objects-in-cpp.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;classes&lt;/b&gt;&lt;/a&gt; etc.) are declared/defined. C++ has many libraries and their might be a situation where one identifier&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;(name of the variables, functions, &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/classes-and-objects-in-cpp.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;classes&lt;/b&gt;&lt;/a&gt; etc.)&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;in one library is same as another identifier in another library. Hence to avoid the name conflicts, we use namespaces.&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Defining Namespaces&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Lets take a basic example of a namespace.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#pragma once&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// Name of the namespace is demo&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; demo {

	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a function named greet&lt;/span&gt;
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; greet() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Hello&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
	}

	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining a function named farewell&lt;/span&gt;
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; farewell() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Good Bye&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
	}
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Above is the &lt;a href=&quot;https://letspracticeprogramming.blogspot.com/2021/01/header-files-in-cpp.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;header file&lt;/b&gt;&lt;/a&gt;&amp;nbsp;in which we have defined a namespace named &lt;i&gt;demo.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Namespace declaration starts with the keyword &lt;i&gt;namespace&lt;/i&gt;&amp;nbsp;and then the namespace name, in this case &lt;i&gt;demo&lt;/i&gt;. In this namespace, we have defined two functions, first is &lt;i&gt;greet()&lt;/i&gt;, which says &quot;&lt;i&gt;Hello&quot;&lt;/i&gt;, and second is &lt;i&gt;farewell()&lt;/i&gt;, which says &quot;&lt;i&gt;Good Bye&quot;.&amp;nbsp;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Suppose we hadn&#39;t defined the function in the namespace and their is another function named &lt;i&gt;greet()&lt;/i&gt;&amp;nbsp;in the huge C++ library (collection of code which make up the language), there would have been a name conflict, therefore we defined this function in namespace &lt;i&gt;demo.&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;There are 3 ways through which we can access the members of a namespace&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;b&gt;1. &lt;/b&gt;Using the&lt;b&gt; fully qualified name&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// including the header file which has the namespace&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include &quot;namespaces.h&quot;&lt;/span&gt;

&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;main&lt;/span&gt;() {
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// using a qualified name to access the members of the namespace&lt;/span&gt;
	demo::greet();
	demo::farewell();
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;


&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above example, we use the qualified name to access the member function of the &lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;demo&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;namespace, on line 9 and 10.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;demo::greet();
demo::farewell();
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;A fully qualified name is when we write the namespace name followed by the scope operator (::) followed by the name of the member you want to access. This way we have qualified access to the members of the namespace.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;b&gt;2. &lt;/b&gt;Using the&lt;b&gt; &quot;using&quot; declaration &lt;/b&gt;to bring an individual&amp;nbsp;identifier into scope.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// including the header file which has the namespace&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include &quot;namespaces.h&quot;&lt;/span&gt;

&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;main&lt;/span&gt;() {
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// using the &quot;using&quot; declaration to bring&lt;/span&gt;
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// individual members into scope&lt;/span&gt;
	&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; demo::greet;
	&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; demo::farewell;

	greet();
	farewell();
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;




&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above example on line 10 and 11,&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; demo::greet;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; demo::farewell;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;We brought both the member functions into scope individually, using the &quot;using&quot; declaration. This way is useful when we need one or two members of a namespace.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;b&gt;3. &lt;/b&gt;Using the&lt;b&gt; &quot;using&quot; directive.&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// including the header file which has the namespace&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include &quot;namespaces.h&quot;&lt;/span&gt;

&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// using the &quot;using&quot; directive&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; demo;
&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;main&lt;/span&gt;() {

	greet();
	farewell();
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above example, we used the &quot;using&quot; directive on line 7, which will bring all the members of the namespace into scope. This should be used when we use many of the namespace members.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;We can define a namespace in multiple blocks in the same file and within multiple files, for example&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// namespace_header.h&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#pragma once&lt;/span&gt;

&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; demo {		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining first block of namespace demo&lt;/span&gt;
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; greet() {
		std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Hello&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
	}
}
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;/*&lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;* &lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;* some code&lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;* &lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;*/&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; demo {		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// definition continues in the second block&lt;/span&gt;
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; farewell() {
		std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Good Bye&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
	}
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above example the namespace demo is divided in two blocks and likewise it&#39;s parts can be defined in multiple files. Same is with namespace std, it is defined in many blocks in many header files in the C++ library.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Members declared in the namespaces can be defined outside the namespace through explicit qualification. But the definition of the member should come after the point of their declaration in the namespace.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#pragma once&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;
&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; demo {
    
    &lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; greet();
}

&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// defining the declared function&lt;/span&gt;
&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; demo::greet() { cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Hello&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl; } 
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// error: farewell() is not a member of demo yet&lt;/span&gt;
&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; demo::farewell() { cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Goodbye&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl; }

&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; demo {
    &lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; farewell(); &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// declaring the member function&lt;/span&gt;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;


&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above example, function &lt;i&gt;greet()&lt;/i&gt;, which&amp;nbsp;is declared in the first block of namespace &lt;i&gt;demo&lt;/i&gt;, is defined outside the namespace definition through explicit qualification. And on line 11, the function &lt;i&gt;farewell()&lt;/i&gt;&amp;nbsp;is defined through explicit qualification but the problem is at that point the function &lt;i&gt;farewell()&lt;/i&gt;&amp;nbsp;is not the part of the namespace demo, it is declared in the namespace &lt;i&gt;demo&lt;/i&gt;, in its second block on line 14. So that becomes an error. The declaration and definition should be the correct order.&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;b&gt;Global namespaces&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;An identifier which is not declared or defined in an any namespace, it is the part of the global namespace. Global namespace identifiers can be qualified by appending only the scope operator and no namespace name.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; int1 = &lt;span style=&quot;color: #009999;&quot;&gt;1&lt;/span&gt;;	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// variable defined in global namespace&lt;/span&gt;

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;main&lt;/span&gt;() {
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// local variable in the main function&lt;/span&gt;
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; int1 = &lt;span style=&quot;color: #009999;&quot;&gt;2&lt;/span&gt;;
	
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// using the scope operator to access the global identifier&lt;/span&gt;
	::int1 = &lt;span style=&quot;color: #009999;&quot;&gt;2&lt;/span&gt;;

	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above example, on line 4, a global variable &lt;i&gt;int1&amp;nbsp;&lt;/i&gt;is defined. Also in the main function, a variable named &lt;i&gt;int1&amp;nbsp;&lt;/i&gt;is defined. Because the names are same, the global variable &lt;i&gt;int1 &lt;/i&gt;is hidden in the main function, to use the global variable &lt;i&gt;int1, &lt;/i&gt;we need to qualify the&amp;nbsp; name&amp;nbsp; being used that is, appending the scope operator (::) to the member name, there is no need of a namespace name. On line 11 we used a fully qualified name to access the global variable &lt;i&gt;int1&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;span style=&quot;font-size: small; font-weight: normal;&quot;&gt;Let&#39;s look at&lt;/span&gt; &lt;span style=&quot;font-size: x-large;&quot;&gt;Nested Namespaces&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Nested namespace is a namespaces under a namespace. The code under parent namespace do not has unqualified access to child namespace&#39;s members, but the code in the child namespace has unqualified access (no need to append the namespace name and the scope operator to the members to be accessed) to the members of the parent namespace.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Below is an example.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#pragma once&lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// parent namespace&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; parent {
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; func1() {			&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// function defined in parent namespace&lt;/span&gt;
		std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Welcome to the parent namespace THROUGH THE CHILD NAMESPACE.&quot;&lt;/span&gt;&amp;lt;&amp;lt; std::endl;
	}
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// child namespace&lt;/span&gt;
	&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; child {
		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// function defined in the child namespace&lt;/span&gt;
		&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; func2() {
			&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// returns the result of a call to the function in the parent namespace&lt;/span&gt;
			&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; func1();	
		}
		&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; func3() {		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// Another function in the child namespace&lt;/span&gt;
			std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;This is the child namespace THROUGH THE PARENT NAMESPACE.&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
		}
	}
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; func4() {		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// another function in the parent namespace&lt;/span&gt;
		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// have to use the scope operator to access the child namespace members&lt;/span&gt;
		&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; child::func3();
	}
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Namespace definition starts from line 3, that is the &lt;i&gt;parent&lt;/i&gt; namespace, in which we defined function &lt;i&gt;func1.&lt;/i&gt;&amp;nbsp;From&amp;nbsp;line 8, a namespace named &lt;i&gt;child&lt;/i&gt; is defined.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the &lt;i&gt;child &lt;/i&gt;namespace a function named&amp;nbsp;&lt;i&gt;func2 &lt;/i&gt;is defined, which returns the result of the call to the function &lt;i&gt;func1&lt;/i&gt;,&lt;i&gt;&amp;nbsp;&lt;/i&gt;which is defined in the &lt;i&gt;parent&lt;/i&gt; namespace. Here the code in the&amp;nbsp;&lt;i&gt;child &lt;/i&gt;namespace has unqualified access to the members of the &lt;i&gt;parent&lt;/i&gt;&amp;nbsp;namespace.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;From line 14, function &lt;i&gt;func3&lt;/i&gt;&amp;nbsp;is defined, on line 17 &lt;i&gt;child &lt;/i&gt;namespace definition completes. Now we are in the &lt;i&gt;parent&lt;/i&gt;&amp;nbsp;namespace, on line 18 function named &lt;i&gt;func4&lt;/i&gt;&amp;nbsp;is defined which returns the result of the call to the function &lt;i&gt;func3&lt;/i&gt;, which is defined in the &lt;i&gt;child&lt;/i&gt;&amp;nbsp;namespace. Here we have to use the qualified name to access the members of the &lt;i&gt;child &lt;/i&gt;namespace.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;There is a case when the members of the &lt;i&gt;child&lt;/i&gt;&amp;nbsp;namespace can be accessed without using the qualified name, that is when the &lt;i&gt;child &lt;/i&gt;namespace is defined as &lt;b&gt;inline.&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;&lt;b&gt;Inline Namespaces&lt;/b&gt;&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Following is the example of an inline nested namespace. The below example is same as the above example except on line 8, the &lt;i&gt;child&lt;/i&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#pragma once&lt;/span&gt;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// parent namespace&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; parent {
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; func1() {			&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// function defined in parent namespace&lt;/span&gt;
		std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Welcome to the parent namespace THROUGH THE CHILD NAMESPACE.&quot;&lt;/span&gt;&amp;lt;&amp;lt; std::endl;
	}
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// child namespace defined as inline&lt;/span&gt;
	 &lt;span style=&quot;color: #0000aa;&quot;&gt;inline&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; child {
		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// function defined in the child namespace&lt;/span&gt;
		&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; func2() {
			&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// returns the result of a call to the function in the parent namespace&lt;/span&gt;
			&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; func1();	
		}
		&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; func3() {		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// Another function in the child namespace&lt;/span&gt;
			std::cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;This is the child namespace THROUGH THE PARENT NAMESPACE.&quot;&lt;/span&gt; &amp;lt;&amp;lt; std::endl;
		}
	}
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; func4() {		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// another function in the parent namespace&lt;/span&gt;
		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// we don&#39;t have to use the scope operator to access the child namespace members&lt;/span&gt;
		&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; func3();
	}
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;namespace is defined as &lt;i&gt;inline&lt;/i&gt;, and on line 20, the namespace name and the scope operator is gone, as the &lt;i&gt;child&lt;/i&gt;&amp;nbsp;namespace is defined as &lt;i&gt;inline&lt;/i&gt;, the code in the &lt;i&gt;parent&lt;/i&gt;&amp;nbsp;namespace has unqualified access to the members of the &lt;i&gt;child&lt;/i&gt;&amp;nbsp;namespace.&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Declaring and defining namespace members&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;One thing to keep in mind while &quot;declaring&quot; members in a namespace is when you define that very member at a different place you need to provide a fully qualified name even if you use the &quot;using&quot; directive.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Below is an example.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;1 
2
3
4
5&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#pragma once&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; demo {
	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// just declaring the function greet&lt;/span&gt;
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; greet();
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In above code, we have just declared a function in a namespace in a header file.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include &quot;namespace_header.h&quot;&lt;/span&gt;

&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// using directive to use the namespace demo&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; demo;

&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// To define the function we need a qualified name&lt;/span&gt;
&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; demo::greet() {
	cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Hello&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
}

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; main() {
	greet();
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above example, on line 6 we using the &quot;using&quot; directive to bring into scope every thing in the namespace &lt;i&gt;demo&lt;/i&gt;, despite that we have to use the qualified name to define the function &lt;i&gt;greet()&lt;/i&gt; which was declared in namespace &lt;i&gt;demo&lt;/i&gt;. If we didn&#39;t do this, the function &lt;i&gt;greet()&lt;/i&gt;, will be an individual function having global scope and the compiler is confused between two overloaded functions (functions with same name), the first one is declared in the namespace and the second is the defined in the global namespace in the cpp file which is an error.&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;span style=&quot;font-size: x-large;&quot;&gt;&lt;b&gt;Namespace Aliases&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// namespace with a very big name&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; this_is_the_namespace_name {
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; greet() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Hello&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
	}
}

&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// alias for the big name&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; NSName = this_is_the_namespace_name;

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;main&lt;/span&gt;() {
	NSName::greet();
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above example, namespace&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;i&gt;this_is_the_namespace_name&lt;/i&gt;&amp;nbsp;is defined, as it is a very big name, on line 12 we made an alias for the namespace name.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; NSName = this_is_the_namespace_name;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Now instead of&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;i&gt;this_is_the_namespace_name&lt;/i&gt;&amp;nbsp;we can write &lt;i&gt;NSName&lt;/i&gt;. On line 15 we used the alias name to access the &lt;i&gt;greet&lt;/i&gt;&amp;nbsp;function.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Anonymous / unnamed namespaces&lt;/span&gt;&lt;/h3&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;A namespace without a name is called anonymous or unnamed namespace. The anonymous namespace or its members are not visible to the code outside the file in which it is defined.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// namespace with no name&lt;/span&gt;
&lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt;  {
	&lt;span style=&quot;color: #00aaaa;&quot;&gt;void&lt;/span&gt; greet() {
		cout &amp;lt;&amp;lt; &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;Hello&quot;&lt;/span&gt; &amp;lt;&amp;lt; endl;
	}
}

&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; main() {
	greet();
	&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above example, on line 5, their is no name for the namespace and also we don&#39;t need the qualified access to the members.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;References :-&amp;nbsp;&lt;a href=&quot;https://docs.microsoft.com/en-us/cpp/cpp/namespaces-cpp&quot;&gt;https://docs.microsoft.com/en-us/cpp/cpp/namespaces-cpp&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://letspracticeprogramming.blogspot.com/feeds/3329621995157941907/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/01/namespaces-in-cpp.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/3329621995157941907'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/3329621995157941907'/><link rel='alternate' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/01/namespaces-in-cpp.html' title='Namespaces in C++'/><author><name>Saurabh</name><uri>http://www.blogger.com/profile/03890363916503088023</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX0fmK91PXdZkLfX2Yqloq9Q01aP6rj7zRV84uOzbV0el--Ouu7Kn7yoGCc7GolJ2UAFSFu65I0rpoVZR4spxiVq45lkwWFp6B-NrKPfQRhAj2hgU4QSn1neTdtxXktq4/s220/Screenshot_20210111_231427.JPG'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRJwoaUPTUFwULZqlPrHmnSr1we3nm1m-iGIknv1PGiJwQf8rF3giVNV3MNTx_611ZLBG-f1Cj6KQV7XSAcnuv3kN7ZQwXFLuAnoK-YsWh3OWoAnfWIwNSbTIly_2iBGKPvH6bkNuqMvo/s72-w640-h296-c/Namespaces+in+C%252B%252B.png" height="72" width="72"/><thr:total>0</thr:total><georss:featurename>Jaripatka, Nagpur, Maharashtra 440014, India</georss:featurename><georss:point>21.1820557 79.0859952</georss:point><georss:box>-7.1281781361788461 43.9297452 49.492289536178845 114.2422452</georss:box></entry><entry><id>tag:blogger.com,1999:blog-4094940111921452231.post-7258286727359981607</id><published>2021-01-14T00:20:00.013+05:30</published><updated>2021-02-03T20:31:05.959+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="Header files"/><category scheme="http://www.blogger.com/atom/ns#" term="header files in C++"/><title type='text'>Header Files in C++</title><content type='html'>&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5k0xvk8F_dEqCwcLoZSiL6GFAuFN8H7pOpuQEyKvuZisuND_hAANOxukRIytFlnX_NryPf0mI825fd79Raq5D1RCFeCR5uO-3EwNqiLineGTR8SSm9lWGtSh2eypvMa3Jojg0sf_u4dc/s392/Header+for+hficpp+article.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Header Files in C++&quot; border=&quot;0&quot; data-original-height=&quot;392&quot; data-original-width=&quot;392&quot; height=&quot;413&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5k0xvk8F_dEqCwcLoZSiL6GFAuFN8H7pOpuQEyKvuZisuND_hAANOxukRIytFlnX_NryPf0mI825fd79Raq5D1RCFeCR5uO-3EwNqiLineGTR8SSm9lWGtSh2eypvMa3Jojg0sf_u4dc/w413-h413/Header+for+hficpp+article.png&quot; title=&quot;Header Files in C++&quot; width=&quot;413&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;So header files in C++ are files which contain code such as classes, functions, structs....&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In every C++ program we have seen the &quot;&lt;i&gt;iostream&lt;/i&gt;&quot; header file, this header file contains objects like &lt;i&gt;cin&lt;/i&gt; and &lt;i&gt;cout&lt;/i&gt; which handle input and output respectively. There are many standard library header files such as iostream. Others are the user defined header files which we make to have our own code.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;The benefit of a header file is that once we write, for example a 100 line code and we need that code in many of your programs, (just as to use the cin and cout objects we use the &quot;&lt;i&gt;iostream&lt;/i&gt;&quot; header) it is cumbersome to write that 100 line code again and again, so we&amp;nbsp; write that code in a header file and just use the header file to get the 100 line code in each of our programs.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;To use the header file, we write &quot;&lt;i&gt;#include&lt;/i&gt;&quot;, which is a preprocessor directive that will first process the code in the header file and then the code in the file in which the header file is used.&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Making a Header File&lt;/span&gt;&lt;/h3&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In C the header files end with the .h extension, in C++ the standard library files don&#39;t have the .h extension but the user defined header files in C++ have to have the .h extension. We can use the C libraries in C++ by excluding the .h extension and putting a &#39;c&#39; before the library name. for example - &amp;lt;stdio.h&amp;gt; in C becomes &amp;lt;cstdio&amp;gt; in C++.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Let&#39;s look at a basic example of a header file.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt; // demoHeader.h&lt;/span&gt;
 &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// A header file&lt;/span&gt;

&lt;span style=&quot;color: #4c8317;&quot;&gt; #pragma once		&lt;/span&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// preprocessor directive to include the file only once&lt;/span&gt;
 using namespace std;
&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt; // function to convert lowercase letters of a string to uppercase&lt;/span&gt;

 string low_to_up(string str1) {		    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// function header&lt;/span&gt;
	 &lt;span style=&quot;color: #0000aa;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; i = &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;; i != str1.size(); ++i) {   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// using the traditional for loop &lt;/span&gt;
						   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// to go through each character in the string.&lt;/span&gt;
		 str1[i] = toupper(str1[i]);	   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// Making each character uppercase&lt;/span&gt;
	 }
	 &lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; str1;				   &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// returning the modified string&lt;/span&gt;
 }
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;



&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 3,&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#pragma once	&lt;/span&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// preprocessor directive to include the file only once&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&quot;&lt;i&gt;#pragma once&lt;/i&gt;&quot; is a preprocessor&amp;nbsp;directive, which tells the compiler to include the code of the header file only once. This is necessary to not repeat the code. Repeating the code may cause errors and to avoid this, we use &quot;&lt;i&gt;#pragma once&lt;/i&gt;&quot;.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Then from line 8, we define a function&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;named&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;low_to_up&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;which returns a string and takes a string parameter&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;string &lt;span style=&quot;color: #00aa00;&quot;&gt;low_to_up&lt;/span&gt;(string str1) {		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// function header&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Line 9 states the for loop header,&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: #00aaaa;&quot;&gt;int&lt;/span&gt; i = &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;; i != str1.size(); ++i) {&lt;span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// using the traditional for loop to go &lt;/span&gt;
					    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// &lt;/span&gt;&lt;i&gt;&lt;span style=&quot;color: #999999;&quot;&gt;through each character in the string.&lt;/span&gt;&lt;/i&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;The for loop traverses through all the characters in the string, each character is converted to uppercase on line 11.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;str1[i] = toupper(str1[i]);		&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// Making each character uppercase&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;When all the characters are finished processing, the for loop ends and then, we return the modified string.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;



&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; str1;	    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// returning the modified string&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;That was the header file, now we write the main cpp file,&lt;/span&gt;&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;Including the header file&lt;/span&gt;&lt;/h3&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt; // program.cpp&lt;/span&gt;

&lt;span style=&quot;color: #4c8317;&quot;&gt; #include&amp;lt;iostream&amp;gt;				&lt;/span&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// standard library header file&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt; #include &quot;demoHeader.h&quot;			&lt;/span&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// user defined header file&lt;/span&gt;

&lt;span style=&quot;color: #0000aa;&quot;&gt; using&lt;/span&gt; &lt;span style=&quot;color: #0000aa;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #00aaaa;&quot;&gt; int&lt;/span&gt; &lt;span style=&quot;color: #00aa00;&quot;&gt;main&lt;/span&gt;() {
	
	 string text = &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;abcdefghijklmnopqrstuvwxyz&quot;&lt;/span&gt;;	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// Initializing a string variable&lt;/span&gt;
	 cout &amp;lt;&amp;lt; text &amp;lt;&amp;lt; endl;
	 text =  low_to_up(text);	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// assigning the return value of the low_to_up function&lt;/span&gt;
					&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// to the variable name&lt;/span&gt;
	 cout &amp;lt;&amp;lt; text &amp;lt;&amp;lt; endl;	    &lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// printing the modified value.&lt;/span&gt;

	 &lt;span style=&quot;color: #0000aa;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #009999;&quot;&gt;0&lt;/span&gt;;
 }
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;At line 3 is the standard library file (&lt;i&gt;&amp;lt;iostream&amp;gt;&lt;/i&gt;) and at line 4 is the use defined header file which we just made.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #4c8317;&quot;&gt;#include&amp;lt;iostream&amp;gt;		&lt;/span&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// standard library header file&lt;/span&gt;
&lt;span style=&quot;color: #4c8317;&quot;&gt;#include &quot;demoHeader.h&quot;		&lt;/span&gt;&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// user defined header file&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;The name of the header file should be in double quotes, preceded&amp;nbsp;by the preprocessor directive &quot;&lt;i&gt;#include&lt;/i&gt;&quot;.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 10, we define a string variable named as &lt;i&gt;text&lt;/i&gt;, which is assigned a string literal.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;


&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;string text = &lt;span style=&quot;color: #aa5500;&quot;&gt;&quot;abcdefghijklmnopqrstuvwxyz&quot;&lt;/span&gt;;	&lt;span style=&quot;color: #aaaaaa; font-style: italic;&quot;&gt;// Initializing a string variable&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 11, we simply print the value of variable &lt;i&gt;text.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 12, we assign to&amp;nbsp;&lt;i&gt;text &lt;/i&gt;the result of the call to the function &lt;i&gt;low_to_up, &lt;/i&gt;with &lt;i&gt;text &lt;/i&gt;being its parameter. The function converts the string to uppercase and assigns its return value to &lt;i&gt;text&lt;/i&gt;, then again on line 14, we print the value of &lt;i&gt;text.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;The result is&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;div style=&quot;text-align: center;&quot;&gt;abcdefghijklmnopqrstuvwxyz&lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;ABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;So, we wrote the function in the header file and just used the header file in the main cpp file with the help of the preprocessor directive. Like wise if we want to use that function in any other program we make, we don&#39;t have to write the full function code, just have to use the header file.&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://letspracticeprogramming.blogspot.com/feeds/7258286727359981607/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/01/header-files-in-cpp.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/7258286727359981607'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/7258286727359981607'/><link rel='alternate' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/01/header-files-in-cpp.html' title='Header Files in C++'/><author><name>Saurabh</name><uri>http://www.blogger.com/profile/03890363916503088023</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX0fmK91PXdZkLfX2Yqloq9Q01aP6rj7zRV84uOzbV0el--Ouu7Kn7yoGCc7GolJ2UAFSFu65I0rpoVZR4spxiVq45lkwWFp6B-NrKPfQRhAj2hgU4QSn1neTdtxXktq4/s220/Screenshot_20210111_231427.JPG'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5k0xvk8F_dEqCwcLoZSiL6GFAuFN8H7pOpuQEyKvuZisuND_hAANOxukRIytFlnX_NryPf0mI825fd79Raq5D1RCFeCR5uO-3EwNqiLineGTR8SSm9lWGtSh2eypvMa3Jojg0sf_u4dc/s72-w413-h413-c/Header+for+hficpp+article.png" height="72" width="72"/><thr:total>0</thr:total><georss:featurename>Jaripatka, Nagpur, Maharashtra 440014, India</georss:featurename><georss:point>21.1820557 79.0859952</georss:point><georss:box>-7.1281781361788461 43.9297452 49.492289536178845 114.2422452</georss:box></entry><entry><id>tag:blogger.com,1999:blog-4094940111921452231.post-6724951844250970187</id><published>2021-01-13T15:37:00.003+05:30</published><updated>2021-11-19T20:59:31.804+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="Const"/><category scheme="http://www.blogger.com/atom/ns#" term="Low level const"/><category scheme="http://www.blogger.com/atom/ns#" term="Top level and low level const"/><category scheme="http://www.blogger.com/atom/ns#" term="Top level const"/><title type='text'>Top Level and Low Level Const in C++</title><content type='html'>&lt;div style=&quot;clear: both; text-align: center;&quot;&gt;&lt;span style=&quot;font-family: courier; font-size: x-large;&quot;&gt;&lt;b&gt;&lt;br&gt;&lt;i&gt;&lt;br&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWaU3FumlwpG-NFOeJpoxdbj_AKvj_9u2bHUVKy39UCxfvmadGZl-WokMyE8k4z5YCRjLA6gH9m35cW6eFZPZJc1Ud3YiMkd73tjvZN3vVp045ZpuJS7QX5QNjope_vzyOcZv07GgX4rc/s1302/Header+for+tlllcicpp+article.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Top Level and Low Level Consts in C++&quot; border=&quot;0&quot; data-original-height=&quot;540&quot; data-original-width=&quot;1302&quot; height=&quot;239&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWaU3FumlwpG-NFOeJpoxdbj_AKvj_9u2bHUVKy39UCxfvmadGZl-WokMyE8k4z5YCRjLA6gH9m35cW6eFZPZJc1Ud3YiMkd73tjvZN3vVp045ZpuJS7QX5QNjope_vzyOcZv07GgX4rc/w632-h239/Header+for+tlllcicpp+article.png&quot; title=&quot;Top Level and Low Level Consts in C++&quot; width=&quot;632&quot;&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br&gt;&lt;/div&gt;&lt;div style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Let&#39;s look at an example..&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;  #include&amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;  using&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; std;

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;  int&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;main&lt;/span&gt;() {
  	  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; int1 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, int2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;;       &lt;span style=&quot;color: #888888;&quot;&gt;// Defining two integer variables&lt;/span&gt;
	  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt; ptr1 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;int1;      &lt;span style=&quot;color: #888888;&quot;&gt;// ptr1 has a top - level const&lt;/span&gt;
	  &lt;span style=&quot;color: #888888;&quot;&gt;// ptr1 is a constant pointer to integer variable int1&lt;/span&gt;

	  ptr1 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;int2;                 &lt;span style=&quot;color: #888888;&quot;&gt;// error : we cannot assign the address of any other variable to ptr1&lt;/span&gt;
	  &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;ptr1 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;;                    &lt;span style=&quot;color: #888888;&quot;&gt;// we can change the value of int1 through ptr1&lt;/span&gt;
	  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; ptr2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;int1;      &lt;span style=&quot;color: #888888;&quot;&gt;// ptr2 has a low level const&lt;/span&gt;
	  &lt;span style=&quot;color: #888888;&quot;&gt;// ptr2 is a pointer to a constant integer&lt;/span&gt;

	  &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;ptr2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;;                    &lt;span style=&quot;color: #888888;&quot;&gt;// error : we cannot change the value of int1 through ptr2&lt;/span&gt;
	  ptr2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;int2;                 &lt;span style=&quot;color: #888888;&quot;&gt;// we can assign address of another variable to ptr2, now ptr2 points to int2&lt;/span&gt;
	  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt; ptr3 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;int1;  &lt;span style=&quot;color: #888888;&quot;&gt;// here ptr3 has both top level and low level const&lt;/span&gt;

	  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; int3 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;;	        &lt;span style=&quot;color: #888888;&quot;&gt;// Defining a constant integer variable int3, here int3 has a top level const&lt;/span&gt;
	  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; ptr4 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;int3;	&lt;span style=&quot;color: #888888;&quot;&gt;// ptr4 is a pointer to a constant integer, here ptr4 has a low level const&lt;/span&gt;

	  &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
  }
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;


&lt;p&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;In the above example, on line 5 we defined two integer variables&amp;nbsp;&lt;/span&gt;&lt;/p&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; int1 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1&lt;/span&gt;, int2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;2&lt;/span&gt;;         &lt;span style=&quot;color: #888888;&quot;&gt;// Defining two integer variables&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;on line 6, we define&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;ptr1 &lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;which is a&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;constant pointer to an integer&lt;/span&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&amp;nbsp;and it is assigned the address of &lt;/span&gt;&lt;i style=&quot;font-family: courier;&quot;&gt;int1&lt;/i&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;.&lt;/span&gt;&lt;/p&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt; ptr1 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;int1;        &lt;span style=&quot;color: #888888;&quot;&gt;// ptr1 has a top - level const&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Here &lt;i&gt;ptr1&lt;/i&gt; has a top level const, that means we cannot assign an address of some other variable to it.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 9 we try to assign the address of &lt;i&gt;int2&lt;/i&gt; to &lt;i&gt;ptr1&lt;/i&gt;,&lt;/span&gt;&lt;/p&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt; ptr1 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;int2;  &lt;span style=&quot;color: #888888;&quot;&gt;// error : we cannot assign the address of any other variable to ptr1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;which is an error because &lt;i&gt;ptr1&lt;/i&gt; is a constant pointer to &lt;i&gt;int1&lt;/i&gt;. Although we can change the value of &lt;i&gt;int1&lt;/i&gt; through &lt;i&gt;ptr1&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br&gt;&lt;/span&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;ptr1 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;;     &lt;span style=&quot;color: #888888;&quot;&gt;// we can change the value of int1 through ptr1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;br&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 11. we define &lt;i&gt;ptr2&lt;/i&gt;, which is a pointer to a constant integer and is also assigned the address of &lt;i&gt;int1&lt;/i&gt;. Here &lt;i&gt;ptr2&lt;/i&gt; has a low level const. Although &lt;i&gt;int1&lt;/i&gt; is not a constant, the definition means that we cannot change the value of &lt;i&gt;int1&lt;/i&gt; through &lt;i&gt;ptr2&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; ptr2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;int1;    &lt;span style=&quot;color: #888888;&quot;&gt;// ptr2 has a low level const&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;// ptr2 is a pointer to a constant integer&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;br&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 14, we try to change the value of &lt;i&gt;int1&lt;/i&gt; through &lt;i&gt;ptr2&lt;/i&gt;,&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;ptr2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;;   &lt;span style=&quot;color: #888888;&quot;&gt;// error : we cannot change the value of int1 through ptr2&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;br&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;that&#39;s an error because &lt;i&gt;ptr2&lt;/i&gt; is a pointer to constant integer. we can assign the address of another variable to &lt;i&gt;ptr2&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;ptr2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;int2;      &lt;span style=&quot;color: #888888;&quot;&gt;// we can assign address of another variable to ptr2,&lt;/span&gt;
                   &lt;span style=&quot;color: #888888;&quot;&gt;// now ptr2 points to int2&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;br&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 16, we define &lt;i&gt;ptr3&lt;/i&gt;, which is a constant pointer to constant integer.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt; ptr3 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt;int1;  &lt;span style=&quot;color: #888888;&quot;&gt;// here ptr3 has both top level and low level const&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;br&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Here, &lt;i&gt;ptr3&lt;/i&gt; which is also assigned the address of &lt;i&gt;int1&lt;/i&gt;, has both top level and low level const, that means neither we can assign a different address to &lt;i&gt;ptr3&lt;/i&gt;, nor we can change the value of the variable &lt;i&gt;ptr3&lt;/i&gt; is pointing to.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;Now, at line 18 we defined a constant int named &lt;i&gt;int3&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br&gt;&lt;/span&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(255, 255, 255); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; int3 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;;	&lt;span style=&quot;color: #888888;&quot;&gt;// Defining a constant integer variable int3, here int3 has a top level const&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;br&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;i&gt;int3&lt;/i&gt; is a constant, so it&#39;s value cannot be changed.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;&lt;br&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: courier;&quot;&gt;On line 19, we defined a pointer to a constant integer named &lt;i&gt;ptr4&lt;/i&gt;, it is pointing towards a constant integer, that&#39;s why is has a low level const, otherwise it would have been an error.&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://letspracticeprogramming.blogspot.com/feeds/6724951844250970187/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/01/top-level-low-level-consts-cpp.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/6724951844250970187'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4094940111921452231/posts/default/6724951844250970187'/><link rel='alternate' type='text/html' href='https://letspracticeprogramming.blogspot.com/2021/01/top-level-low-level-consts-cpp.html' title='Top Level and Low Level Const in C++'/><author><name>Saurabh</name><uri>http://www.blogger.com/profile/03890363916503088023</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX0fmK91PXdZkLfX2Yqloq9Q01aP6rj7zRV84uOzbV0el--Ouu7Kn7yoGCc7GolJ2UAFSFu65I0rpoVZR4spxiVq45lkwWFp6B-NrKPfQRhAj2hgU4QSn1neTdtxXktq4/s220/Screenshot_20210111_231427.JPG'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWaU3FumlwpG-NFOeJpoxdbj_AKvj_9u2bHUVKy39UCxfvmadGZl-WokMyE8k4z5YCRjLA6gH9m35cW6eFZPZJc1Ud3YiMkd73tjvZN3vVp045ZpuJS7QX5QNjope_vzyOcZv07GgX4rc/s72-w632-h239-c/Header+for+tlllcicpp+article.png" height="72" width="72"/><thr:total>0</thr:total></entry></feed>