<?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-6188731032231049428</id><updated>2025-07-20T05:24:11.664-07:00</updated><category term="c++"/><category term="c"/><category term="Data structure"/><category term="Linux"/><category term="c++ constructor"/><category term="c glossary"/><category term="operator overloading"/><category term="templates in C++"/><category term="File handling"/><category term="Linked list"/><category term="Operator overloading c++"/><category term="String functions"/><category term="app"/><category term="c array"/><category term="c++ questions"/><category term="interview questions"/><category term="overloading"/><category term="pointers"/><category term="reference in C++"/><category term="sorting"/><category term="templates"/><category term="virtual function"/><category term="2D array"/><category term="C program"/><category term="C structures"/><category term="Deletion of node in LL"/><category term="Embedded books"/><category term="Intro to Exceptions in C++"/><category term="Linked list. Deletion of node in LL"/><category term="Linux commands"/><category term="Middle node of SLL"/><category term="Reverse SLL"/><category term="Stack"/><category term="Virtual inheritance"/><category term="append to linked list"/><category term="array and pointer"/><category term="arrays"/><category term="auto"/><category term="base class and derived class"/><category term="bitwise"/><category term="bool in c++"/><category term="break"/><category term="c bitwise operators"/><category term="c operators"/><category term="c++ program"/><category term="case"/><category term="char"/><category term="cin and cout"/><category term="command line arguments"/><category term="const function"/><category term="copy constructor"/><category term="cpp"/><category term="default parameters"/><category term="destructors in c++"/><category term="disable guest in ubuntu"/><category term="double"/><category term="extra destructor"/><category term="float"/><category term="for"/><category term="fork"/><category term="function call overloding"/><category term="functions"/><category term="functor"/><category term="goto"/><category term="inheritance and access specifiers"/><category term="inheritance in c++"/><category term="int"/><category term="lcm of n numbers"/><category term="linux pipes"/><category term="linux process"/><category term="list"/><category term="long double"/><category term="long int"/><category term="long long int"/><category term="memory allocation"/><category term="null pointer"/><category term="overloading ++"/><category term="pointers introduction"/><category term="polymorphism"/><category term="prime numbers"/><category term="print linked list"/><category term="private destructor"/><category term="private inheritance"/><category term="process"/><category term="ps"/><category term="public inheritance"/><category term="qt"/><category term="quiz"/><category term="round off error"/><category term="screen shot in ubuntu"/><category term="set path in ubunut"/><category term="short int"/><category term="show all startup apps in ubuntu"/><category term="singleton"/><category term="static"/><category term="static data in c++"/><category term="static function in c+"/><category term="static members of class"/><category term="stl"/><category term="string"/><category term="subscript operator overloading"/><category term="tar command"/><category term="top"/><category term="typedef"/><category term="ubuntu tricks"/><category term="unsigned"/><category term="virtual destructor"/><category term="void pointer"/><category term="volatile"/><category term="while"/><title type='text'>C with Linux</title><subtitle type='html'>Blog about programming in C, C++ and Linux</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>94</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-6608318504753894422</id><published>2017-10-29T09:55:00.001-07:00</published><updated>2017-10-29T09:58:58.665-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="app"/><title type='text'>Data structures using Cpp </title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
When my son told me that no one uses &quot;C&quot; nowadays, I realized my 
folly. I am writing an app, working my heads off, searching questions 
and solving them for an app which&amp;nbsp; has lot of competition and may not
 find many users.&lt;br /&gt;
&lt;br /&gt;
But writing all those 130 programs 
in C++ will take another year. (Yes I &quot;have&quot; written around 130 programs). It should not take another year, but you know 
me.&lt;br /&gt;
&lt;br /&gt;
So I have come to this conclusion. Before I release
 the app, I will implement&amp;nbsp; basic structures -&amp;nbsp; LL, stack, 
queue and BT in C++. And I will also try to add another 30 more programs
 in C++. That is -&amp;nbsp; versions of programs in C++ for which C programs already exist. &lt;br /&gt;
&lt;br /&gt;
I
 see two advantages here - for the app user. a) he/she will be able to 
write DS programs in shorter and more compact and intuitive way. b) 
he/she will become more familiar with C++ language.&lt;br /&gt;
&lt;br /&gt;
What
 better example do we get for encapsulation (keep your head and append 
and delete and display functions inside of the class), data hiding - private and public (head
 - what head are you talking about? You can only access the list. How we
 implement append, insert, delete or display is of no concern to you).&lt;br /&gt;
&lt;br /&gt;
And
 for once, the reader will be happy to write a C++ program instead of C.
 Because there is no need to return head pointers, no need to use double
 pointers and no need to worry if the function modifies more than one 
parameter.&lt;br /&gt;
&lt;br /&gt;
So I will post these C++ programs in my data structures blog. Happy DSing!&lt;br /&gt;
&lt;br /&gt;
P.S. If you have not noticed already, all my data structure programs will be available in &lt;a href=&quot;http://data-structures-in-c.blogspot.in/&quot;&gt;Data structures&lt;/a&gt; blog instead of &lt;a href=&quot;https://cwithlinux.blogspot.in/&quot;&gt;C with Linux &lt;/a&gt;. &lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/6608318504753894422/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2017/10/plan-to-write-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6608318504753894422'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6608318504753894422'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2017/10/plan-to-write-c.html' title='Data structures using Cpp '/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-589412527574830742</id><published>2017-07-11T08:48:00.000-07:00</published><updated>2017-07-11T08:48:02.912-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="fork"/><category scheme="http://www.blogger.com/atom/ns#" term="Linux"/><category scheme="http://www.blogger.com/atom/ns#" term="linux process"/><category scheme="http://www.blogger.com/atom/ns#" term="process"/><category scheme="http://www.blogger.com/atom/ns#" term="ps"/><category scheme="http://www.blogger.com/atom/ns#" term="top"/><title type='text'>Linux processes</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Process in operating system is a program under execution. A process has its own memory space. In some operating systems, the term &#39;task&#39; is used instead.&lt;br /&gt;
&lt;br /&gt;
When a program starts execution, the process is created. And the process is destroyed when the program ends. A process may consist of one or more threads which execute instructions concurrently. &lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Process ID - pid&amp;nbsp;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Every process has a unique identifier called process id or &lt;b&gt;pid&lt;/b&gt;. And each process also has a parent process id called &lt;b&gt;ppid&lt;/b&gt;. Parent is the process which started the execution of given process. &lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
List of running processes&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
To know which processes are running at any given time, you can use ps command. But ps will show only processes belonging to the given terminal from where you are running ps command.&lt;/div&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
ps -el&lt;/h4&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
This will list all processes running. l here is for long listing, where pid, ppid, command of the program and other informations are listed.&amp;nbsp;&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;
Given below is the screenshot of &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;ps -el|less&lt;/span&gt; command&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&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/AVvXsEjSR4b5gzAH2dqJkBkR1GMv057DYUYsNVGHTvZtEZ8MlUIPVZ3xeAOmisDRStVaR_urqny2nW_rI1CRz0UPx6D1o-yrKByMqpeHT0HGcvg_uGddP5eXrLRlkilQGzzMGmzUdv2Wa6N3rPFB/s1600/psel.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;638&quot; data-original-width=&quot;1132&quot; height=&quot;360&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSR4b5gzAH2dqJkBkR1GMv057DYUYsNVGHTvZtEZ8MlUIPVZ3xeAOmisDRStVaR_urqny2nW_rI1CRz0UPx6D1o-yrKByMqpeHT0HGcvg_uGddP5eXrLRlkilQGzzMGmzUdv2Wa6N3rPFB/s640/psel.png&quot; width=&quot;640&quot; /&gt;&lt;/a&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;
If you really want to know the tree structure of processes, you can given &lt;b&gt;pstree&lt;/b&gt; command.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&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/AVvXsEiA4Ubk9Y2qnGrIUYKlDD7H7WctJ7hSyBbvZ7jMMCS3GF_atYDVK3Ej2ftM8v81XYjgvar8Xm9F6UYj-QCk0uqZgPGs_kuUWvHrplShgOfMS17JMq9rgU_d9eYuxLAjtSyRRqBH2YmuuqjQ/s1600/pstree.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;638&quot; data-original-width=&quot;1132&quot; height=&quot;360&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiA4Ubk9Y2qnGrIUYKlDD7H7WctJ7hSyBbvZ7jMMCS3GF_atYDVK3Ej2ftM8v81XYjgvar8Xm9F6UYj-QCk0uqZgPGs_kuUWvHrplShgOfMS17JMq9rgU_d9eYuxLAjtSyRRqBH2YmuuqjQ/s640/pstree.png&quot; width=&quot;640&quot; /&gt;&lt;/a&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;
There are many more options available for ps command.&amp;nbsp;&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;b&gt;top&lt;/b&gt; command also lists the running processes dynamically in 
descending order of their cpu usage. It gives other informations such as
 memory usage, how long system has been up etc.&amp;nbsp; &lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&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/AVvXsEgCdzYn8qICfYF6XTd_LdD_zPaj_9YNSYqX8wnXtc5Wkkv2jJ_VSp7EvT96uUhyzoZ4HKJyOPu8TVguW2AqcLT-e0SF72OGjC35Gzu3CjM57hCgSp7DF1GNdly-bIawgrJZXUqgHl_1A5vn/s1600/top.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;638&quot; data-original-width=&quot;1132&quot; height=&quot;360&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCdzYn8qICfYF6XTd_LdD_zPaj_9YNSYqX8wnXtc5Wkkv2jJ_VSp7EvT96uUhyzoZ4HKJyOPu8TVguW2AqcLT-e0SF72OGjC35Gzu3CjM57hCgSp7DF1GNdly-bIawgrJZXUqgHl_1A5vn/s640/top.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Pid of my program?&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
You can get the pid of your program and also ppid using two library functions, called - you get it - getpid() and getppid(). You need to include the header file unistd.h for these functions.&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;br /&gt;&lt;/div&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #f8f8f8; 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: 0;&quot;&gt;&lt;span style=&quot;color: #bc7a00;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bc7a00;&quot;&gt;#include&amp;lt;unistd.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;main&lt;/span&gt;()
{
 printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;my pid is %u&quot;&lt;/span&gt;,getpid());
 printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;my ppid is %u&quot;&lt;/span&gt;,getppid());
 &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #666666;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
And when you run the program, you notice that the ppid of the process is the pid of the shell.&lt;br /&gt;
&lt;br /&gt;
Don&#39;t expect to see this pid of your program next time you run ps -el command. Your program runs two statements and ends. So the process is no longer present.&amp;nbsp; &lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Can I create a new process through program?&amp;nbsp;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Yes, you can create your own process using a function called fork(). fork() creates a new process by replicating the existing process. The new process is called child process and the existing one is called parent process.&amp;nbsp;&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;
The child process has ppid as that of&amp;nbsp; parent process. As the child is the exact duplicate of parent process, it has&amp;nbsp; a copy of all parent&#39;s variables, open file descriptor, shared memories.&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;
fork() returns pid of the child process in parent&#39;s context and 0 in child&#39;s context. This can be used for differentiating between parent process and child process.&amp;nbsp;&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;/div&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #f8f8f8; 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: 0;&quot;&gt;&lt;span style=&quot;color: #bc7a00;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bc7a00;&quot;&gt;#include&amp;lt;unistd.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bc7a00;&quot;&gt;#include&amp;lt;sys/types.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;main&lt;/span&gt;()
{
 &lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; a &lt;span style=&quot;color: #666666;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #666666;&quot;&gt;10&lt;/span&gt;;
 &lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; b&lt;span style=&quot;color: #666666;&quot;&gt;=11&lt;/span&gt;;
 &lt;span style=&quot;color: #b00040;&quot;&gt;pid_t&lt;/span&gt; pid;
 printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;before fork, pid is %d&lt;/span&gt;&lt;span style=&quot;color: #bb6622; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;&lt;/span&gt;,getpid());
 pid &lt;span style=&quot;color: #666666;&quot;&gt;=&lt;/span&gt; fork();
 &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;if&lt;/span&gt;(pid&lt;span style=&quot;color: #666666;&quot;&gt;==0&lt;/span&gt;)
 {
  printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;I am child process. My pid is %d&lt;/span&gt;&lt;span style=&quot;color: #bb6622; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;&lt;/span&gt;,getpid());
  printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;Child saying hello&quot;&lt;/span&gt;);
 }
 &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;else&lt;/span&gt; 
 {
  printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;I am parent process. My pid is %d&lt;/span&gt;&lt;span style=&quot;color: #bb6622; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;&lt;/span&gt;,getpid());
  printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;parent saying hello&quot;&lt;/span&gt;);
 }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
The output of the program will be&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;before fork, pid is 5195&lt;br /&gt;I am parent process. My pid is 5195&lt;br /&gt;parent saying helloI am child process. My pid is 5196&lt;br /&gt;Child saying hello&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In the next post we will discus s  more about fork and vfork and  wait  functions  &lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/589412527574830742/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2017/07/linux-processes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/589412527574830742'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/589412527574830742'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2017/07/linux-processes.html' title='Linux processes'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSR4b5gzAH2dqJkBkR1GMv057DYUYsNVGHTvZtEZ8MlUIPVZ3xeAOmisDRStVaR_urqny2nW_rI1CRz0UPx6D1o-yrKByMqpeHT0HGcvg_uGddP5eXrLRlkilQGzzMGmzUdv2Wa6N3rPFB/s72-c/psel.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-6986243512202419122</id><published>2017-05-31T05:16:00.000-07:00</published><updated>2017-05-31T05:16:38.316-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="command line arguments"/><category scheme="http://www.blogger.com/atom/ns#" term="pointers"/><title type='text'>Command line arguments in C</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot;&gt;
What are the different ways of providing input in a c programme?&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
You have used scanf, gets, getc. There is another much 
easier way. That is to provide the values along with program name when 
executing it.&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
So instead of saying &lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;./a.out&lt;/span&gt;&amp;nbsp; you would use &lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;./a.out hello world&lt;/span&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
When such arguments are provided they have to be accessed 
using main function with an int parameter and another array of char 
pointers.&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
These are&amp;nbsp; called argc (argument count) and argv (argument vector) respectively.&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;
&lt;/span&gt;
&lt;br /&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;int main (int argc, char *argv [])&lt;br /&gt;
{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ......}&lt;/span&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Let us start with a simple program.&amp;nbsp;&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #f8f8f8; 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: 0;&quot;&gt;&lt;span style=&quot;color: #bc7a00;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;main&lt;/span&gt;(&lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; argc,&lt;span style=&quot;color: #b00040;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: #666666;&quot;&gt;*&lt;/span&gt;argv[])
{
   printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;argc is %d&lt;/span&gt;&lt;span style=&quot;color: #bb6622; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;&lt;/span&gt;,argc);
   printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;and argv[0] is %s\n&quot;&lt;/span&gt;,argv[&lt;span style=&quot;color: #666666;&quot;&gt;0&lt;/span&gt;]);
   &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #666666;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
If we compile and execute this program, the output will be&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #073763;&quot;&gt;yourhomedir$./a.out &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;argc is 1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;argv[0] is ./a.out&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;Let us understand the program. As we are running the program with no extra argument, argument count is 1 and 0th element of argument vector is executable name itself.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;If we run the program again with hello world, argc will be 3&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #073763;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #073763;&quot;&gt;yourhomedir$&lt;/span&gt;./a.out hello world&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;argc is 3&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;argv[0] is ./a.out&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;Let us use these arguments and write a program to print all the command line arguments&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #f8f8f8; 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: 0;&quot;&gt;&lt;span style=&quot;color: #bc7a00;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;main&lt;/span&gt;(&lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; argc,&lt;span style=&quot;color: #b00040;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: #666666;&quot;&gt;*&lt;/span&gt;argv[])
{
   &lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; i;
   printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;argc is %d&lt;/span&gt;&lt;span style=&quot;color: #bb6622; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;&lt;/span&gt;,argc);
   &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;for&lt;/span&gt; (i&lt;span style=&quot;color: #666666;&quot;&gt;=0&lt;/span&gt;;i&lt;span style=&quot;color: #666666;&quot;&gt;&amp;lt;&lt;/span&gt;argc;i&lt;span style=&quot;color: #666666;&quot;&gt;++&lt;/span&gt;)
    printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;and argv[%d] is %s&lt;/span&gt;&lt;span style=&quot;color: #bb6622; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;&lt;/span&gt;,i,argv[i]);
   &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #666666;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
This is similar to previous program, except that we have added a loop to print all elements of argv in that loop.&amp;nbsp; And the loop repeats argc number of times.&lt;br /&gt;
&lt;br /&gt;
Let us compile and execute this.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;color: #073763;&quot;&gt;yourhomedir$&lt;/span&gt;gcc b1.c&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;color: #073763;&quot;&gt;yourhomedir$&lt;/span&gt;./a.out hello beautiful world&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&amp;nbsp;&lt;span style=&quot;color: black;&quot;&gt;argc is 4&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;argv[0] is ./a.out&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;argv[1] is hello&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;argv[2] is beautiful&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;argv[3] is world&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;You can write a program to add the numbers provided by command line arguments. You can store these numbers in an array and find the largest of all and so on.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;But we will finally write a program to print a file whose name is given as command line argument.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #f8f8f8; 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: 0;&quot;&gt;&lt;span style=&quot;color: #bc7a00;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bc7a00;&quot;&gt;#include&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;main&lt;/span&gt;(&lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; argc,&lt;span style=&quot;color: #b00040;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: #666666;&quot;&gt;*&lt;/span&gt;argv[])
{
   &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;if&lt;/span&gt;(argc&lt;span style=&quot;color: #666666;&quot;&gt;&amp;lt;2&lt;/span&gt;)
   {
 printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;Syntax is ./a.out filename&quot;&lt;/span&gt;);
        &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #666666;&quot;&gt;0&lt;/span&gt;;
   }
   &lt;span style=&quot;color: #b00040;&quot;&gt;FILE&lt;/span&gt; &lt;span style=&quot;color: #666666;&quot;&gt;*&lt;/span&gt;fptr &lt;span style=&quot;color: #666666;&quot;&gt;=&lt;/span&gt; fopen(argv[&lt;span style=&quot;color: #666666;&quot;&gt;1&lt;/span&gt;],&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;r&quot;&lt;/span&gt;);
   &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;if&lt;/span&gt;(fptr&lt;span style=&quot;color: #666666;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;NULL&lt;/span&gt;)
      {
         printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;File open error&quot;&lt;/span&gt;);
         exit(&lt;span style=&quot;color: #666666;&quot;&gt;1&lt;/span&gt;);
      }
   &lt;span style=&quot;color: #b00040;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: #666666;&quot;&gt;*&lt;/span&gt;str;
   &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;while&lt;/span&gt;( (str&lt;span style=&quot;color: #666666;&quot;&gt;=&lt;/span&gt;fgets( str,&lt;span style=&quot;color: #666666;&quot;&gt;100&lt;/span&gt;,fptr))&lt;span style=&quot;color: #666666;&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;NULL&lt;/span&gt;)
      printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;%s&quot;&lt;/span&gt;,str);
   fclose(fptr);
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;color: #073763;&quot;&gt;yourhomedir$&lt;/span&gt;gcc b&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;2&lt;/span&gt;.c&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;color: #073763;&quot;&gt;yourhomedir$&lt;/span&gt;./a.out &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;b2.c&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;int main(int argc,char *argv[])&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; if(argc&amp;lt;2)&lt;br /&gt;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;Syntax is ./a.out filename&quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp; FILE *fptr = fopen(argv[1],&quot;r&quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp; if(fptr==NULL)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;File open error&quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; exit(1);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp; char *str;&lt;br /&gt;&amp;nbsp;&amp;nbsp; while( (str=fgets( str,100,fptr))!=NULL)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;%s&quot;,str);&lt;br /&gt;&amp;nbsp;&amp;nbsp; fclose(fptr);&lt;br /&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;So what we&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt; are doing here is &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;we take the 1s&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;t &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;le&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;ment of argv as filename and try to &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;open that file. If fi&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;le open is successful, we read one line at a time and display it on screen.&amp;nbsp;&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;Quite simple. Isn&#39;t it?&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;For such programs and quiz on these topics,&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt; please download my &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;app -&lt;a href=&quot;https://play.google.com/store/apps/details?id=com.hegdeapps.cquestions&quot;&gt; &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;C interview questions &lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;color: #0c343d;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/6986243512202419122/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2017/05/command-line-arguments-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6986243512202419122'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6986243512202419122'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2017/05/command-line-arguments-in-c.html' title='Command line arguments in C'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-6181047864277194008</id><published>2017-01-26T06:10:00.000-08:00</published><updated>2017-05-30T09:37:25.599-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><title type='text'>Convert a number to hex</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
I am not trying to cast a spell on you. What I am going to write is how to convert a decimal number into hexadecimal in C language.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&quot;&gt;&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; a &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;100&lt;/span&gt;;
printf(&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;%x&quot;&lt;/span&gt;,a);
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&amp;nbsp;So printf format specifier %x will print a value in hex. %o (oh not zero) will print the value in octal.&lt;br /&gt;
&lt;br /&gt;
Is it that simple. It is simple. Do you also know that you can directly type your hex values to variables. And octal too. &lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;int m = 0xff; //255&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;int n = 011;//octal - 9&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
So you type a number with prefix zero - it is treated as octal number. And prefix 0x - it is treated as hexadecimal.&lt;br /&gt;
&lt;br /&gt;
One small program to end the post.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #f8f8f8; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0;&quot;&gt;&lt;span style=&quot;color: #bc7a00;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;main&lt;/span&gt;()
{
    &lt;span style=&quot;color: #b00040;&quot;&gt;int&lt;/span&gt; n;
    &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;while&lt;/span&gt; (&lt;span style=&quot;color: #666666;&quot;&gt;1&lt;/span&gt;)
    {
    printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;Enter a number: (-1 to quit)&quot;&lt;/span&gt;);
    scanf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;%d&quot;&lt;/span&gt;,&lt;span style=&quot;color: #666666;&quot;&gt;&amp;amp;&lt;/span&gt;n);
    &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;if&lt;/span&gt;(n&lt;span style=&quot;color: #666666;&quot;&gt;==-1&lt;/span&gt;)
       &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;break&lt;/span&gt;;
    printf(&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;%4d    %4o    %4x&lt;/span&gt;&lt;span style=&quot;color: #bb6622; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #ba2121;&quot;&gt;&quot;&lt;/span&gt;,n,n,n);
    }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
The program prints numbers in decimal, octal and hexadecimal continuously until -1 is entered.&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
By the way, in C++, you need to use io manipulator hex and oct to print the numbers in hexadecimal and octal.&lt;br /&gt;
&lt;br /&gt;
Liked this post? You will get plenty of such questions in my app&lt;a href=&quot;https://play.google.com/store/apps/details?id=com.hegdeapps.cquestions&quot;&gt; C interview questions &lt;/a&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/6181047864277194008/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2017/01/convert-number-to-hex.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6181047864277194008'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6181047864277194008'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2017/01/convert-number-to-hex.html' title='Convert a number to hex'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-1021210901906958070</id><published>2016-11-29T22:59:00.000-08:00</published><updated>2016-11-29T23:00:07.261-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="const function"/><title type='text'>Constants in C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;div align=&quot;left&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;Constant in
C++ is a value which can not be modified. As in C, we can have literal
constants using &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;#define&lt;/span&gt; and we can have enums or we can define a
variable to be const - fixed.&amp;nbsp; Let us concentrate on const
variables here. &lt;br /&gt;
&lt;br /&gt;
By declaring a variable as
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const&lt;/span&gt;, we ensure that it is not modified accidentlly. Any modification
to constant will give a compilation error.&lt;br /&gt;
&lt;br /&gt;
In the program below,&amp;nbsp; assignment to &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;pi&lt;/span&gt; gives a compiler
error because pi is defined as const. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;

&lt;br /&gt;
&lt;div style=&quot;background: rgb(248 , 248 , 248) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.2em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 0 , 255);&quot;&gt;main&lt;/span&gt;()
{
    &lt;span style=&quot;color: rgb(0 , 128 , 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;float&lt;/span&gt; pi &lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;22.0/7&lt;/span&gt;;
    &lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;int&lt;/span&gt; radius &lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;12&lt;/span&gt;;
    radius&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;++&lt;/span&gt;;&lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;/*ok*/&lt;/span&gt;
    pi &lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;3.14&lt;/span&gt;;&lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;/*error*/&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;Even function parameters can
be constant. We have earlier discussed that making a reference
parameter as constant is always better to avoid the function from
modifying the argument. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;

&lt;br /&gt;
&lt;div style=&quot;background: rgb(248 , 248 , 248) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.2em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 0 , 255);&quot;&gt;printnum&lt;/span&gt;(&lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;&amp;amp;&lt;/span&gt; n)
{
    cout&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;n&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;++&lt;/span&gt;;
}
&lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 0 , 255);&quot;&gt;printnum2&lt;/span&gt;(&lt;span style=&quot;color: rgb(0 , 128 , 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;&amp;amp;&lt;/span&gt; n)
{
     cout&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;n&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;++&lt;/span&gt;;&lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;/*error*/&lt;/span&gt;
}
&lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 0 , 255);&quot;&gt;main&lt;/span&gt;()
{
   &lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;int&lt;/span&gt; a &lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;10&lt;/span&gt;;
   printnum(a);
   cout&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;a;&lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;/*prints 11*/&lt;/span&gt;
   printnum2(a);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;In the code above, &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;printnum&lt;/span&gt;()&lt;/span&gt; &lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;function increments the argument
and it goes undetected by the compiler. Whereas, in&lt;/span&gt; &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;printnum2&lt;/span&gt;()&lt;/span&gt;, &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;n++&lt;/span&gt; &lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;gives a compiler error, because
the parameter is a constant reference, and so it can not be modified. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;So unless your function
intends to change the reference parameter, make it as constant
reference parameter. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;
Now let us consider how to define and use constant members of a class.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;big&gt;&lt;big&gt;&lt;b&gt;Constant Data Members&lt;/b&gt;&lt;/big&gt;&lt;/big&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;
A class can have constant data members by using keyword &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;/span&gt; in declaration. Such
members
can not be modified after the object is created.&lt;/span&gt;
&lt;br /&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;color: #ff6600; font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;The question arises - how do
we initialize such const members?&lt;/span&gt; &lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;Initializing const data
members &lt;span style=&quot;font-weight: bold;&quot;&gt;must&lt;/span&gt; be done using &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;member initializer&lt;/span&gt;
&lt;i&gt;list&lt;/i&gt;&lt;/span&gt;. Because&lt;b&gt; const
data can not be assigned within constructor body.&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;

&lt;br /&gt;
&lt;div style=&quot;background: rgb(248 , 248 , 248) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.2em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(0 , 128 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 0 , 255); font-weight: bold;&quot;&gt;A&lt;/span&gt;
{
 &lt;span style=&quot;color: rgb(0 , 128 , 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;int&lt;/span&gt; n;
&lt;span style=&quot;color: rgb(160 , 160 , 0);&quot;&gt;public:&lt;/span&gt;
 A();
};

A&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;::&lt;/span&gt;A()&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;:&lt;/span&gt;n(&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;22&lt;/span&gt;)
{
  n&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;10&lt;/span&gt;;&lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;/*error*/&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;As can be seen in the
example,&amp;nbsp; &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const &lt;/span&gt;member &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;n&lt;/span&gt;
&lt;/span&gt;is initialized in
member initializer list. And assigning a value to&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt; n&lt;/span&gt;, even in the
constructor, gives an error.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: #fff2cc;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: #fff2cc;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;So if a class has a const data member (a) write a constructor for that
class (b) In the constructor, initialize the const member using member
initializer list.&lt;/span&gt;&lt;/span&gt; 
&lt;br /&gt;
&lt;h3 align=&quot;center&quot;&gt;
&lt;big&gt;&lt;b&gt;constant functions&lt;/b&gt;&lt;/big&gt;&lt;/h3&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;A constant function&lt;span style=&quot;font-weight: bold;&quot;&gt; CAN NOT&lt;/span&gt; modify the &lt;span style=&quot;font-style: italic;&quot;&gt;state&lt;/span&gt; of the object i.e. it can not modify any data member. &lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;b&gt;And
constant objects can only call constant functions.&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;&lt;/span&gt; &lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;A &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const &lt;/span&gt;function is written
by adding keyword &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const &lt;/span&gt;after the parameter list&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;/div&gt;
&lt;div style=&quot;background: rgb(248 , 248 , 248) none repeat scroll 0%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.2em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(0 , 128 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 0 , 255); font-weight: bold;&quot;&gt;A&lt;/span&gt;
{
 &lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;int&lt;/span&gt; a;
&lt;span style=&quot;color: rgb(160 , 160 , 0);&quot;&gt;public:&lt;/span&gt;
 &lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;void&lt;/span&gt; print() &lt;span style=&quot;color: rgb(0 , 128 , 0); font-weight: bold;&quot;&gt;const&lt;/span&gt;;
 &lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 0 , 255);&quot;&gt;seta&lt;/span&gt;(&lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;int&lt;/span&gt; m){a &lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;=&lt;/span&gt; m;}
};
&lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;void&lt;/span&gt; A&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;::&lt;/span&gt;print() &lt;span style=&quot;color: rgb(0 , 128 , 0); font-weight: bold;&quot;&gt;const&lt;/span&gt;
{
 cout&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;a;
 &lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;// a = 0;/*invalid*/&lt;/span&gt;
}
&lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;int&lt;/span&gt; main()
{
 &lt;span style=&quot;color: rgb(0 , 128 , 0); font-weight: bold;&quot;&gt;const&lt;/span&gt; A obj1;
 obj1.print();&lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;//valid&lt;/span&gt;
 obj1.seta(&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;12&lt;/span&gt;);&lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;//error&lt;/span&gt;
 A obj2;
 obj2.seta(&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;12&lt;/span&gt;);&lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;//valid&lt;/span&gt;
 obj2.print();&lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;//valid&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;As you can see in the code above, &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;print&lt;/span&gt;() &lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;function, which is &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const&lt;/span&gt;, is not allowed to change &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;a&amp;nbsp; &lt;/span&gt;- the data member. &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;obj1&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;which is a constant object can call only &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const &lt;/span&gt;function viz. &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;print&lt;/span&gt;(). &lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;It can not call &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;seta&lt;/span&gt;() &lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;function. But a non-constant object&lt;/span&gt; &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;obj2 &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;can call both const and non-const function.&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h3 align=&quot;center&quot;&gt;
Constant objects&lt;/h3&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;If an&amp;nbsp; object is declared as &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const&lt;/span&gt;, it can call only &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const &lt;/span&gt;functions. An ordinary object can call both &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const
&lt;/span&gt;and non-const functions.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&amp;nbsp;
&lt;br /&gt;
&lt;h3 align=&quot;center&quot;&gt;
&lt;big&gt;Mutable members:&lt;/big&gt;&lt;/h3&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;Even from a &lt;span style=&quot;font-style: italic;&quot;&gt;constant&lt;/span&gt; object, &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;mutable&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;data members can be changed
and they can be changed even inside a &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;const&lt;/span&gt;
&lt;/span&gt;functions.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;

&lt;br /&gt;
&lt;div style=&quot;background: rgb(248 , 248 , 248) none repeat scroll 0%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.2em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(0 , 128 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 0 , 255); font-weight: bold;&quot;&gt;A&lt;/span&gt;
{
 &lt;span style=&quot;color: rgb(0 , 128 , 0); font-weight: bold;&quot;&gt;mutable&lt;/span&gt; &lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;int&lt;/span&gt; num_accessed;&lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;/*mutable member*/&lt;/span&gt;
 &lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;int&lt;/span&gt; b;
&lt;span style=&quot;color: rgb(160 , 160 , 0);&quot;&gt;public:&lt;/span&gt;
 &lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;/*other code*/&lt;/span&gt;
 &lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;void&lt;/span&gt; print() &lt;span style=&quot;color: rgb(0 , 128 , 0); font-weight: bold;&quot;&gt;const&lt;/span&gt;;
};

&lt;span style=&quot;color: rgb(176 , 0 , 64);&quot;&gt;void&lt;/span&gt; A&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;::&lt;/span&gt;print() &lt;span style=&quot;color: rgb(0 , 128 , 0); font-weight: bold;&quot;&gt;const&lt;/span&gt;
{
 cout&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;num_accessed&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(186 , 33 , 33);&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;b;
 num_accessed&lt;span style=&quot;color: rgb(102 , 102 , 102);&quot;&gt;++&lt;/span&gt;;&lt;span style=&quot;color: rgb(64 , 128 , 128); font-style: italic;&quot;&gt;/* valid. the member is mutable*/&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;In the code above,&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt; &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;num_accesse&lt;/span&gt;d&lt;/span&gt; is incremented within a &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const &lt;/span&gt;function and the program would compile without errors.  &lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/1021210901906958070/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/constants-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/1021210901906958070'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/1021210901906958070'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/constants-in-c.html' title='Constants in C++'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-1361281870494154681</id><published>2016-11-21T01:40:00.000-08:00</published><updated>2017-05-30T09:32:58.535-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="Virtual inheritance"/><title type='text'>Multiple Inheritance in C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;In C++ a class can inherit from more than one class.
Other languages which support multiple inheritance are python, perl, common lisp etc. &lt;br /&gt;
&lt;br /&gt;In&amp;nbsp;
Java, C# and Ruby,&amp;nbsp; it is not possible to have 2 or more base classes for a given
class. But C++ allows it. &lt;br /&gt;
&lt;/span&gt;
&lt;br /&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;Syntax for
multiple
inheritance is&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;!-- HTML generated using hilite.me --&gt;

&lt;br /&gt;
&lt;div style=&quot;background: rgb(255 , 255 , 255) none repeat scroll 0%; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;cls&lt;/span&gt;&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;-&lt;/span&gt;name&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; base&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;-&lt;/span&gt;cls1,&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; base&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;-&lt;/span&gt;cls2,&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; base&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;-&lt;/span&gt;cls3&lt;/pre&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;{
&lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/*code*/&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;The base
class list is comma
separated and must have access specifier for each base class. Each of these
base classes must have been declared earlier.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;Derived
class objects
contains sub-objects of each of the base classes. &lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;/span&gt;

&lt;br /&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;/div&gt;
&lt;div style=&quot;background: rgb(255 , 255 , 255) none repeat scroll 0%; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;LAnimal&lt;/span&gt;
{
 &lt;span style=&quot;color: rgb(153 , 119 , 0); font-weight: bold;&quot;&gt;public:&lt;/span&gt;
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;void&lt;/span&gt; walk()
 {
 cout&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;walks&quot;&lt;/span&gt;;
 }
};
&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;WAnimal&lt;/span&gt;
{
 &lt;span style=&quot;color: rgb(153 , 119 , 0); font-weight: bold;&quot;&gt;public:&lt;/span&gt;
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;void&lt;/span&gt; swim()
 {
 cout&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;swims&quot;&lt;/span&gt;;
 }
};
&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;Amphibian&lt;/span&gt;&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; LAnimal,&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; WAnimal
{
};
&lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 102 , 187); font-weight: bold;&quot;&gt;main&lt;/span&gt;()
{
 Amphibian frog;
 frog.walk();
 frog.swim();
} &lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;In the
above example, class
Amphibian inherits from 2 classes - LAnimal and WAnimal. It has members
of both these base classes. It can call both walk() and swim()
functions. &lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot; style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;b&gt;&lt;big&gt;&lt;big&gt;Constructors&lt;/big&gt;&lt;/big&gt;&lt;/b&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;The&lt;span style=&quot;font-style: italic;&quot;&gt; derived class constructor and
destructors&amp;nbsp; in multiple inheritace will call all of the base
class constructors and destructors.&lt;/span&gt;&amp;nbsp;
The order of constructor invocations is same as declaration order of
base classes.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;Amphibian constructor will call LAnimal constructor
first, then WAnimal constructor and finally Amphibian constructor. And
order of destructor invocation is reverse of order of constructor
invocation. &lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;h3 align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;Dreaded diamond problem and
Virtual Inheritance&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;When there
is a &lt;span style=&quot;font-style: italic;&quot;&gt;multilevel&lt;/span&gt; and &lt;span style=&quot;font-style: italic;&quot;&gt;multiple&lt;/span&gt; inheritance
together,&amp;nbsp; derived
class may contain more than one instance of an ancestor class. Accessing a member from this base class throws an ambiguity error.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background: rgb(255 , 255 , 255) none repeat scroll 0%; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;A&lt;/span&gt;
{
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; m;
&lt;span style=&quot;color: rgb(153 , 119 , 0); font-weight: bold;&quot;&gt;public:&lt;/span&gt;
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 102 , 187); font-weight: bold;&quot;&gt;setm&lt;/span&gt;(&lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; a){m &lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;=&lt;/span&gt; a;};
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 102 , 187); font-weight: bold;&quot;&gt;getm&lt;/span&gt;(){&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; m;}
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 102 , 187); font-weight: bold;&quot;&gt;print&lt;/span&gt;(){cout&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;m&quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;m&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;endl;}
};
&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; A
{
&lt;span style=&quot;color: rgb(153 , 119 , 0); font-weight: bold;&quot;&gt;public:&lt;/span&gt;
 B(){setm(&lt;span style=&quot;color: rgb(0 , 0 , 221); font-weight: bold;&quot;&gt;10&lt;/span&gt;);};
};
&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; A
{
&lt;span style=&quot;color: rgb(153 , 119 , 0); font-weight: bold;&quot;&gt;public:&lt;/span&gt;
 C() {setm(&lt;span style=&quot;color: rgb(0 , 0 , 221); font-weight: bold;&quot;&gt;20&lt;/span&gt;);};
};
&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;D&lt;/span&gt;&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; B,&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; C
{
 &lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/*multiple inherited class*/&lt;/span&gt;
};
&lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 102 , 187); font-weight: bold;&quot;&gt;main&lt;/span&gt;()
{
 D obj;
 cout&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;obj.getm();&lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/*error*/&lt;/span&gt;
 obj.print();&lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/*error*/&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;When we
compile this program &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;obj.print()&lt;/span&gt;throws
ambiguity error.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;obj has
members of A class duplicated. It has 2 &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;m&lt;/span&gt;s, one is set 10 in B class
constructor. And the other is set to 20 in C class constructor. So
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;obj.getm() &lt;/span&gt;causes a conflict, whether to use m from B class path or m
from C class path.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;Similarly
when calling &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;print()&lt;/span&gt;, compiler is unable to decide whether to call
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;print &lt;/span&gt;through C class or &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;print &lt;/span&gt;through B class.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;This is
called dreaded diamond problem because of the shape of inheritance
diagram. &lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;To avoid
this error, virtual
inheritance is used.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot; style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;b&gt;&lt;big&gt;&lt;big&gt;Virtual Inheritance&lt;/big&gt;&lt;/big&gt;&lt;/b&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;In virtual
inheritance, in spite of multiple paths
to a base class, base class object is included only once.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&amp;nbsp;&lt;!-- HTML generated using hilite.me --&gt;
&lt;br /&gt;
&lt;div style=&quot;background: rgb(255 , 255 , 255) none repeat scroll 0%; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;A&lt;/span&gt;
{
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; m;
&lt;span style=&quot;color: rgb(153 , 119 , 0); font-weight: bold;&quot;&gt;public:&lt;/span&gt;
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 102 , 187); font-weight: bold;&quot;&gt;getm&lt;/span&gt;(){&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;return&lt;/span&gt; m;}
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 102 , 187); font-weight: bold;&quot;&gt;setm&lt;/span&gt;(&lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; a){m &lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;=&lt;/span&gt; a;}
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 102 , 187); font-weight: bold;&quot;&gt;print&lt;/span&gt;(){cout&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;A print&quot;&lt;/span&gt;;}
};
&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;B&lt;/span&gt;&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;virtual&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; A
{
&lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/***code**/&lt;/span&gt;
};
&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;virtual&lt;/span&gt; A
{
&lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/***code**/&lt;/span&gt;
};
&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;D&lt;/span&gt;&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; B,&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;public&lt;/span&gt; C
{
&lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/**this has only one sub object of A*/&lt;/span&gt;
};
&lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 102 , 187); font-weight: bold;&quot;&gt;main&lt;/span&gt;()
{
 &lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/*code*/&lt;/span&gt;
 D obj;
 obj.print();&lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/*No error*/&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;Both B and
C class use virtual inheritance. So when D inherits from both B and C,
it is inheriting &lt;i&gt;&lt;b&gt;only one sub-object of A class.&lt;/b&gt;&lt;/i&gt; So
there is only one data member called m in D class and only one &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;print &lt;/span&gt;function. There will not be any ambiguity when obj.print() function is
called. &lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&amp;nbsp;Also
observe that obj&amp;nbsp; causes&amp;nbsp; A class constructor to be called
only once and A destructor to be called only once.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;And do you know what? This topic and other topics are explained nicely in my C++ app. May be you want to download it from &lt;a href=&quot;https://play.google.com/store/apps/details?id=com.hegdeapps.cppquestions&quot;&gt;google play. &lt;/a&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/1361281870494154681/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/multiple-inheritance-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/1361281870494154681'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/1361281870494154681'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/multiple-inheritance-in-c.html' title='Multiple Inheritance in C++'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-3714778029183256595</id><published>2016-11-19T19:15:00.000-08:00</published><updated>2016-11-19T19:15:21.228-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="operator overloading"/><category scheme="http://www.blogger.com/atom/ns#" term="overloading ++"/><title type='text'>Operator Overloading - II</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Let us consider some special cases of operator
overloading&lt;br /&gt;
&lt;/span&gt;
&lt;br /&gt;
&lt;h3 align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Overloading of subscript
operator:
&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;subscript
operator can be
overloaded to access the dynamically allocated array elements within an
object.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&amp;nbsp;
&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(255, 255, 255) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;Arr&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;arr;
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; size;

&lt;span style=&quot;color: #997700; font-weight: bold;&quot;&gt;public:&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;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt; [](&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; n) ;
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt;[](&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; n) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt;;
&lt;span style=&quot;color: #888888;&quot;&gt;/****code *****/&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;&amp;amp;&lt;/span&gt;Arr&lt;span style=&quot;color: #333333;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt; [](&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; index)
{
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; arr[index];
}

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; Arr&lt;span style=&quot;color: #333333;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt; [](&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; index) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;const&lt;/span&gt;
{
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; arr[index];
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Const&lt;/span&gt; version of
operator
function is written so that constant objects can also access array
elements. Non-constant version is helpful in modifying the array
elements. &lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Make sure
that non-const
version of [] function returns a reference, so that you can assign
values using statement like&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Courier New,Courier,monospace; margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;obj1[3]=99;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;h3 align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Increment and Decrement operator
++ and --:&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;++ and --
have prefix and
postfix versions for basic data types. a++ and ++a.&amp;nbsp; So how do we
overload these operators with
two versions?&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;For
postfix versions of these
functions, we should use a dummy int parameter.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(255, 255, 255) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; 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: 0pt;&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&lt;/pre&gt;
&lt;/td&gt;
      &lt;td&gt;
      &lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;Number&lt;/span&gt;
{
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; num;
&lt;span style=&quot;color: #997700; font-weight: bold;&quot;&gt;public:&lt;/span&gt;
    Number &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;();&lt;span style=&quot;color: #888888;&quot;&gt;/*prefix version*/&lt;/span&gt;
    Number &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; a);&lt;span style=&quot;color: #888888;&quot;&gt;/*postfix version*/&lt;/span&gt;
&lt;span style=&quot;color: #888888;&quot;&gt;/****code***/&lt;/span&gt;
};
Number &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; Number&lt;span style=&quot;color: #333333;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;()
{
    num&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&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;this&lt;/span&gt;;
}

Number Number&lt;span style=&quot;color: #333333;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; dummy)
{
    Number temp &lt;span style=&quot;color: #333333;&quot;&gt;=&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;this&lt;/span&gt;;
    num&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; temp;
}

&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; main()
{
    Number obj(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;9&lt;/span&gt;);
    Number &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;obj2&lt;/span&gt;(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;1000&lt;/span&gt;);
    obj2 &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;obj;&lt;span style=&quot;color: #888888;&quot;&gt;/*calls prefix*/&lt;/span&gt;
    obj &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; obj2&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;;&lt;span style=&quot;color: #888888;&quot;&gt;/*calls postfix*/&lt;/span&gt;
}&lt;/pre&gt;
&lt;/td&gt;
    &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;div style=&quot;font-family: Courier New,Courier,monospace; margin: 0px; text-indent: 0px;&quot;&gt;
&lt;b&gt;&lt;big&gt;&lt;big&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/big&gt;&lt;/big&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;b&gt;&lt;big&gt;&lt;big&gt; &lt;/big&gt;&lt;/big&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;b&gt;&lt;big&gt;&lt;big&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;new operator: &lt;/span&gt;&lt;/big&gt;&lt;/big&gt;&lt;/b&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;New
operator takes size_t
parameter and returns a void pointer. The easy way of overloading new
operator would be &lt;/span&gt;&lt;/div&gt;
&amp;nbsp;
&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(255, 255, 255) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;Number&lt;span style=&quot;color: #333333;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;new&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;size_t&lt;/span&gt; bytes)
{
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;ptr &lt;span style=&quot;color: #333333;&quot;&gt;=&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;new&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;char&lt;/span&gt;[bytes];&lt;span style=&quot;color: #888888;&quot;&gt;/*call global new operator*/&lt;/span&gt;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; (&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;) ptr;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;i&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;i&gt;&lt;b&gt;Note
that compiler provided
new operator for classes will be sufficient in most situations.&lt;/b&gt;&lt;/i&gt;
You
need to overload new and delete operator, only if you want to use your
own block of memory for dynamic allocation. &lt;/span&gt;&lt;/div&gt;
&lt;h3 align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Insertion and Extraction
operators (&amp;lt;&amp;lt; and &amp;gt;&amp;gt;) :&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;These two
operators are used to
read and write objects from &lt;span style=&quot;font-style: italic;&quot;&gt;iostreams
and files&lt;/span&gt;.
These should be &lt;span style=&quot;font-style: italic; font-weight: bold;&quot;&gt;overloaded
as non-member functions&amp;nbsp; because their first parameter will not be
the current class object. &lt;/span&gt;. &amp;gt;&amp;gt; must have &lt;span style=&quot;font-style: italic;&quot;&gt;istream&lt;/span&gt; object as first parameter
and &amp;lt;&amp;lt; must have &lt;span style=&quot;font-style: italic;&quot;&gt;ostream&lt;/span&gt;
object as first parameter. &lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;These two
functions must return reference to
iostream objects in order to use chaining in io operation. And it is
necessary to use second parameter also as reference parameter in
extraction operator as the function modifies object. &lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&amp;nbsp;
&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(255, 255, 255) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;ostream &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;(ostream&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; out, Number &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; obj)
{
    out&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;obj.num;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; out;
}

istream &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;(istream&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; in, Number &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; obj)
{
    in&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;obj.num;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; in;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;h3 align=&quot;left&quot;&gt;
&lt;br /&gt;
&lt;/h3&gt;
&lt;h3 align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Conversion Operators:&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;These are
used to convert
objects to basic data types. They are written without return type.
Instead, data type is the name of the operator. &lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&amp;nbsp;
&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(255, 255, 255) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;Number&lt;/span&gt;
{
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; a;
&lt;span style=&quot;color: #997700; font-weight: bold;&quot;&gt;public:&lt;/span&gt;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;int&lt;/span&gt;()&lt;span style=&quot;color: #888888;&quot;&gt;/*Converts Number object to int*/&lt;/span&gt;
    {
         &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; a;
    }
&lt;span style=&quot;color: #888888;&quot;&gt;/****code*****/&lt;/span&gt;
};&lt;/pre&gt;
&lt;/div&gt;
&lt;h3 align=&quot;left&quot;&gt;
&lt;br /&gt;
&lt;/h3&gt;
&lt;h3 align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Function call operator:&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;parantheses
- () can be
overloaded to create an object which behaves like a function. Such
objects are called function objects or &lt;i&gt;&lt;b&gt;functors&lt;/b&gt;&lt;/i&gt;.&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&amp;nbsp;
&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(255, 255, 255) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;Cube&lt;/span&gt;
{
&lt;span style=&quot;color: #997700; font-weight: bold;&quot;&gt;public:&lt;/span&gt;
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt; ()(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; n)
{
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; n&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;n&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;n;
}
};
&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;()
{
    Cube cobj;
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; m &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; cobj(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt;);&lt;span style=&quot;color: #888888;&quot;&gt;/*this cubes 10 and returns answer*/&lt;/span&gt;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;h3 align=&quot;left&quot;&gt;
&lt;br /&gt;
&lt;/h3&gt;
&lt;h3 align=&quot;center&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Overloading of assignment
operator:&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Compiler
provides overloaded
assignment operator function for a class. But you have to write your
own assignment operator function if you need deep copy.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;Assignment
operator function
looks almost like a copy constructor. You allocate memory and you copy
elements. &lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;But before
that, &lt;b&gt;you should release earlier memory allocated.&lt;/b&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;There is
one more problem
however. What if you use a statement which ultimately becomes &lt;b&gt;self
assignment&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Courier New,Courier,monospace; margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;
ob1 = ob1;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;In this
case, if you delete
memory, all hell breaks loose. &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;To avoid
this problem, &lt;span style=&quot;font-style: italic;&quot;&gt;check for self
assignment&lt;/span&gt;, then
delete memory, allocate new memory and copy elements. &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: Arial;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&amp;nbsp;
&lt;div style=&quot;-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(255, 255, 255) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;A&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; A&lt;span style=&quot;color: #333333;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;(A &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; obj1)
{
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;!=&amp;amp;&lt;/span&gt;obj1)&lt;span style=&quot;color: #888888;&quot;&gt;/*self assignment?*/&lt;/span&gt;
    {
      &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;delete&lt;/span&gt; arr;
      arr &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;[obj1.n];
      &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;n;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
         arr[i] &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; obj1.arr[i];
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&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;this&lt;/span&gt;;
}&lt;/pre&gt;
&lt;/div&gt;
&amp;nbsp;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/3714778029183256595/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/operator-overloading-ii.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/3714778029183256595'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/3714778029183256595'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/operator-overloading-ii.html' title='Operator Overloading - II'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-484626222584882303</id><published>2016-11-19T11:27:00.001-08:00</published><updated>2017-05-30T09:33:50.287-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="overloading"/><title type='text'>Function Overloading in C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;Let us look as abs() function in C. You should abs() for int, fabs() for float, labs() for long. All these functions find absolute value of a number. As two functions can not have same name, C library has so many names for abs function. This is not the case in C++&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;In C++, two or more functions can have the same name if they have
different number of parameters or different types of parameters. These
are called &lt;/span&gt;&lt;i&gt;overloaded functions.&amp;nbsp;
&lt;/i&gt;&lt;br /&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;Overloaded functions must
differ by&lt;/span&gt; &lt;span style=&quot;font-style: italic;&quot;&gt;number of parameters &lt;/span&gt;or
&lt;span style=&quot;font-style: italic;&quot;&gt;types&lt;/span&gt; of parameters or &lt;span style=&quot;font-style: italic;&quot;&gt;both&lt;/span&gt;.&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt; In case of class members,
they can differ by&lt;span style=&quot;font-family: &amp;quot;times&amp;quot; , &amp;quot;times new roman&amp;quot; , serif;&quot;&gt; &lt;span style=&quot;font-style: italic;&quot;&gt;const&lt;/span&gt;-ness.
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;!-- HTML generated using hilite.me --&gt;

&lt;br /&gt;
&lt;div style=&quot;background: rgb(255 , 255 , 255) none repeat scroll 0%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;sum&lt;/span&gt;(&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; a,&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; b) {return a+b;}
&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;sum&lt;/span&gt;(&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; a,&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; b,&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; c){ return a+b+c;}
&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;float&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;sum&lt;/span&gt;(&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;float&lt;/span&gt; m,&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;float&lt;/span&gt; n) {return m+n;}&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;These three are all
overloaded
functions sum. &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;Two functions &lt;b&gt;can not &lt;/b&gt;be
overloaded, if they differ only by their &lt;b&gt;return types.&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;/div&gt;
&lt;!-- HTML generated using hilite.me --&gt;

&lt;br /&gt;
&lt;div style=&quot;background: rgb(255 , 255 , 255) none repeat scroll 0%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; product(&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; a,&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; b);
&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;float&lt;/span&gt; product(&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; a,&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; b);&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;/*&lt;/span&gt;invalid&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;*/&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;When an overloaded function
is
called, the compiler calls a version based on type of arguments and
number of arguments. &lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;/div&gt;
&lt;div style=&quot;background: rgb(255 , 255 , 255) none repeat scroll 0%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; x,&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;int&lt;/span&gt; y;

x&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;=&lt;/span&gt;y&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0 , 0 , 221); font-weight: bold;&quot;&gt;33&lt;/span&gt;;
x &lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;sum&lt;/span&gt;(x,y,x&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: rgb(0 , 0 , 221); font-weight: bold;&quot;&gt;4&lt;/span&gt;);&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;/*&lt;/span&gt;calls &lt;span style=&quot;color: rgb(0 , 0 , 221); font-weight: bold;&quot;&gt;3&lt;/span&gt; parameter &lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;sum&lt;/span&gt; function&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;*/&lt;/span&gt;

&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;float&lt;/span&gt; m&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;sum&lt;/span&gt;(&lt;span style=&quot;color: rgb(102 , 0 , 238); font-weight: bold;&quot;&gt;8.9&lt;/span&gt;f,&lt;span style=&quot;color: rgb(102 , 0 , 238); font-weight: bold;&quot;&gt;1.2&lt;/span&gt;f);&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;/*&lt;/span&gt;calls &lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;float&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;sum&lt;/span&gt; function&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;*/&lt;/span&gt;

double n&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(0 , 112 , 32);&quot;&gt;sum&lt;/span&gt;(&lt;span style=&quot;color: rgb(102 , 0 , 238); font-weight: bold;&quot;&gt;12.1&lt;/span&gt;,&lt;span style=&quot;color: rgb(102 , 0 , 238); font-weight: bold;&quot;&gt;3.2&lt;/span&gt;);&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;/*&lt;/span&gt;ambiguity error&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;*/&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;Last line produces compiler
error
because the compiler does not know whether to convert parameter to
float or int.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;As you can see, some times, the overloaded functions are very similar except for types of parameters. In such cases, we can use template functions which are generic functions.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;C++ also has another type of overloading for classes. That is operator overloading where operator behavior is modified for the given class.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;Get these notes &lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;in your mobi&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;le. Do&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;wnload my&lt;a href=&quot;https://play.google.com/store/apps/details?id=com.hegdeapps.cppquestions&quot;&gt; C++ Notes&lt;/a&gt; app. &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/484626222584882303/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/function-overloading-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/484626222584882303'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/484626222584882303'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/function-overloading-in-c.html' title='Function Overloading in C++'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-3000532637513810011</id><published>2016-11-19T11:14:00.003-08:00</published><updated>2016-11-29T23:00:24.177-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="reference in C++"/><title type='text'>Reference variables in C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;In C language, we have call by value and call by pointer of functions. Both have their own drawbacks. C++ provides a better solution - call by reference.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;A reference variable in C++&amp;nbsp; is an alias for another variable.
&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;Such a variable looks like an
ordinary variable but behaves like a pointer.&amp;nbsp; It always refers to another variable.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;In function
calls, reference parameter modifies the actual argument, without the hassle of addresses
and indirections.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-align: center; text-indent: 0px;&quot;&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;Defining a reference variable&amp;nbsp; &lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;A reference variable is defined with &lt;i&gt;&lt;b&gt;&amp;amp;&lt;/b&gt;&lt;/i&gt; along with its name. It should always be initialized during definition.&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&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;&amp;amp;&lt;/span&gt;b &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; a;&lt;span style=&quot;color: #888888;&quot;&gt;/*definition of a reference*/&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;b is a reference variable and
it refers to a. That is b is another name for a.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;Functions with reference parameters&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;A function with reference parameter changes the actual argument because the parameter is an alias for argument. In case of large structs and classes, it is better to send a reference parameter, as it avoids copying of data.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&quot;&gt;&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;triple&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;&amp;amp;&lt;/span&gt;a)
{
    a &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: #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; m &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt;;
  triple(m);&lt;span style=&quot;color: #888888;&quot;&gt;/*changes m to 30*/&lt;/span&gt;
  cout&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;m;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;In the above example, m is sent as a reference parameter. So m is changed by the function triple().&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;If the function needs a reference parameter for efficiency,&amp;nbsp; but the function should not change the argument, you should define parameter as constant reference.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-weight: bold; margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;Notes&lt;/span&gt;&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;&amp;nbsp;A reference should always be
initialized to another variable while defining it.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;
&lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp; A reference variable can
not refer to a different variable later.&lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;
&lt;/span&gt;&lt;ol&gt;&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;i.e.&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt; b = c ;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;will not make &lt;span style=&quot;font-style: italic;&quot;&gt;b&lt;/span&gt; refer to &lt;span style=&quot;font-style: italic;&quot;&gt;c&lt;/span&gt;. Instead it assigns &lt;span style=&quot;font-style: italic;&quot;&gt;c&lt;/span&gt; to &lt;span style=&quot;font-style: italic;&quot;&gt;a&lt;/span&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/ol&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;
&lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp; A reference variable
should always be initialized when defining.&lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;
&lt;/span&gt;&lt;ol&gt;&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;float &amp;amp;m1 =
m2; /*m1 is initialized to m2*/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/ol&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;
&lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp; An array of reference
variables is not possible&lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;
&lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp; If a function has a
reference parameter, changes to this reference parameter will change
the actual argument.&lt;/span&gt;&lt;/li&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;
&lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;If a function returns a
reference value, that function can be used in LHS of an assignment
statement. &lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;margin: 0px; text-align: center; text-indent: 0px;&quot;&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;Returning a reference &lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div style=&quot;font-family: Courier New,Courier,monospace; margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;A function returning a reference has the advantage that it can be used in LHS of an expression. But you have to make sure that, the return value of such a function is within the scope even when function exits.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&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;&amp;amp;&lt;/span&gt;fn1(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; m,&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; n)
{
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(m&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;n) &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; m; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; n;
}
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; main()
{
  &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; a1 &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;,b1&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;12&lt;/span&gt;;
  fn1(a1,b1) &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;100&lt;/span&gt;;
}  
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;helvetica&amp;quot; , sans-serif;&quot;&gt;Here main fn1() returns a reference to b1. So b1 will be set to 100 in the assignment statement of main().&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/3000532637513810011/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/reference-variables-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/3000532637513810011'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/3000532637513810011'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/reference-variables-in-c.html' title='Reference variables in C++'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-2187590019705423873</id><published>2016-11-18T22:27:00.000-08:00</published><updated>2016-11-29T23:00:41.136-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="Operator overloading c++"/><title type='text'>Overload () operator</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Let us see how to overload parenthesis operator for matrix class such that matrix(i,j) returns ith row, jth column element of the matrix.&lt;br /&gt;
&lt;br /&gt;
matrix.h &lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#ifndef MATRIX_H&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#define MATRIX_H&lt;/span&gt;

&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;matrix&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;elements;
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; nr,nc;
&lt;span style=&quot;color: #997700; font-weight: bold;&quot;&gt;public:&lt;/span&gt;
   
    matrix();
    &lt;span style=&quot;color: #333333;&quot;&gt;~&lt;/span&gt;matrix();
    &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;getNrows&lt;/span&gt;(){&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; nr;}
    &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;getNcols&lt;/span&gt;(){&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; nc;}
    matrix(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; nr,&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; nc);
    &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt;() (&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; row,&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; col);

};

&lt;span style=&quot;color: #557799;&quot;&gt;#endif &lt;/span&gt;&lt;span style=&quot;color: #888888;&quot;&gt;// MATRIX_H&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
#ifdef preprocessor directive was inserted by codelite IDE. What it does is to protect us from multiple inclusion of the same file. If matrix_h is not defined then only include its declaration.&lt;br /&gt;
&lt;br /&gt;
Next let is&amp;nbsp; look at data elements of the class viz nr,nc and elements. nr and nc are number of rows and columns of the matrix. Notice that these have only getters not setters because once a matrix is defined, you should not be able to change nr or nc.&lt;br /&gt;
&lt;br /&gt;
Elements is where the elements of matrix are stored. I am using a one dimensional dynamic int array. So each access of element will have to be row*number of columns + col.&lt;br /&gt;
&lt;br /&gt;
The class has a default constructor and constructor with 2 parameters - nr and nc.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;

&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&quot;&gt;matrix&lt;span style=&quot;color: #333333;&quot;&gt;::&lt;/span&gt;matrix()
{
    nr &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; nc &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: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;elements &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;[nr&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;nc];
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;nr&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;nc;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
    {
        elements[i]&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    }
}

matrix&lt;span style=&quot;color: #333333;&quot;&gt;::~&lt;/span&gt;matrix()
{
}
matrix&lt;span style=&quot;color: #333333;&quot;&gt;::&lt;/span&gt;matrix(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; nr,&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; nc)
{
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;nr &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; nr;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;nc &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; nc;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;elements &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt;[nr&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;nc];
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;nr&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;nc;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
    {
        elements[i]&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
    }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Constructors allocate memory to array. And initialize elements to 0. We have totally nr*nc elements in the matrix.&lt;br /&gt;
&lt;br /&gt;
Now we should see how to overload () operator for this class.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&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;&amp;amp;&lt;/span&gt; matrix&lt;span style=&quot;color: #333333;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;operator&lt;/span&gt;()(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i,&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; j)
{
   &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; index &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;nc;
   index&lt;span style=&quot;color: #333333;&quot;&gt;+=j&lt;/span&gt;;
   &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; elements[index];
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
The function just returns ith row and jth column element from the matrix. And index is i*numberOfColumns+j because my implementation is using 1 d array.&lt;br /&gt;
&lt;br /&gt;
But a crucial point to remember is return type of operator function. It is int reference. Not just int. Because we must be able to assign values to the elements of the matrix with the help of this operator.&amp;nbsp; So the function returns &lt;b&gt;&lt;i&gt;reference &lt;/i&gt;&lt;/b&gt;to ith row jth column element of the matrix.&lt;br /&gt;
&lt;br /&gt;
Let us use it in action.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&quot;&gt;&lt;span style=&quot;color: #557799;&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include&quot;matrix.h&quot;&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;( )
{
    matrix m(&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;,&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;);
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;j&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;3&lt;/span&gt;;j&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
        {
            &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; temp;
            cout&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Value:&quot;&lt;/span&gt;;
            cin&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;temp;
            m(i,j)&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;temp;
        }
       
      &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;m.getNrows();i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
      {
        &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;j&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;m.getNcols();j&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
        {
            cout&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;m(i,j)&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;  &quot;&lt;/span&gt;;
        }  
        cout&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;endl;
      }      
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
In main function we are creating a matrix of 3X3, reading values into it and displaying it back.&lt;br /&gt;
&lt;br /&gt;
Download the complete program from &lt;a href=&quot;https://drive.google.com/file/d/0B2yEYnooo9GXNDY5MmswamJjQUU/view?usp=sharing&quot;&gt;here&amp;nbsp; &lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/2187590019705423873/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/overload-operator.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/2187590019705423873'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/2187590019705423873'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/overload-operator.html' title='Overload () operator'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-6962872760721397720</id><published>2016-11-17T22:59:00.000-08:00</published><updated>2016-11-17T22:59:34.569-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="static"/><category scheme="http://www.blogger.com/atom/ns#" term="static data in c++"/><category scheme="http://www.blogger.com/atom/ns#" term="static function in c+"/><title type='text'>Static members in C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 align=&quot;center&quot;&gt;
&lt;big&gt;Static data member&lt;/big&gt;
&lt;/h3&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
A static data member belongs
to the class, not to any object. It is shared by all the objects of the
class.&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
To make a data member static,
it should be defined with keyword static inside the class body. &lt;span style=&quot;font-style: italic;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;span style=&quot;font-style: italic;&quot;&gt;A static data MUST also be defined outside
the body of the class because inside class, the declaration of static
data is not definition. &lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background: rgb(255 , 255 , 255) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;SimpleInterest&lt;/span&gt;
{
 &lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;double&lt;/span&gt; rate;
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; amount;
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;float&lt;/span&gt; period;
&lt;span style=&quot;color: rgb(153 , 119 , 0); font-weight: bold;&quot;&gt;public:&lt;/span&gt;
 &lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/*funnctions*/&lt;/span&gt;
};
&lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;double&lt;/span&gt; SimpleInterest&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;::&lt;/span&gt;rate&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(102 , 0 , 238); font-weight: bold;&quot;&gt;0.07&lt;/span&gt;;&lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/*definition*/&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
Static data members can be
accessed using dot or arrow operator but they &lt;span style=&quot;font-style: italic;&quot;&gt;can also be accessed without an object&lt;/span&gt;,
using class name followed by scope resolution operator.&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background: rgb(255 , 255 , 255) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 102 , 187); font-weight: bold;&quot;&gt;main&lt;/span&gt;()
{
 SimpleInterest&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;::&lt;/span&gt;rate&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: rgb(102 , 0 , 238); font-weight: bold;&quot;&gt;0.08&lt;/span&gt;;
 SimpleInterest obj;
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;float&lt;/span&gt; intrst &lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;=&lt;/span&gt; obj.amount&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;*&lt;/span&gt;obj.period&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;*&lt;/span&gt;obj.rate;
 cout&amp;lt;&amp;lt;intrst;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 align=&quot;center&quot;&gt;
&lt;big&gt;Static member functions&lt;/big&gt;&lt;/h3&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
A static member function can
be called directly without using any object,&amp;nbsp; with class name and
scope resolution operator.&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
A function is made static by
using static keyword within the class body in function declaration.&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
Rules for static functions.&lt;/div&gt;
&lt;ol&gt;
&lt;/ol&gt;
&lt;ol&gt;
&lt;li&gt;A static function can not use &quot;&lt;span style=&quot;font-style: italic; font-weight: bold;&quot;&gt;this&lt;/span&gt;&quot; pointer.
Hence it can &lt;span style=&quot;font-style: italic;&quot;&gt;not&lt;/span&gt; access &lt;span style=&quot;font-style: italic;&quot;&gt;non-static&lt;/span&gt; data members.&lt;/li&gt;
&lt;li&gt; A static function can &lt;span style=&quot;font-style: italic;&quot;&gt;not&lt;/span&gt;
be &lt;span style=&quot;font-style: italic;&quot;&gt;const&lt;/span&gt; &lt;/li&gt;
&lt;li&gt; Static function can &lt;span style=&quot;font-style: italic;&quot;&gt;not&lt;/span&gt;
be &lt;span style=&quot;font-style: italic;&quot;&gt;virtual&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background: rgb(255 , 255 , 255) none repeat scroll 0% 50%; border-color: gray; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
&lt;pre style=&quot;line-height: 125%; margin: 0pt;&quot;&gt;&lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(187 , 0 , 102); font-weight: bold;&quot;&gt;A&lt;/span&gt;
{
 &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; n;
 static int m;
&lt;span style=&quot;color: rgb(153 , 119 , 0); font-weight: bold;&quot;&gt;public:&lt;/span&gt;
 A(&lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; m);
 &lt;span style=&quot;color: rgb(0 , 136 , 0); font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(0 , 102 , 187); font-weight: bold;&quot;&gt;print&lt;/span&gt;() ;&lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/*static function*/&lt;/span&gt;
};
&lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; A::m=4;

&lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;void&lt;/span&gt; A&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;::&lt;/span&gt;print()
{
 cout&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Hello world&quot;&lt;/span&gt;;
 cout&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;n;&lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/*error. Can not access n*/&lt;/span&gt;
 cout&amp;lt;&amp;lt;m;&lt;span style=&quot;color: rgb(136 , 136 , 136);&quot;&gt;/*no error. m is static*/&lt;/span&gt;}
&lt;span style=&quot;color: rgb(51 , 51 , 153); font-weight: bold;&quot;&gt;int&lt;/span&gt; main()
{
 A&lt;span style=&quot;color: rgb(51 , 51 , 51);&quot;&gt;::&lt;/span&gt;print();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
print() function of A class, which is
static can not access n, as n is non-static data. &lt;/div&gt;
&lt;div style=&quot;margin: 0px; text-indent: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/6962872760721397720/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/static-members-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6962872760721397720'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6962872760721397720'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/static-members-in-c.html' title='Static members in C++'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-4556174603777550106</id><published>2016-11-17T08:06:00.001-08:00</published><updated>2016-11-17T08:06:31.068-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="Stack"/><category scheme="http://www.blogger.com/atom/ns#" term="templates in C++"/><title type='text'>Stack template class in C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Today let us write a template class Stack. Stack is a data structure of LIFO - last in first out. But of course, STL (standard template library) has a stack class in it and you can use it.&lt;br /&gt;
&lt;br /&gt;
But still let us write our own Stack.&lt;br /&gt;
&lt;br /&gt;
Stack needs the following methods - push, pop, empty and peek or top. push method add a value to the top of the stack. Pop removes a value from the top of the stack. empty finds out whether the stack is empty. peek returns the value from top of the stack without actually removing it.&lt;br /&gt;
&lt;br /&gt;
Did you know that function call in most of these languages make use of Stack data structure for managing call frames. That is the reason, a return will return from the most recently called function.&lt;br /&gt;
&lt;br /&gt;
Anyways. How do we implement the stack. We can use a vector or a deque from STL. If we want to use basic data types, we can use an array or our own linked list. Array will be limited by size of the array. So linked list?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node
{
    T num;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;next;
};

&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;Stack&lt;/span&gt;
{
       node &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;*&lt;/span&gt;topnode;
&lt;span style=&quot;color: #997700; font-weight: bold;&quot;&gt;public:&lt;/span&gt;
      Stack();
      &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;empty&lt;/span&gt;();
      &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;push&lt;/span&gt;(T data);
      T &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;pop&lt;/span&gt;();
      T &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; top();
};
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Why templates? Because our class becomes flexible and can be used for most data types.&lt;br /&gt;
&lt;br /&gt;
We have &lt;b&gt;self referential structure&lt;/b&gt; &lt;i&gt;&lt;b&gt;node &lt;/b&gt;&lt;/i&gt;with data and link to the next node. Data is a template variable. So it can be int, float, string etc.&lt;br /&gt;
&lt;br /&gt;
Next we have class Stack which has one data parameter topnode or top of the stack. And we must have constructor and the four methods.&lt;br /&gt;
&lt;br /&gt;
Constructor should initialize topofstack or topnode to 0. If you have written a similar program in C, you would have set it to NULL pointer. C++ prefers plain 0 for pointer rather than predefined constant 0. You know that NULL is nothing but a pointer with 0 value.&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
Stack&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;::&lt;/span&gt;Stack()
{
    topnode &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;&lt;span style=&quot;color: #888888;&quot;&gt;/*initialize*/&lt;/span&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
For push function, we have to create a new node using dynamic memory allocation. and place this node on top of stack. You should perform these operation in push operation. &lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;create a new node&amp;nbsp;&lt;/li&gt;
&lt;li&gt;newnode-&amp;gt;next = topnode&lt;/li&gt;
&lt;li&gt;topnode = newnode&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;void&lt;/span&gt; Stack&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;::&lt;/span&gt;push(T data)
{
     node &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;*&lt;/span&gt;newnode &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;new&lt;/span&gt; node&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;;
     newnode&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;num &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; data;
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(topnode){
         newnode&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;next &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; topnode;
     }&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
         newnode&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;next &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
     }
     topnode &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; newnode;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Similarly in pop operation, we have to perform&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;copy topnode to temp&lt;/li&gt;
&lt;li&gt;topnode = topnode-&amp;gt;next&amp;nbsp;&lt;/li&gt;
&lt;li&gt;copy value in temp to num&lt;/li&gt;
&lt;li&gt;delete temp&lt;/li&gt;
&lt;li&gt;return num&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
T Stack&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;::&lt;/span&gt;pop( )
{
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(&lt;span style=&quot;color: #333333;&quot;&gt;!&lt;/span&gt;empty())
        {
            node&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; temp &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; topnode;
            topnode &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; topnode&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;next;
            T num &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; temp&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;num;
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;delete&lt;/span&gt; temp;
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; num;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
        {
             &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; (T)&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
        }
}
&lt;/pre&gt;
&lt;/div&gt;
Empty is a simple function, you will just check if topnode is 0. If it is zero Stack is empty&lt;br /&gt;
&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&quot;&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;bool&lt;/span&gt; Stack&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;::&lt;/span&gt;empty()
{
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; topnode&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;/div&gt;
But since all these functions are part of template class, they all must have template header before them.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Remember: For a template class, the class definition and &lt;b&gt;also &lt;/b&gt;function definitions must be in header file.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Complete Program&lt;br /&gt;
&lt;!-- HTML generated using hilite.me --&gt;&lt;br /&gt;
&lt;div style=&quot;background: #ffffff; 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: 0;&quot;&gt;Stack.h

&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node
{
    T num;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt;next;
};

&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;Stack&lt;/span&gt;
{
       node &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;*&lt;/span&gt;topnode;
&lt;span style=&quot;color: #997700; font-weight: bold;&quot;&gt;public:&lt;/span&gt;
      Stack();
      &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;bool&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;empty&lt;/span&gt;();
      &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;push&lt;/span&gt;(T data);
      T &lt;span style=&quot;color: #0066bb; font-weight: bold;&quot;&gt;pop&lt;/span&gt;();
      T &lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; top();
};


&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
Stack&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;::&lt;/span&gt;Stack()
{
    topnode &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;&lt;span style=&quot;color: #888888;&quot;&gt;/*initialize*/&lt;/span&gt;
}
&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;bool&lt;/span&gt; Stack&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;::&lt;/span&gt;empty()
{
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; topnode&lt;span style=&quot;color: #333333;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
}

&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;void&lt;/span&gt; Stack&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;::&lt;/span&gt;push(T data)
{
     node &lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;*&lt;/span&gt;newnode &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;new&lt;/span&gt; node&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;;
     newnode&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;num &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; data;
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(topnode){
         newnode&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;next &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; topnode;
     }&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;{
         newnode&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;next &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
     }
     topnode &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; newnode;
}

&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
T Stack&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;::&lt;/span&gt;pop( )
{
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(&lt;span style=&quot;color: #333333;&quot;&gt;!&lt;/span&gt;empty())
        {
            node&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;*&lt;/span&gt; temp &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; topnode;
            topnode &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; topnode&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;next;
            T num &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; temp&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;num;
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;delete&lt;/span&gt; temp;
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; num;
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
        {
             &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; (T)&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
        }
}

&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #bb0066; font-weight: bold;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt;
T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;amp;&lt;/span&gt; Stack&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;::&lt;/span&gt;top( )
{
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;if&lt;/span&gt;(&lt;span style=&quot;color: #333333;&quot;&gt;!&lt;/span&gt;empty())
        {                        
            T num &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; topnode&lt;span style=&quot;color: #333333;&quot;&gt;-&amp;gt;&lt;/span&gt;num;             
            &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; num;
        }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;else&lt;/span&gt;
        {
             &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;return&lt;/span&gt; (T)&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;
        }
}

main.cpp
&lt;span style=&quot;color: #557799;&quot;&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #557799;&quot;&gt;#include&quot;Stack.h&quot;&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; argc, &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: #333333;&quot;&gt;**&lt;/span&gt;argv)
{
 Stack&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&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;&amp;gt;&lt;/span&gt; s1;
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;for&lt;/span&gt;(&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;0&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #0000dd; font-weight: bold;&quot;&gt;10&lt;/span&gt;;i&lt;span style=&quot;color: #333333;&quot;&gt;++&lt;/span&gt;)
    {
        &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; m;
        cin&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;m;
        s1.push(m);
    }
    &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(&lt;span style=&quot;color: #333333;&quot;&gt;!&lt;/span&gt;s1.empty())
    {
        &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;int&lt;/span&gt; a;
        a &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; s1.pop();
        cout&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;Popped value is &quot;&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;a&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;;
    }
    Stack&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&amp;gt;&lt;/span&gt; s2;
    s2.push(&lt;span style=&quot;color: #0044dd;&quot;&gt;&#39;c&#39;&lt;/span&gt;);
    s2.push(&lt;span style=&quot;color: #0044dd;&quot;&gt;&#39;+&#39;&lt;/span&gt;);
    s2.push(&lt;span style=&quot;color: #0044dd;&quot;&gt;&#39;+&#39;&lt;/span&gt;);
    
     &lt;span style=&quot;color: #008800; font-weight: bold;&quot;&gt;while&lt;/span&gt;(&lt;span style=&quot;color: #333333;&quot;&gt;!&lt;/span&gt;s2.empty())
    {
        &lt;span style=&quot;color: #333399; font-weight: bold;&quot;&gt;char&lt;/span&gt; a;
        a &lt;span style=&quot;color: #333333;&quot;&gt;=&lt;/span&gt; s2.pop();
        cout&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; a&lt;span style=&quot;color: #333333;&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0; color: #666666; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;background-color: #fff0f0;&quot;&gt;&quot;&lt;/span&gt;;
    }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/4556174603777550106/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/stack-template-class-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/4556174603777550106'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/4556174603777550106'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/stack-template-class-in-c.html' title='Stack template class in C++'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-8307680724812341858</id><published>2016-11-13T06:29:00.001-08:00</published><updated>2016-11-13T06:30:54.182-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="c++ constructor"/><category scheme="http://www.blogger.com/atom/ns#" term="c++ program"/><title type='text'>C++ program with constructor and constant function</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Today let us write a program with class, member methods and constructor.&lt;br /&gt;
&lt;br /&gt;
Q:&lt;br /&gt;
Write a class point with two data members x and y. Write getters and setters for these. Now create an array of 10 points and print the farthest point from the origin.&lt;br /&gt;
&lt;br /&gt;
Point.h&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: &amp;quot;andale mono&amp;quot; , &amp;quot;lucida console&amp;quot; , &amp;quot;monaco&amp;quot; , &amp;quot;fixed&amp;quot; , monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;class point
{
    float x;
    float y;
public:
    point(float m=0,float n=0);
    float  getx() const;
    float  gety() const;
    void setx(const float &amp;amp; a);
    void sety(const float &amp;amp; b);
};
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Point.cpp&lt;/div&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: &amp;quot;andale mono&amp;quot; , &amp;quot;lucida console&amp;quot; , &amp;quot;monaco&amp;quot; , &amp;quot;fixed&amp;quot; , monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;#include&quot;Point.h&quot;
point::point(float a,float b):x(a),y(b)
{
}
float   point::getx() const
{
    return x;
}
float   point::gety() const
{
    return y;
}
void point::setx(const float &amp;amp;a)  
{
   x = a;
}
void point::sety(const float &amp;amp;b)  
{
   y = b;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
main.cpp
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: &amp;quot;andale mono&amp;quot; , &amp;quot;lucida console&amp;quot; , &amp;quot;monaco&amp;quot; , &amp;quot;fixed&amp;quot; , monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;#include&amp;lt;iostream&amp;gt;
#include&quot;Point.h&quot;
#include&amp;lt;cmath&amp;gt;
using std::cin;
using std::cout;
 
int main()
{
    point pobj[4];
    for(int i=0;i&amp;lt;4;i++)
    {
        cout&amp;lt;&amp;lt;&quot;Enter x and y of point&quot;&amp;lt;&amp;lt;i&amp;lt;&amp;lt;&quot;:&quot;;
        float x1,y1;
        cin&amp;gt;&amp;gt;x1&amp;gt;&amp;gt;y1;
        pobj[i].setx(x1);
        pobj[i].sety(y1);
    }
    /*find farthest point*/
    float largest_dist = -1;
    int index = -1;
    for(int i=0;i&amp;lt;4;i++)
     {
         float squared_dist = pobj[i].getx()*pobj[i].getx() + pobj[i].gety()*pobj[i].gety();
         float dist_from_origin = sqrt(squared_dist);
         if(dist_from_origin&amp;gt;largest_dist)
         {
               largest_dist = dist_from_origin;
               index = i;
         }
     }
     cout&amp;lt;&amp;lt;&quot;The farthest point is (&quot;&amp;lt;&amp;lt;pobj[index].getx()&amp;lt;&amp;lt;&quot;,&quot;&amp;lt;&amp;lt;pobj[index].gety()&amp;lt;&amp;lt;&quot;)&quot;;
     return 0;
}
               
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Now let us try to understand the code.&lt;br /&gt;
&lt;br /&gt;
Point class has x and y as private data members. Which means main() function or any other outside function can not directly access the code. So the getters and setters. getter returns the data member. And setter changes it.&lt;br /&gt;
&lt;br /&gt;
Let us look at getx() and gety(). They return data members x and y respectively. x and y of which Point. The point from where the function is called. &lt;i&gt;&lt;b&gt;this &lt;/b&gt;&lt;/i&gt;point. If you want you can add this pointer to the code. &lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: &amp;quot;andale mono&amp;quot; , &amp;quot;lucida console&amp;quot; , &amp;quot;monaco&amp;quot; , &amp;quot;fixed&amp;quot; , monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;float   point::getx() const
{
    return x;//or return this-&amp;gt;x;
}
float   point::gety() const
{
    return y;
}&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
But why are these functions &lt;b&gt;constants&lt;/b&gt;? Because getx and gety need not and should not change x and y co-ordinate. So making the function &lt;i&gt;const &lt;/i&gt;ensures this. In &lt;i&gt;const function, if state of an object is changed, there will be a compiler error.&amp;nbsp;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&amp;nbsp; &lt;/i&gt;&lt;br /&gt;
But setx() and sety() are not const functions. Why? Because they have to change x and y.&lt;br /&gt;
&lt;br /&gt;
Look at a modified main function. We have created a constant point p1 and called getx and gety on this constant object. No error. But when we call setx() on p1, there will be compiler error. Because a constant object can call only constant functions. &lt;br /&gt;
&lt;i&gt; &lt;/i&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: &amp;quot;andale mono&amp;quot; , &amp;quot;lucida console&amp;quot; , &amp;quot;monaco&amp;quot; , &amp;quot;fixed&amp;quot; , monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;int main()
{
   const Point p1(10,3);
   cout&amp;lt;&amp;lt;p1.getx()&amp;lt;&amp;lt;&quot;,&quot;&amp;lt;&amp;lt;p1.gety();//no error
   float m;
   cin&amp;gt;&amp;gt;m;
   p1.setx(m);//error
   .....
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Now let us look at the constructor&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: &amp;quot;andale mono&amp;quot; , &amp;quot;lucida console&amp;quot; , &amp;quot;monaco&amp;quot; , &amp;quot;fixed&amp;quot; , monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;point::point(float a,float b):x(a),y(b)
{
}
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The constructor is very simple. It takes two parameters a and b. And&amp;nbsp; assigns x to a and y to b using member initializer list&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;.x(a) , y(b)&lt;/span&gt; is equivalent to saying &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;x=a; &lt;/span&gt;and &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;y=b;&lt;/span&gt; inside the constructor&lt;br /&gt;
&lt;br /&gt;
Member initializer list is faster than assignments and in case of &lt;b&gt;constant &lt;/b&gt;data members, you should use member initializer. If x is const float, the x=a; in constructor throws a compilation error.&lt;br /&gt;
&lt;br /&gt;
In main function, we are creating an array of 10 points and reading their x an y using a for loop. No, we are reading a temporary variable x1 and use this in setx function.&lt;br /&gt;
&lt;br /&gt;
A simple&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: &amp;quot;andale mono&amp;quot; , &amp;quot;lucida console&amp;quot; , &amp;quot;monaco&amp;quot; , &amp;quot;fixed&amp;quot; , monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
cin&amp;gt;&amp;gt;pobj[i].x 
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
will throw compilation error. Because x is private. But won&#39;t it be simpler to make x and y as public and directly read and write them. NO. Because making data public violates the principle of Encapsulation and any future change in implementation affects other code which use this class.&amp;nbsp; Always make data members as private&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: &amp;quot;andale mono&amp;quot; , &amp;quot;lucida console&amp;quot; , &amp;quot;monaco&amp;quot; , &amp;quot;fixed&amp;quot; , monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;    &lt;b&gt;point pobj[4];&lt;/b&gt;
    for(int i=0;i&amp;lt;4;i++)
    {
        cout&amp;lt;&amp;lt;&quot;Enter x and y of point&quot;&amp;lt;&amp;lt;i&amp;lt;&amp;lt;&quot;:&quot;;
        float x1,y1;
        cin&amp;gt;&amp;gt;x1&amp;gt;&amp;gt;y1;
        pobj[i].setx(x1);
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Which constructor do array elements of pobj use here? We know that if a class has any user defined constructor, then compiler does not provide default constructor.&lt;br /&gt;
&lt;br /&gt;
If we look at Point.h&lt;br /&gt;
&amp;nbsp;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: &amp;quot;andale mono&amp;quot; , &amp;quot;lucida console&amp;quot; , &amp;quot;monaco&amp;quot; , &amp;quot;fixed&amp;quot; , monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
class point
{
    float x;
    float y;
public:
    point(float &lt;b&gt;m=0&lt;/b&gt;,float &lt;b&gt;n=0&lt;/b&gt;);
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
we see that parameters m and n have default values 0 in the constructor. So the 2 parameter constructor behaves like default constructor (constructor with 0 parameters) and is called when initialing array elements.&lt;br /&gt;
&lt;br /&gt;
The rest of the code is simple finding a largest element in an array kind of stuff.&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/8307680724812341858/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/c-program-with-constructor-and-constant.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/8307680724812341858'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/8307680724812341858'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/c-program-with-constructor-and-constant.html' title='C++ program with constructor and constant function'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-6115470650458218038</id><published>2016-11-08T03:34:00.001-08:00</published><updated>2016-11-08T03:34:41.798-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="bool in c++"/><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="cin and cout"/><title type='text'>Input Output operation in C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In C++, Input Output operations are performed using two objects -&amp;nbsp;
&lt;span style=&quot;font-style: italic;&quot;&gt;cin&lt;/span&gt; and &lt;span style=&quot;font-style: italic;&quot;&gt;cout&lt;/span&gt;. Note that they are not
functions but objects. They use overloaded operators &amp;gt;&amp;gt; and
&amp;lt;&amp;lt;. c here indicates console.&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;
&lt;big&gt;&lt;big&gt;cin&lt;/big&gt;&lt;/big&gt;&lt;br /&gt;
cin&amp;nbsp; is an &lt;span style=&quot;font-style: italic;&quot;&gt;istream&lt;/span&gt;
object. &lt;/b&gt;Any&amp;nbsp; variable like int, float,
double or char array is read from console using cin object and&amp;nbsp;
extraction operator(&amp;gt;&amp;gt;). cin can also read user defined objects
if their classes overload extraction operator.&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;span style=&quot;font-family: &amp;quot;courier&amp;quot;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;pre&gt;&lt;span style=&quot;font-family: &amp;quot;courier&amp;quot;;&quot;&gt;#include&amp;lt;iostream&amp;gt;
using namespace std;
int main() 
{ 
 int a; float b; int&amp;nbsp; c; 
 cin&amp;gt;&amp;gt;a; 
 cin&amp;gt;&amp;gt;b&amp;gt;&amp;gt;c;

}&lt;/span&gt;&lt;/pre&gt;
&lt;span style=&quot;font-family: &amp;quot;courier&amp;quot;;&quot;&gt;
&lt;/span&gt;
Unlike scanf which has a complex syntax, cin does not use ampersand
operator ( &amp;amp; ) and it does
not need format specifiers like %d, %f.. To read multiple variables,
&amp;gt;&amp;gt;
operator is &lt;b&gt;chained&lt;/b&gt;, comma is not used.&lt;br /&gt;
&lt;br /&gt;
&lt;big&gt;&lt;big&gt;&lt;b&gt;cout&lt;/b&gt;&lt;/big&gt;&lt;/big&gt;&lt;br /&gt;
&lt;b&gt;cout is an object of &lt;span style=&quot;font-style: italic;&quot;&gt;ostream&lt;/span&gt;
class &lt;/b&gt;and values can be written on console
using cout and insertion operator(&amp;lt;&amp;lt;). cout also does not need
format specifiers.&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;cout&amp;lt;&amp;lt;&quot;Hello
world&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;cout&amp;lt;&amp;lt;a&amp;lt;&amp;lt;b&amp;lt;&amp;lt;c;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;big&gt;&lt;big&gt;&lt;b&gt;endl&lt;/b&gt;&lt;/big&gt;&lt;/big&gt;&lt;br /&gt;
endl is an io manipulator. It prints the character &lt;i&gt;&lt;b&gt;\n&lt;/b&gt;&lt;/i&gt;
and flushes the stream.&lt;br /&gt;
&lt;br /&gt;
&lt;big&gt;&lt;b&gt;Other IO manipulators&lt;/b&gt;&lt;/big&gt;&lt;br /&gt;
C++ has a large set of io manipulators. Some of the main ones are &lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;setw(n) and width(n) - set the width for subsequent fields. &lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;left and right - justify the field to left or right. Only work
after setw() or width()&lt;/li&gt;
&lt;li&gt;setfill(ch) - if the field does not fill the width, ch is used to
fill empty spaces&lt;/li&gt;
&lt;li&gt;setprecision(n) - sets the number of digits printed after decimal
point for floating point values&lt;/li&gt;
&lt;li&gt;boolalpha - shows the boolean values as true and false instead of
1 and 0&lt;/li&gt;
&lt;li&gt;noboolalpha - shows boolean values as 1 and 0&lt;/li&gt;
&lt;li&gt;skipws and noskipws - by default white spaces are skipped when
reading character input. noskipws will not skip white spaces.&lt;/li&gt;
&lt;li&gt;hex, oct, dec - displays integers in the given numer system&lt;/li&gt;
&lt;li&gt;setbase(8/10/6) - sets the base for number. Similar to hex, oct
and dec&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;
int n = 3;&lt;br /&gt;
cout&amp;lt;&amp;lt;&quot;|&quot;&amp;lt;&amp;lt;setw(10)&amp;lt;&amp;lt;n&amp;lt;&amp;lt;endl;&lt;br /&gt;
int m = 88;&lt;br /&gt;
cout&amp;lt;&amp;lt;hex&amp;lt;&amp;lt;m;//prints 58&lt;br /&gt;
bool b = true;&lt;br /&gt;
cout&amp;lt;&amp;lt;boolalpha&amp;lt;&amp;lt;b;//prints true&lt;br /&gt;
char str[30];&lt;br /&gt;
cin&amp;gt;&amp;gt;str;&lt;br /&gt;
cout&amp;lt;&amp;lt;&quot;you typed &quot;&amp;lt;&amp;lt;str;//prints only first word of input&lt;br /&gt;
cin.ignore(INT_MAX,&#39;\n&#39;);&lt;br /&gt;
cin.getline(str,29);&lt;br /&gt;
cout&amp;lt;&amp;lt;&quot;you typed &quot;&amp;lt;&amp;lt;str;//prints the entire string&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;By default cin with strings reads only till first white space
and next cin will read next word etc. So if you type &quot;Hello world&quot;,
your output will be &quot;you typed Hello&quot;. To read the complete line of
input, you can use &lt;i&gt;&lt;b&gt;cin.getline(char_array,size)&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Another problem is after reading first word, the buffer has the rest of
the string, So subsequent reads with cin will not wait for user input,
instead take the value from buffer. Buffer can be flushed using &lt;i&gt;&lt;b&gt;cin.ignore(how_many,till_char);&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
If a wrong input is entered, an error flag is set which may cause
infinite loop&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;br /&gt;
&amp;nbsp;while(m!=-1)&lt;br /&gt;
&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cin&amp;gt;&amp;gt;m;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout&amp;lt;&amp;lt;m;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;Here if a character is entered instead of a number, infinite loop
is caused. To avoid it, clear the error flag and ignore the offending
character. &lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;
&lt;br /&gt;
&amp;nbsp;while(m!=-1)&lt;br /&gt;
&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cin&amp;gt;&amp;gt;m;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(cin.fail())&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cin.clear();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
cin.ignore(INT_MAX,&#39;\n&#39;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; else{&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; cout&amp;lt;&amp;lt;m;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;}&lt;br /&gt;
&lt;/span&gt;&lt;big&gt;&lt;b&gt;NameSpaces&lt;/b&gt;&lt;/big&gt;&lt;br /&gt;
&lt;br /&gt;
C++ organizes names into different namespaces in order to avoid name
conflicts. All library functions and objects belong to the namespace
&quot;std&quot;. So to use cin and cout, we need to import this namespace. This
is done using&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;
using namespace std;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
Or you can use the fully qualified names viz &lt;b&gt;&lt;i&gt;std::cin&lt;/i&gt;&lt;/b&gt;, &lt;i&gt;&lt;b&gt;std::cout&lt;/b&gt;&lt;/i&gt;.&lt;i&gt;&lt;b&gt;
&lt;br /&gt;
::&lt;/b&gt;&lt;/i&gt; is called scope resolution operator. &lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;std::cout&amp;lt;&amp;lt;&quot;Hello
world&quot;;&lt;br /&gt;
&lt;/span&gt;&amp;nbsp;
&lt;br /&gt;
&lt;b&gt;&lt;big&gt;&lt;big&gt;bool&lt;/big&gt;&lt;/big&gt;&lt;br /&gt;
&lt;/b&gt;C++ has an additional data type bool. bool stores boolean values
true and false. But for i/o operations true is treated as 1 and false
is treated as 0.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;font-family: Courier New,Courier,monospace;&quot;&gt;bool m;
m=true;
cout&amp;lt;&amp;lt;m;//prints 1 here. 
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/6115470650458218038/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/input-output-operation-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6115470650458218038'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6115470650458218038'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/input-output-operation-in-c.html' title='Input Output operation in C++'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-4874167610487504162</id><published>2016-11-03T04:22:00.000-07:00</published><updated>2016-11-03T04:22:22.609-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="c operators"/><title type='text'>Know your operators</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;You know basic operators of any programming language. You know very well that x is not multiplication, but * is. You also know that % does not give percentage, instead gives reminder. You also know about &amp;amp;&amp;amp; || and !&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;But that is just basic stuff or as t&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;he clic&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;he goes ti&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;p of the iceberg&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. C has a very rich set of operators. And these allow you to write very small and cryptic programs.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;a++ or ++a?&amp;nbsp;&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;You know about increment operators - pre-increment and post increment. You can increment your number a using ++a or a++. Which is better and why?&lt;/span&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: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;The answer is - it &lt;b&gt;does not&lt;/b&gt; matter. Both increment the number - an integer. &lt;i&gt;Please remember that you can not increment a float or a double. But you can increment a char - &lt;/i&gt;because char is basically an integer.&amp;nbsp;&lt;/span&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: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;So you use a++; or ++a;&amp;nbsp;&lt;/span&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: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;But it matters when this i&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;increment&lt;/span&gt; operat&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ion &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;s used in an expression. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&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: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Let us look at an example.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; int m = 2, n=3;  
 m = 2 + ++n; &lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Here m will be 6 and n will be 4.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;But if we use pos&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;t increment operator&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; int m = 2, n=3;  
 m = 2 + &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;n++;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;
&amp;nbsp;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Here m will be 5 because 2+n is assigned to m&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt; and &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;af&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ter that&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt; n is incremente&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;d.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Same rules apply to &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;decrement&lt;/span&gt; operators --. &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;W&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;hat is sum+=n?&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;You use &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;this ty&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;pe of&lt;/span&gt; &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;expression qui&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;te often&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&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;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;sum = sum+n;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&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: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;So, C has a short hand operator for th&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;is called +=&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&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;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;sum+=n;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&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: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;is same as&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt; s&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;um = sum+n;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&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: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Y&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ou have &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;such o&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;perators for all arithme&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;tic and bitwise o&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;perators.&amp;nbsp; &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;You have &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;-=&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;, *=, /=, %=, &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;lt;&amp;lt;=,&amp;gt;&amp;gt;=, &amp;amp;=,&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;|=,^= &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;What is the relation?&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Of course&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;, you know about less tha&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;n, greater than, less than or &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;equal to&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;, greater tha&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;n or equal to&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt; (&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &amp;lt;, &amp;gt;, &amp;lt;=, &amp;gt;=). But confus&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ing operators for newbies are equal &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;to and not equal to.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;i&gt;Equal to &lt;/i&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;i&gt;operat&lt;/i&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;i&gt;or is not &lt;/i&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;i&gt;=&lt;/i&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;i&gt;.&lt;/i&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;= &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;is assign&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ment oper&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ator. E&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;qual to is ==. And not equal t&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;o is !=.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; int a = n%2;  
 if(a==0)  
   printf(&quot;%d is even&quot;,n);  
 else  
   printf(&quot;%d is odd&quot;,n);   
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Three is a crowd&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;C has only one ternary operator?: . Ter&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;nary operat&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;or is&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;an operator which takes 3 op&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;erands. This &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;operator &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;works like if&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt; - else s&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;tatement.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;  int m,n,p; m = 3;n=2;  
  p = m&amp;gt;n?m:n;  
&lt;/code&gt;&lt;/pre&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;The &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;second statement is &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;equ&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ivalent to &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; if(m&amp;gt;n)  
   p = m;  
 else  
   p = n;  
&lt;/code&gt;&lt;/pre&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;You can write &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;compl&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ete code to print even-ness of &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;a number &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;by tweaking this operator.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; int m;
 &lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;sca&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;nf(&quot;%d&quot;,&amp;amp;m);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;  (m%2)?&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;pri&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ntf(&quot;O&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;dd&quot;):printf(&quot;Even)&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Where &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;is the address?&lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;You can &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;get the address of a variable using &amp;amp; operator. Please note that, &amp;amp; is also us&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ed for bitwise&amp;nbsp; and &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;operation and &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;amp;&amp;amp;&amp;nbsp;&lt;/span&gt; is logical &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;and.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;int a =0;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;int *ptr &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;= &amp;amp;a&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;//&amp;amp; is address op&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;erator&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;amp;a gi&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ves &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&quot;address of&quot; &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;a.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt; ptr = &amp;amp;(a+10)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;produces error because a+10 is not lvalue and has no add&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ress.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&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: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Once we have address, we can &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;get value at that address usi&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ng * operator&amp;nbsp; &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;- called indirection operator&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; .&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; int b = *ptr;//indirection operator&lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&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: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;b &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;is assigned to &lt;/span&gt;the value at address &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;in &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ptr.&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&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: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Again the onus is on you to ensure t&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;hat ptr poi&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;nts to correct&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt; &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;location&lt;/span&gt;. If &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ptr is &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;uninitialized or has 0 &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;value or any other invalid value&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt; - &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;you &lt;/span&gt;get the infamous segmentation fault.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Just a dot&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Operator . &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;is used to access member of a struct&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ure or a union. -&amp;gt; &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;arrow operator is used to access member of a&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt; structure usi&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ng a pointer.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; struct student {int id,char name[10]};  
 struct student s;  
 s.id = 10;  
 strcpy(s.name,&quot;FooBar&quot;};&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; struct student *sptr = &amp;amp;s; sptr-&amp;gt;id=11; &lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;And t&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;he rest&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt; There &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;are bitwise operators &amp;amp;,|,!&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;, ^ and complement. And there are left and right shift operators. Read &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;about &lt;a href=&quot;http://cwithlinux.blogspot.in/2016/06/bitwise-operators.html&quot;&gt;bitwise operators here&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/4874167610487504162/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/know-your-operators.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/4874167610487504162'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/4874167610487504162'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/11/know-your-operators.html' title='Know your operators'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-5837512632281715290</id><published>2016-08-01T00:01:00.000-07:00</published><updated>2016-11-29T23:01:39.347-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="c++ constructor"/><title type='text'>Magic!!!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Google developers&#39; page asks you to enchant the users.&lt;br /&gt;
&lt;br /&gt;
So get ready for some magic.&lt;br /&gt;
&lt;br /&gt;
You know that anything created will be destroyed. And the corollary is, anything destroyed would have been created earlier. Law of conservation of mass - Is it?&lt;br /&gt;
&lt;br /&gt;
P.S. It is not the law of conservation of mass. The law says mass can not be created or destroyed. In my excitement to write a blog about magic, I overlooked it. &lt;br /&gt;
&lt;br /&gt;
So in C++, creation is assisted (or followed) by constructor and destruction by destructor. So if I show you a program in which there is a destructor without a constructor, we are defying the common principle.&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
Take down this code and execute it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; #include&amp;lt;iostream&amp;gt;  
 using namespace std;  
 class A  
 {  
   public:  
   A(){cout&amp;lt;&amp;lt;&quot;constructor &quot;;}  
   ~A(){cout&amp;lt;&amp;lt;&quot;destructor &quot;;}  
 };  
 void fn1(A obj)  
 {  
   cout&amp;lt;&amp;lt;&quot;hello magical world &quot;;  
 }  
 int main()  
 {  
    A obj1;  
    fn1(obj1);  
 }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
What did you say the output was. Is it&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;constructor hello magical world destructor&lt;/span&gt;&lt;br /&gt;
?&lt;br /&gt;
&lt;br /&gt;
No, It is not? Then is it&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;constructor hello magical world destructor destructor&lt;/span&gt;&lt;br /&gt;
?&lt;br /&gt;
&lt;br /&gt;
It is. Isn&#39;t it? So the question is, where is the extra destructor coming from?&amp;nbsp; One object - one constructor, one destructor. That is what is expected. But here we have one constructor and two destructors!&lt;br /&gt;
&lt;br /&gt;
So we have destroyed an object without constructing it? Is this the THE MAGICAL OBJECT?&lt;br /&gt;
&lt;br /&gt;
Tell me the reason.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/5837512632281715290/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/08/magic.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/5837512632281715290'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/5837512632281715290'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/08/magic.html' title='Magic!!!'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-308641806274075738</id><published>2016-07-07T00:31:00.001-07:00</published><updated>2016-07-07T00:31:30.479-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="c++ constructor"/><category scheme="http://www.blogger.com/atom/ns#" term="private destructor"/><title type='text'>Private constructor in C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;An interview question asked will be &quot;Can a constructor of a class be private&quot;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Yes, it can be. By making a constructor as private, the program will compile fine. But the question is can we create objects from such a class. Of course not. How can we when we can not access the constructor?&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;But it is possible. We can create an object by having a static member function which creates an object by calling private constructor and return this object.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background: #e9e9e9; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; #include&amp;lt;iostream&amp;gt;  
 using namespace std;  
 class A  
 {  
  int m; 
  A(){cout&amp;lt;&amp;lt;&quot;constructor&quot;;} //this is private constructor
  public:  
  static A createObject();  
  
 };  
 A A::createObject()  
 {  
  return A();  
 }  
 int main()  
 {  
    A obj1 = A::createObject();   
 }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;As we can see the function createObject() creates a temporary object using A() and returns this object.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Since the method is static, it can be called without using any object. It is called using class name and scope resolution operator.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Some time we not only want the constructor to be private, we want the class to let only one object created. Such a class is called singleton and has many applications. We will see how to create a singleton in a future post :)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/h4&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Private destructor&lt;/span&gt;&lt;/h4&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;This&amp;nbsp; is tricky. No matter how we create a constructor, destructor should be called and there is no way we can avoid this. Nor can we call a private a destructor.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;So even if we write a class with private destructor, we can NOT create an object from such a class. Because any object created must be destr&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;oyed which shou&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ld call a destructor&lt;/span&gt; which is not possible. &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Or should it be? What if we construct an object using dynamic memory allocation - using new. Then the object gets destroyed only if we call delete on that object.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;So we can have a private destructor for a class, and create an object from that class using new operator, if we do not call delete to destroy that object.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; #include&amp;lt;iostream&amp;gt;  
 using namespace std;  
 class A  
 {  
  int m;  
  ~A(){}  
  public:  
  A(){}  
 };  
 int main()  
 {  
   A *ptr = new A;  
   cout&amp;lt;&amp;lt;&quot;Hello world&quot;;  
 }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;As we see here, an object of A class is created using new A, but it is not destroyed using delete. The program compiles and runs fine.&amp;nbsp; Do not ask me what purpose does it serve. I don&#39;t know.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;But next time some one asks you, can constructor or destructor of a class be private, impress them with a detailed answer.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/308641806274075738/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/07/private-constructor-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/308641806274075738'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/308641806274075738'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/07/private-constructor-in-c.html' title='Private constructor in C++'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-5208655920213496931</id><published>2016-06-09T07:03:00.001-07:00</published><updated>2016-06-09T22:56:00.910-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="bitwise"/><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="c bitwise operators"/><title type='text'>Bitwise operators</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;C is a small language. It has just 32 keywords. But it has a rich set of operators.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;The operators which are commonly not used are bitwise operators. Here C comes closer to Assembly language because it lets you manipulate individual bits of a number.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Let us look at some examples.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;int a,b,c;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;a = 10;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;b = 12;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;c = a|b;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Here | is bitwise OR operator. Individual bits of a and b are ORed and result is stored in c.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;a = 00001010&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;b = 00001100&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;c = 00001110&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;So c will be 14&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;A small summary of bitwise operators here&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;style type=&quot;text/css&quot;&gt;.nobrtable br { display: none }&lt;/style&gt;

&lt;br /&gt;
&lt;div class=&quot;nobrtable&quot;&gt;
&lt;table border=&quot;2&quot; bordercolor=&quot;#0033FF&quot; cellpadding=&quot;6&quot; cellspacing=&quot;3&quot; style=&quot;width: 70%px;&quot;&gt;
   
&lt;tbody&gt;
&lt;tr style=&quot;background: #dddddd; border: 2px solid black;&quot;&gt;
    &lt;th&gt;Operator&lt;/th&gt;
    &lt;th&gt;Description&lt;/th&gt;
    &lt;th&gt;Example&lt;/th&gt;
  &lt;/tr&gt;
&lt;tr style=&quot;border: 2px solid black;&quot;&gt;
    &lt;td align=&quot;center&quot;&gt;&amp;amp;&lt;/td&gt;
    &lt;td&gt;AND - If both bits are 1, result is 1. If not result is 0&lt;/td&gt;
    &lt;td&gt;10&amp;amp;4 is 1010 | 0100 = 0000&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr border=&quot;2px&quot;&gt;
    &lt;td align=&quot;center&quot;&gt;|&lt;/td&gt;
    &lt;td&gt;OR-  If either bit is  1, result is 1. If not result is 0&lt;/td&gt;
    &lt;td&gt;10|4 is 1010 |0100 = 1110 = 14&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr style=&quot;border: 2px solid black;&quot;&gt;
    &lt;td align=&quot;center&quot;&gt;^&lt;/td&gt;
    &lt;td&gt;EXOR - If either bits are 1, but not both, result is 1. If not result is 0&lt;/td&gt;
    &lt;td&gt;10^4 is 1010 ^ 0100 = 1110  
        10^8 is 1010 ^1000 = 0010 = 2&lt;/td&gt;
  &lt;/tr&gt;
&lt;tr style=&quot;border: 2px solid black;&quot;&gt;
    &lt;td align=&quot;center&quot;&gt;&amp;lt;&amp;lt;&lt;/td&gt;
    &lt;td&gt;LEFT SHIFT - Shifts bits by n places to the left, adding 0 in empty bits&lt;/td&gt;
    &lt;td&gt;10&amp;lt;&lt;2 101000=&quot;40&amp;lt;/td&quot; 1010=&quot;&quot;&gt;
  &lt;/2&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;border: 2px solid black;&quot;&gt;
    &lt;td align=&quot;center&quot;&gt;&amp;gt;&amp;gt;&lt;/td&gt;
    &lt;td&gt;RIGHT SHIFT - Shifts bits by n places to the right adding 0 in empty bits &lt;/td&gt;
    &lt;td&gt;10&amp;lt;&lt;2 101000=&quot;40&quot; 1010=&quot;&quot; are=&quot;&quot; beyond=&quot;&quot; bits=&quot;&quot; lost=&quot;&quot; lsb=&quot;&quot; shifted=&quot;&quot; td=&quot;&quot;&gt;
  &lt;/2&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;border: 2px solid black;&quot;&gt;
    &lt;td align=&quot;center&quot;&gt;&amp;gt;&amp;gt;&lt;/td&gt;
    &lt;td&gt;COMPLEMENT Complements all the bits. i.e. 1 to 0 and 0 to 1&lt;/td&gt;
    &lt;td&gt;~10 is 11110101 for unsigned char (1 byte)     
&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Shift Operators&lt;/h3&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Left shift - &amp;lt;&amp;lt;, shifts all bits of a number to left by given number of bits. The empty bits &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;in LSB&lt;/span&gt; are filled with 0.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;e.g.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;a = 10;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;b = a&amp;lt;&amp;lt;2;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;a = 00001010;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;b = 00101000;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;So b will be 40.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&amp;gt;&amp;gt; shifts all bits of the number to right by given number of bits. The bits pushed beyond LSB are lost.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;a = 10;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;b = a&amp;gt;&amp;gt;2;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;000010101 &amp;gt;&amp;gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt; 2 = 00000010&amp;nbsp;&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;b becomes 2.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;As you might have guessed, left shift operator &lt;i&gt;multiplies &lt;/i&gt;the number by 2 to the power of n&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;R&lt;/span&gt;ight shift &lt;i&gt;divides &lt;/i&gt;the number by 2 to the power of n.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;b&gt;|=, &amp;amp;= &lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;and o&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;ther operators&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;And arithmetic assignment operator, there are bitwise assignment operators.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;|=&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt; &amp;amp;= &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; ^= &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;lt;&amp;lt;=&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;gt;&amp;gt;=&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;a |=b ;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;is equivalent to a = a|b;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Now time for some small program.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; #include&amp;lt;stdio.h&amp;gt;  
 int main()  
 {  
   int a =10;  
   int b = 1;  
   int c;  
   c = a|b;  
   printf(&quot;OR %d\n&quot;,c);  
   c = a&amp;amp;b;  
   printf(&quot;AND %d\n&quot;,c);  
   c = a^b;  
   printf(&quot;EXOR %d\n&quot;,c);  
   c = a&amp;lt;&amp;lt;b;  
   printf(&quot;Left shift %d\n&quot;,c);  
   a &amp;gt;&amp;gt;=b;  
   printf(&quot;right shift= %d\n&quot;,a);  
   c = ~b;  
   printf(&quot;complement of b%d\n&quot;,c);   
 }  
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/5208655920213496931/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/06/bitwise-operators.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/5208655920213496931'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/5208655920213496931'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/06/bitwise-operators.html' title='Bitwise operators'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-6699600620767603889</id><published>2016-05-30T22:34:00.002-07:00</published><updated>2016-05-30T22:34:45.372-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><title type='text'>int main() or void main()</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Many people still use void main() in their programs. Their justification is why should main return anything? and to where does it return a values? And my compiler accepts main() without return type.&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;main() should return a 0 value to indicate successful execution of the program.&lt;/li&gt;
&lt;li&gt;main() returns the value to operating system.&lt;/li&gt;
&lt;li&gt;If your program writes&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; main()&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /********/}&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; it is int main().&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Default return type in C is int, not void.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;All modern compilers give a warning when you use void main(). In fact in C++, void main() will give compiler error&lt;/li&gt;
&lt;/ol&gt;
Two signatures of main function are&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;int main();&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;and&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;int main(int argc, char *argv[])&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
First one is used in normal programs. Second signature is used when you want to access and process command line arguments.&lt;br /&gt;
&lt;br /&gt;
So do not use void main() in your code, even if your compiler does not throw any errors. &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/6699600620767603889/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/05/int-main-or-void-main.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6699600620767603889'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6699600620767603889'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/05/int-main-or-void-main.html' title='int main() or void main()'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-170327830462977522</id><published>2016-05-12T04:38:00.003-07:00</published><updated>2016-11-29T23:01:17.968-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><title type='text'>Online learning - C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Today&#39;s generation has no patience. No patience to read books (or attend classes :) ). They learn stuff through youtube. How effective is that? And how reliable are the videos?&lt;br /&gt;
&lt;br /&gt;
But there are online courses available from various sites like Coursera, Edx, Udemy etc. These are reasonably better, I think.&lt;br /&gt;
&lt;br /&gt;
When I was browsing through online courses for C++, I came across &lt;a href=&quot;http://www.sololearn.com/Play/CPlusPlus/&quot;&gt;sololearn &lt;/a&gt;and visited their C++ course. I was impressed. The course was concise but really good. And the best part is each descriptive page is followed by a quiz. What is needed for us - who have the attention span of a two year old :)&lt;br /&gt;
&lt;br /&gt;
Should I give a disclaimer here? I am not a paid advertiser for that course. The reason behind this post is - I am eager to share the information with others as always.&lt;br /&gt;
&lt;br /&gt;
And by the way, sololearn has android apps for courses, which are good too.&lt;br /&gt;
&lt;br /&gt;
By the way MIT has open course ware for C++&amp;nbsp; complete with downloadable lecture notes and assignments &lt;a href=&quot;http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-096-introduction-to-c-january-iap-2011/lecture-notes/&quot;&gt;here&amp;nbsp; &lt;/a&gt;and &lt;a href=&quot;http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-088-introduction-to-c-memory-management-and-c-object-oriented-programming-january-iap-2010/index.htm&quot;&gt;here&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
And there is a course in &lt;a href=&quot;http://www-h.eng.cam.ac.uk/help/tpl/languages/C%2B%2B.html&quot;&gt;university of cambridge&lt;/a&gt; here.&lt;br /&gt;
&lt;br /&gt;
Before I forget, &lt;a href=&quot;http://www.informit.com/guides/guide.aspx?g=cplusplus&quot;&gt;informit reference guide on C++&lt;/a&gt; has in depth material and is really good (but is not downloadable) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/170327830462977522/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/05/online-learning.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/170327830462977522'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/170327830462977522'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/05/online-learning.html' title='Online learning - C++'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-3630379309013650887</id><published>2016-05-09T18:07:00.001-07:00</published><updated>2016-05-09T18:07:27.825-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="function call overloding"/><category scheme="http://www.blogger.com/atom/ns#" term="functor"/><category scheme="http://www.blogger.com/atom/ns#" term="subscript operator overloading"/><title type='text'>Overloading of [] and ()</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;Let us see some unusual operators to overload.&lt;/span&gt;&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Subscript operator overloading &lt;/h3&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&amp;nbsp;Let us say you have an array class of some type and you want to access the elements of the array using familiar [] operator, then you can overload this.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;So here comes code&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;  class Array  
 {  
   int *numarray;  
   int size;  
 public:  
   --------  
   int operator [](int index);  
   -----  
 };  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;The class looks quite straight forward. It is array of ints and subscript operator returns the element of the array. So the function can be&lt;/span&gt;&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; int Array::operator[](int index)&lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;
  { &lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;  
   return numarray[index];&lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;  
  }&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;But there is still some problem in this code. Don&#39;t you think so? Let us look at its usage&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;Array obj(5);
cout&amp;lt;&amp;lt;obj[0];
obj[1] = 22;&lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; &lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;  &lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;obj[1] = 22 needs us to be able to place our operator function on LHS. Which means --- we need a reference return type.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;So the proper class with operator functions is&amp;nbsp; &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;#include&amp;lt;iostream&amp;gt;
using std::cout; 
class Array
{ int *element;
 int size; 
 public:
    Array(int n=1);
    int &amp;amp; operator [](int index);
   const int &amp;amp;operator[](int index) const;
    ~Array();
    Array(const Array &amp;amp; obj);
    Array&amp;amp; operator =(const Array &amp;amp;other);
}; 
Array::Array(int n)
{
 size = n; 
 element = new int[n];
 for(int i=0;i&amp;lt;n;i++)
 {
  element[i]=0;
 }
} 
Array::Array(const Array &amp;amp;obj)
{  
 size = obj.size;
 element = new int[size];
 for(int i=0;i&amp;lt;size;i++)
 {
  element[i]=obj.element[i];
 }
} 
Array&amp;amp; Array::operator = (const Array &amp;amp;other)
{
 if(this!=&amp;amp;other){
  delete []element;
   
   size = other.size;
 element = new int[size];
 for(int i=0;i&amp;lt;size;i++)
 {
  element[i]=other.element[i];
 }
 }
 return *this;
}

int &amp;amp;Array ::operator[](int n)
{
 if(n&amp;gt;=0 &amp;amp;&amp;amp; n&amp;lt;size)
     return element[n];
  else return 0;
} 
 
const int&amp;amp; Array::operator[](int n) const
{
 if(n&amp;gt;=0 &amp;amp;&amp;amp; n&amp;lt;size)
     return element[n];
}
 
Array::~Array()
{
 delete[]element;
}&lt;/code&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Now you should ask me why are there two subscript functions. The reason is, if there is a constant object, it can call only const functions. &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const &lt;/span&gt;functions should not modify the state of the object. That is the reason one [] operator function is &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const &lt;/span&gt;and returns &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;const int &amp;amp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Function call operator&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Function call operator&amp;nbsp; should be a member function. It is normally used for function objects such as iterators etc.&lt;/span&gt;&lt;/span&gt;&lt;/span&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: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Let us look at an example.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;class Cube
{
 public:
 int operator ()(int num);
};
int Cube::operator()(int num)
{
 return num*num*num;
}&lt;/code&gt;&lt;/pre&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: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;As we can see the class has an operator function which takes an int and returns its cube. Let us see its usage.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;        Cube funobj;
 cout&amp;lt;&amp;lt;funobj(4);&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana,sans-serif;&quot;&gt;In this case funobj is function object and is also called a &lt;b&gt;functor&lt;/b&gt;.
&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/3630379309013650887/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/05/overloading-of-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/3630379309013650887'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/3630379309013650887'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/05/overloading-of-and.html' title='Overloading of [] and ()'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-690745543175240130</id><published>2016-05-07T18:37:00.001-07:00</published><updated>2017-05-26T09:59:06.520-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="File handling"/><title type='text'>File Handling in C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
File handling in C++ is not that difficult. All these times, you have been using insertion operator - &amp;lt;&amp;lt; and extraction operator &amp;gt;&amp;gt; to write and read data to screen. Insertion operator uses cout object - which is an object of ostream class and extraction operator works on cin - an istream object.&lt;br /&gt;
&lt;br /&gt;
ifstream is a derived class of istream&amp;nbsp; and can be used for files in read mode and ofstream is derived from ostream and is used for writing to files.&lt;br /&gt;
&lt;br /&gt;
This is how you start writing to a&amp;nbsp; file&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt;ofstream obj(&quot;myfile.txt&quot;);//constructor creates the file and opens it. 
int n;&amp;lt; &lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&amp;nbsp;If the file can not be created or opened the object will be false.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt;if(!obj)&lt;/code&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt; cout&amp;lt;&amp;lt;&quot;File open error&quot; &lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Another method of opening a file is a 2 step process&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt;ifstream obj2;   obj2.open(&quot;filename&quot;); &lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Both the constructor and open method can take second parameter which is the mode of opening the file.&lt;br /&gt;
&lt;br /&gt;
The mode of opening a file can be&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;ios::in , ios::out, ios::app, ios::trunc&lt;/span&gt; etc.&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Writing to file:&lt;/h3&gt;
Let us write hello world to a file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt; #include&amp;lt;iostream&amp;gt;  
 #include&amp;lt;fstream&amp;gt;  
 using namespace std;  
 int main()  
 {  
      ofstream obj(&quot;myfile.txt&quot;);  
      if(!obj)  
      {  
        cout&amp;lt;&amp;lt;&quot;could not open file&quot;;  
           exit(1);  
      }  
      obj&amp;lt;&amp;lt;&quot;Hello world&quot;&amp;lt;&amp;lt;endl;  
      obj.close();  
 }  
&lt;/code&gt;&lt;/pre&gt;
&amp;nbsp;Voila! We wrote our first hello world file.&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Reading from a file:&lt;/h3&gt;
To read the same, we need to use an ifstream object and&amp;nbsp; extraction operator&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt; #include&amp;lt;iostream&amp;gt;  
 #include&amp;lt;fstream&amp;gt;  
 using namespace std;  
 int main()  
 {  
      ifstream obj(&quot;myfile.txt&quot;);  
      if(!obj)  
      {  
        cout&amp;lt;&amp;lt;&quot;could not open file&quot;;  
           exit(1);  
      }  
      char s[100]; 
      while(obj) 
      {  
          obj&amp;gt;&amp;gt;s;  
          cout&amp;lt;&amp;lt;s;
      }
 } &lt;/code&gt;&lt;/pre&gt;
Do you notice anything stranage? There are no format specifiers anywhere and no addressof operator. That is because &amp;gt;&amp;gt; and &amp;lt;&amp;lt; are overloaded for all types of data. And C++ uses reference parameters, so no addresses.&lt;br /&gt;
&lt;br /&gt;
Also there are no white spaces. &amp;gt;&amp;gt; operator ignores whitespaces.&lt;br /&gt;
&lt;br /&gt;
Even if you read the string from screen, the similar thing can be expected. To read a string with white spaces and everything, you can use getline function&lt;br /&gt;
&lt;br /&gt;
obj.getline(str,100);&lt;br /&gt;
&lt;br /&gt;
where str is character array - buffer and 100 is size of buffer.&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt;&amp;nbsp;     char str[100];
      while(obj)
      {
           obj&amp;gt;&amp;gt;str;
           cout&amp;lt;&amp;lt;str;
     }&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
There is a safer version without mentioning the buffer length. This is non-member function.&lt;br /&gt;
&lt;br /&gt;
getline(istream-object,char-array,delimiter)&lt;br /&gt;
which reads upto the delimiter character.&lt;br /&gt;
&lt;br /&gt;
You can use this as&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt;&lt;/code&gt;getline(obj,str,&#39;\n&#39;);&lt;/pre&gt;
&lt;br /&gt;
What if you meed to read and write from the same file? That is possible too, using the mode ios::in|ios::out and using an fstream object, like this.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt; int main()  
 {  
      fstream obj(&quot;myfile.txt&quot;,ios::in|ios::out);  
      if(!obj)  
      {  
        cout&amp;lt;&amp;lt;&quot;could not open file&quot;;  
           exit(1);  }
     &lt;/code&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt; char s[100];
      while(obj)
      {
           obj&amp;gt;&amp;gt;s;
           cout&amp;lt;&amp;lt;s;
     } 
     obj.clear();//clear the errors on object
     obj&amp;lt;&amp;lt;&quot;Now we write again to same file&quot;;
     obj.close();&lt;/code&gt;}&lt;/code&gt;&lt;/pre&gt;
Do you notice some thing here? Instead of checking for NULL, we just check if the stream is true or false.&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
bad? fail?&lt;/h3&gt;
Another method of checking the validity of stream is using bad(), eof() and fail() functions. bad() returns true if any invalid operation is performed on the stream. eof() returns true when end of file is reached. And fail() is true if either bad() or eof() are true.&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Seek and you will find it&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Remember fseek() and ftell() in C? We have equivalents here. Only we have four functions. seekg(), seekp(), tellg(), tellp(). And you are right. g is for get pointer - for reading and p is put pointer - for writing.&amp;nbsp;&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;
seekg() and seekp() move the file pointer to the required location. They need offset as first parameter and optional seek direction as second parameter.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt;     obj.clear();//clear the errors on object
     obj.seekp(0); 
     obj&amp;lt;&amp;lt;&quot;Now we write again to same file&quot;;
     obj.close();&lt;/code&gt;&lt;/pre&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
This code writes the line to the beginning of the file.&amp;nbsp;&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;
tellg() and tellp() give the location of getpointer and putpointer in terms of bytes.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Objects to a file&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
So far we wrote and read PODs . But we can write entire objects to the file by overloading &amp;lt;&amp;lt; and extraction operators for these classes as shown below.&lt;/div&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;bgcolor: #111166 color: black; word-wrap: normal;&quot;&gt;   class NumString
{
 int num;
 char str[40];
public:
 NumString(int n=0):num(n),str(&quot;nothing&quot;){}
 int getNum()
 {
  return num;
 }
 void setNum(int n)
 {
  num = n;
 }
 void setstr(char *s)
 {
  strcpy(str,s);
 }
 char * getstr()
 {
  return str;
 }
};

ostream&amp;amp; operator &amp;lt;&amp;lt; (ostream &amp;amp;out,NumString &amp;amp;obj)
{
 out&amp;lt;&amp;lt;obj.getNum()&amp;lt;&amp;lt;&quot; &quot;&amp;lt;&amp;lt;obj.getstr()&amp;lt;&amp;lt;&quot;\n&quot;;
 return out;
}
 int main()  
 {  
      fstream obj(&quot;myfile.txt&quot;,ios::out);
      NumString nobj(100);
   nobj.setstr(&quot;Some string&quot;);
   obj&amp;lt;&amp;lt;nobj;
      return 0;
 }&lt;/code&gt;&lt;/pre&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;
And now we wrote our object to the file. You want to read objects from the file? Overload extraction operator!&lt;br /&gt;
&lt;br /&gt;
To learn basic and advanced topics in C++ and solve quiz and write programs, you may want to download my app &lt;a href=&quot;https://play.google.com/store/apps/details?id=com.hegdeapps.cppquestions&quot;&gt;c++ notes &lt;/a&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;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/690745543175240130/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/05/file-handling-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/690745543175240130'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/690745543175240130'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/05/file-handling-in-c.html' title='File Handling in C++'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-6119258491126329941</id><published>2016-04-27T23:58:00.001-07:00</published><updated>2016-04-27T23:59:09.272-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="Intro to Exceptions in C++"/><title type='text'>Exceptional situation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
As part of my C++ journey (not for the first time), I embarked upon &quot;exception handling&quot;. After reading the first few paragraphs,&amp;nbsp; I wanted to get my hands dirty by a code which catches an exception and shots howzzat.&lt;br /&gt;
&lt;br /&gt;
Which exception is easily conceivable that divide by zero? So I cinned two numbers, divided them and waited with bated breath.&amp;nbsp; And was sadly disappointed. Instead of catching the error, my program crashed. I tried &quot;exception&quot;, &quot;Exception&quot;, &quot;std::exception&quot;.&amp;nbsp; All IDEs are not android studios to give the correct version and its needed header file. Incidentally I am using Codelite with mingw compiler it suggested - not bad for a windows gcc IDE.&lt;br /&gt;
&lt;br /&gt;
So I had an idea - use catch(...) - which can not fail. It is supposed to catch all exceptions. I thought stupid Windows system is unable to handle exceptions. So rewrote the code in ubuntu. With exactly same results.&lt;br /&gt;
&lt;br /&gt;
So there must be some thing wrong with the code? What can be wrong with 3 line code - try - divide n1 by n2 followed by catch?&lt;br /&gt;
&lt;br /&gt;
Again my memory said I had been here before. So let me just throw an int error&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;try&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;{
     if (n2==0)&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;         &amp;nbsp;throw 5;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;return n1/n2;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;}&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;catch (int e)&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;{&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&amp;nbsp;cout&amp;lt;&amp;lt;&quot;Exception&quot;;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;}&lt;/code&gt;&lt;/pre&gt;
&amp;nbsp;And voila, the program worked. So the problem is not with exception handling. It is with floating point exception or divide by zero error.&lt;br /&gt;
&lt;br /&gt;
Little googling told me that Stroustrup wanted divide by zero to be handled by low level code like Assembly language, not be exception handling.&lt;br /&gt;
&lt;br /&gt;
Why don&#39;t any books mention that? Don&#39;t they know that we code - enthusiasts first try with division by zero error.&lt;br /&gt;
&lt;br /&gt;
So now I am telling you. Don&#39;t expect, an exception to be thrown or caught when you divide by zero.&lt;br /&gt;
&lt;br /&gt;
Another thing. Instead of throwing int, if you want to throw an exception object, the method is to say &lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;throw exception();&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;     try{  
           if(n2==0) throw exception();  
           return n1/n2;  
      }catch( exception e)  
      {  
           cout&amp;lt;&amp;lt;&quot;there was an error in divide&quot;;  
           cout&amp;lt;&amp;lt;e.what();  
      }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
In a few days, I will add another post - this time a serious one, about exception handling.
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/6119258491126329941/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/04/exceptional-situation.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6119258491126329941'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/6119258491126329941'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/04/exceptional-situation.html' title='Exceptional situation'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-9004949130113338624</id><published>2016-04-27T23:23:00.000-07:00</published><updated>2016-04-27T23:23:05.262-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="templates"/><category scheme="http://www.blogger.com/atom/ns#" term="templates in C++"/><title type='text'>Template Specialization</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Sometimes template classes are not just right for a particular data type. In order to redefine such specialized template classes you need to use template with empty angular brackets followed by class name and type name in angle brackets.&lt;br /&gt;
&lt;br /&gt;
Like this&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; template &amp;lt;class T&amp;gt;  
 class Number  
 {  
      T n;  
 public:  
   Number(T n1):n(n1){}  
      T getNumber();  
      void setNumber(T n);  
 };  
 template &amp;lt;class T&amp;gt;  
 T Number&amp;lt;T&amp;gt;::getNumber()  
 {  
      return n;  
 }  
 template&amp;lt;class T&amp;gt;  
 void Number&amp;lt;T&amp;gt;::setNumber(T n1)  
 {  
      n = n1;  
 }  
 template&amp;lt;&amp;gt;  
 class Number&amp;lt;char&amp;gt;  
 {  
      int n;  
      public:  
      Number(char n1);  
      void setNumber(char n1);  
      char getNumber();  
 };  
 &lt;b&gt;Number&amp;lt;char&amp;gt;::Number(char n1)  &lt;/b&gt;
      {  
           n = (int)n1;  
      }  
   &lt;b&gt;void Number&amp;lt;char&amp;gt;::setNumber(char n1)   &lt;/b&gt;     
      {  
           n = (int)n1;  
      }  
   &lt;b&gt;char Number&amp;lt;char&amp;gt;::getNumber()   &lt;/b&gt;     
      {  
           return (char)n;  
      }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
For the Number template class, specialization is done for char type using&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; template&amp;lt;&amp;gt;  
 class Number&amp;lt;char&amp;gt;  
 {&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;----------- &lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;}; &lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Do you notice something here? When defining&amp;nbsp; member functions of specialized template class- Number&amp;lt;char&amp;gt; outside the body of class, template keyword is not used. Instead, just class name with data type in angle brackets is used.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; &lt;b&gt;Number&amp;lt;char&amp;gt;::Number(char n1)  &lt;/b&gt;
      {  
           n = (int)n1;  
      }  &lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
When instantiated with int, float etc, generic Number class is used. When instantiated with char, specialized class is used.&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;         Number&amp;lt;int&amp;gt; nobj(10);//uses generic Number
  cout&amp;lt;&amp;lt;&quot;int number is &quot;&amp;lt;&amp;lt;nobj.getNumber()&amp;lt;&amp;lt;&quot;\n&quot;;   
  Number&amp;lt;char&amp;gt; nobjchar(&#39;A&#39;);//uses Number&amp;lt;char&amp;gt; class
  cout&amp;lt;&amp;lt;&quot;char number is &quot;&amp;lt;&amp;lt;nobjchar.getNumber()&amp;lt;&amp;lt;&quot;\n&quot;; &lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Template specialization for function templates&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
We can write a function with same name as template function. Then this explicit function overrides template function for that data-type.&amp;nbsp;&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;
In this example, print function is defined explicitly for string type.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;template &amp;lt;class T&amp;gt;
void print(T val){
 cout&amp;lt;&amp;lt;&quot;Value is &quot;&amp;lt;&amp;lt;val&amp;lt;&amp;lt;endl;
}
void print(std::string s)
{
 cout&amp;lt;&amp;lt;&quot;String is&quot;&amp;lt;&amp;lt;s;
}
int main(int argc, char **argv)
{
   print(10.2);//uses template function
   string s = &quot;Hello C++ developer&quot;;
   print(s);//uses non-template print function
}&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
It is also possible to overload a template function with another template function.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;template &amp;lt;class T&amp;gt;
void print(T val){
 cout&amp;lt;&amp;lt;&quot;Value is &quot;&amp;lt;&amp;lt;val&amp;lt;&amp;lt;endl;
}&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;template &amp;lt;class T,class U&amp;gt;&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;void print(T val1,U val2)&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;{&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;    cout&amp;lt;&amp;lt;&quot;First value is &quot;&amp;lt;&amp;lt;val1&amp;lt;&amp;lt;&quot;Second value is &quot;&amp;lt;&amp;lt;val2;&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;}&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;int main()&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;{&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;     int a,b;&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;     a=b=12;&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;     float c = 88.8;&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;     print(a);//first template function&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;     print(a,c);//second template function&lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre style=&quot;background: #f0f0f0; border: 1px dashed #cccccc; color: black; font-family: &amp;quot;arial&amp;quot;; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;&lt;b&gt;} &lt;/b&gt;&lt;/code&gt;&lt;/pre&gt;
&amp;nbsp;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/9004949130113338624/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/04/template-specialization.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/9004949130113338624'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/9004949130113338624'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/04/template-specialization.html' title='Template Specialization'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6188731032231049428.post-3512157863201551173</id><published>2016-03-28T06:39:00.000-07:00</published><updated>2016-03-28T06:39:05.871-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="c++ questions"/><title type='text'>A sample C++ question for you to ponder</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
I am collecting C++ questions. And I really don&#39;t want to ask the much used what is inheritance? what is polymorphism?&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
And I don&#39;t want to ctrl C and ctrl V either. So I am making up my own questions. I can see a smirk on your faces.&lt;br /&gt;
&lt;br /&gt;
OK, OK. Let us see if you can predict the output of the program. Do predict without compiling and running the program please.&lt;br /&gt;
&lt;br /&gt;
#include&amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp; int n;&lt;br /&gt;
public:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; A(int);&lt;br /&gt;
&amp;nbsp;&amp;nbsp; ~A();&lt;br /&gt;
&amp;nbsp;&amp;nbsp; void print();&lt;br /&gt;
&amp;nbsp;} &lt;br /&gt;
&amp;nbsp; &amp;nbsp; A::A(int m):n(m)&lt;br /&gt;
&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; cout&amp;lt;&amp;lt;&quot;Constructor&quot;;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp; A::~A()&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; cout&amp;lt;&amp;lt;&quot;Destructor&quot;;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
void A::print()&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout&amp;lt;&amp;lt;m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A obj1(10);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; obj1.print();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A obj2[3];&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; obj2[0].print();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Again, I see the smirk on your face. If you say the output is&lt;br /&gt;
ConstructorConstructorConstructorConstructorDestructorDestructorDestructorDestructor&lt;br /&gt;
&lt;br /&gt;
If you say that, now you can compile and run the program.&lt;br /&gt;
&lt;br /&gt;
:) &lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://cwithlinux.blogspot.com/feeds/3512157863201551173/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cwithlinux.blogspot.com/2016/03/a-sample-c-question-for-you-to-ponder.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/3512157863201551173'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6188731032231049428/posts/default/3512157863201551173'/><link rel='alternate' type='text/html' href='http://cwithlinux.blogspot.com/2016/03/a-sample-c-question-for-you-to-ponder.html' title='A sample C++ question for you to ponder'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13247878485630067800</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>