<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;DkADRXk8eip7ImA9WhRaFEw.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160</id><updated>2012-02-16T09:52:54.772-08:00</updated><title>Learn C++ Programming within 2 weeks</title><subtitle type="html">learn cpp,c++,know about cpp,c++,advanced c programming,learn c++,cpp,c++,enjoy c++,cpp,c++ programming,c++ tutorial,cpp Tutorial</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://knowaboutcpp.blogspot.com/" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>18</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/LearnCProgrammingWithin2Weeks" /><feedburner:info uri="learncprogrammingwithin2weeks" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;C0EAQH0_eip7ImA9WxNWFEg.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-4892880213668994988</id><published>2009-10-13T09:33:00.002-07:00</published><updated>2009-10-13T09:34:01.342-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-13T09:34:01.342-07:00</app:edited><title>RRS - Module 6</title><content type="html">&lt;p&gt;Integrate all the modules properly so that the Railway Reservation System works efficiently. Run and test all the modules properly. Take care of exceptions if any in the system. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-4892880213668994988?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/kYl8uo4qU6lsiF113DGAj6dNqaM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/kYl8uo4qU6lsiF113DGAj6dNqaM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/kYl8uo4qU6lsiF113DGAj6dNqaM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/kYl8uo4qU6lsiF113DGAj6dNqaM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/xK14KDRi6nQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/4892880213668994988/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/rrs-module-6.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/4892880213668994988?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/4892880213668994988?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/xK14KDRi6nQ/rrs-module-6.html" title="RRS - Module 6" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/rrs-module-6.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0EFQ387fSp7ImA9WxNWFEg.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-425912679726706828</id><published>2009-10-13T09:33:00.001-07:00</published><updated>2009-10-13T09:33:32.105-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-13T09:33:32.105-07:00</app:edited><title>RRS - Module 5</title><content type="html">&lt;p&gt;Before we start with this module, verify if you have completed with the previous module. Only after you complete with it, should you start with this module. &lt;/p&gt;&lt;p&gt;In this module we would enable the user to cancel his reserved tickets. &lt;/p&gt;&lt;p&gt;A user can cancel reserved ticket and the fare is refunded according to the rules given. &lt;/p&gt;&lt;p&gt;&lt;b&gt;Cancellation Rules:&lt;/b&gt;&lt;br /&gt;TICKETS on which Reservations has been made.&lt;br /&gt;&lt;/p&gt; &lt;ol&gt;&lt;li&gt; If a ticket on which reservation of a seat or berth has been made, is presented for cancellation, refund of fare shall be made after deducting cancellation charge from the fare as follows :- &lt;ol&gt;&lt;li&gt; If a ticket is presented for cancellation more than 12 hours before the scheduled departure of the train cancellation charge shall be deducted at the flat rate of Rs. 50/- for A.C Class and Rs. 20/- for Sleeper class. &lt;/li&gt;&lt;li&gt; If the ticket is presented for cancellation within 12 hours and up to 3 hours before the scheduled departure of the train, cancellation charges shall be 10% of the fare paid. (Rounded off to nearest rupee). &lt;/li&gt;&lt;li&gt; If the ticket is presented for cancellation within 3 hours and up to 30 Minutes before the scheduled departure of the train, Cancellation charges shall be 60% of the fare paid. (Rounded off to nearest rupee). &lt;/li&gt;&lt;li&gt; If the ticket is presented for cancellation after the scheduled departure of the train, refund money is Rs 0. &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Steps to be followed for Cancellation part in this module are:&lt;br /&gt;&lt;/p&gt; &lt;ol&gt;&lt;li&gt; Login as User &lt;/li&gt;&lt;li&gt; Allow the user to cancel his/her ticket. (Note: The user can cancel a single ticket even when he has booked 2 tickets which have a single pnr_no) &lt;/li&gt;&lt;li&gt; The amount to be refunded should be mentioned to the user. &lt;/li&gt;&lt;li&gt; The total amount transacted on each day should also be seen by the Admin. This part should be done in the Admin Interface. &lt;/li&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-425912679726706828?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/DQrDvBF8YroIM8Bh9iR48Ww-FWA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/DQrDvBF8YroIM8Bh9iR48Ww-FWA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/DQrDvBF8YroIM8Bh9iR48Ww-FWA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/DQrDvBF8YroIM8Bh9iR48Ww-FWA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/-XTnxtsBmvQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/425912679726706828/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/rrs-module-5.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/425912679726706828?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/425912679726706828?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/-XTnxtsBmvQ/rrs-module-5.html" title="RRS - Module 5" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/rrs-module-5.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0IMRnkycCp7ImA9WxNWFEg.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-7464143147440128539</id><published>2009-10-13T09:31:00.000-07:00</published><updated>2009-10-13T09:33:07.798-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-13T09:33:07.798-07:00</app:edited><title>RRS - Module 4</title><content type="html">&lt;p&gt;In this module let us create Ticket objects and allow the user to book his tickets &lt;/p&gt;&lt;p&gt;Users cannot book tickets for unscheduled trains. The user needs to check the schedule and the availability of tickets for a train and then only book tickets. &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W2/M6/module4.JPG" alt="module4.JPG" /&gt; &lt;/p&gt;&lt;p&gt;Steps to be followed to create Ticket objects:&lt;br /&gt;&lt;/p&gt; &lt;ol&gt;&lt;li&gt; Create a base class called Ticket, by identifying the common attributes of a Ticket&lt;br /&gt;&lt;/li&gt;&lt;li&gt; Create 2 derived classes called, AC_Ticket and SC_Ticket, for the above given base class (AC_Ticket is for AC class and SC_Ticket is for sleeper class)&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Steps to be followed for Reservation part in this module are:&lt;br /&gt;&lt;/p&gt; &lt;ol&gt;&lt;li&gt; Login as User&lt;br /&gt;&lt;/li&gt;&lt;li&gt; Check Schedule of the trains available&lt;br /&gt;&lt;/li&gt;&lt;li&gt; Check the availability of berths&lt;br /&gt;&lt;/li&gt;&lt;li&gt; If available berths are there, allow the user to book tickets – AC or Sleeper&lt;br /&gt;&lt;/li&gt;&lt;li&gt; A User can book maximum of 2 tickets at a time. There is a single pnr number given.&lt;br /&gt;&lt;/li&gt;&lt;li&gt; Save the tickets in the folder that contains that particular train and the required day.&lt;br /&gt;&lt;/li&gt;&lt;li&gt; Display the ticket to the user in a neat format &lt;/li&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-7464143147440128539?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/_-Xp3DxFOteGiVMurHDCGvjJcZc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/_-Xp3DxFOteGiVMurHDCGvjJcZc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/_-Xp3DxFOteGiVMurHDCGvjJcZc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/_-Xp3DxFOteGiVMurHDCGvjJcZc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/gwR6-Buy2z0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/7464143147440128539/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/rrs-module-4.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/7464143147440128539?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/7464143147440128539?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/gwR6-Buy2z0/rrs-module-4.html" title="RRS - Module 4" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/rrs-module-4.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0IMQH4_fip7ImA9WxNWFEg.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-4268791625302135125</id><published>2009-10-13T09:29:00.001-07:00</published><updated>2009-10-13T09:33:01.046-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-13T09:33:01.046-07:00</app:edited><title>RRS - Module 3</title><content type="html">&lt;p&gt;In this module, let us create Train objects and allow Admin to schedule the trains for a week. &lt;/p&gt;&lt;p&gt;Steps to be followed in this module: &lt;/p&gt; &lt;ol&gt;&lt;li&gt;Create a class called Train by identifying its member variables &lt;/li&gt;&lt;li&gt;Write the code for the Interface Menu.  &lt;ol&gt;&lt;li&gt; Login as Admin and do the following &lt;ol&gt;&lt;li&gt; Add new trains &lt;/li&gt;&lt;li&gt; Save the objects in file &lt;/li&gt;&lt;li&gt; Schedule the trains for that week. (Note: Check the assumptions we made at the beginning of this project regarding scheduling) &lt;/li&gt;&lt;li&gt; While scheduling, the folder structure to be created is &lt;ol&gt;&lt;li&gt; RRS\TrainData\TrainNo  &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;li&gt; The entire schedule could also be written into a file &lt;/li&gt;&lt;li&gt; Display the schedule &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;li&gt; Logout from Admin Interface and log into the User Interface &lt;ol&gt;&lt;li&gt; Display the schedule based on train &lt;/li&gt;&lt;li&gt; Display the schedule based on Source and Destination &lt;/li&gt;&lt;li&gt; Display the schedule based on Date &lt;/li&gt;&lt;li&gt; Display all the trains scheduled  &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Note: Make sure that all the display functions print the details in a neat tabular format &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-4268791625302135125?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/vRq2VeyVuil7iiBWDRSPLAuj4qw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vRq2VeyVuil7iiBWDRSPLAuj4qw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/vRq2VeyVuil7iiBWDRSPLAuj4qw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vRq2VeyVuil7iiBWDRSPLAuj4qw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/wxwYl9QI7YQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/4268791625302135125/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/rrs-module-3.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/4268791625302135125?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/4268791625302135125?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/wxwYl9QI7YQ/rrs-module-3.html" title="RRS - Module 3" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/rrs-module-3.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0IDRHoyfSp7ImA9WxNWFEg.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-5425809749564100405</id><published>2009-10-13T09:29:00.000-07:00</published><updated>2009-10-13T09:32:55.495-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-13T09:32:55.495-07:00</app:edited><title>RRS - Module 2</title><content type="html">&lt;p&gt;You have identified the objects and their members in the previous module. In this module, let us start the project by first creating the two main important objects – User and Admin &lt;/p&gt;&lt;p&gt;Steps to be followed in this module:&lt;br /&gt;&lt;/p&gt; &lt;ol&gt;&lt;li&gt;Create a base class called Person, by identifying the common attributes of user and admin&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Create 2 derived classes called, User and Admin, for the above given base class &lt;/li&gt;&lt;li&gt;Write the code for the Interface Menu.&lt;br /&gt;&lt;ol&gt;&lt;ol&gt;&lt;li&gt; Initialize the system by creating an object for Admin class&lt;br /&gt;&lt;/li&gt;&lt;li&gt; Save the Admin object in a file&lt;br /&gt;&lt;/li&gt;&lt;li&gt; Create two interfaces where one is for Admin and the other is for User&lt;br /&gt;&lt;/li&gt;&lt;li&gt; Login as the Admin and do the following&lt;br /&gt;&lt;ol&gt;&lt;li&gt; Add new users&lt;br /&gt;&lt;/li&gt;&lt;li&gt; Save the objects to the file&lt;br /&gt;&lt;/li&gt;&lt;li&gt; Display the User Details. To achieve this read the details from the file sequentially as objects and display the details in a tabular format.&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;li&gt;Logout from Admin Interface and login to the User Interface. Do the following:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Display his own details &lt;/li&gt;&lt;li&gt;User should be able to change his own details.&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-5425809749564100405?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/2JLZbQRHxpgINv5V18FzOh97cFM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/2JLZbQRHxpgINv5V18FzOh97cFM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/2JLZbQRHxpgINv5V18FzOh97cFM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/2JLZbQRHxpgINv5V18FzOh97cFM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/i58P6gFTFXQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/5425809749564100405/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/rrs-module-2.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/5425809749564100405?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/5425809749564100405?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/i58P6gFTFXQ/rrs-module-2.html" title="RRS - Module 2" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/rrs-module-2.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0MDR3c4eCp7ImA9WxNWFEg.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-5898031272474645268</id><published>2009-10-13T09:26:00.000-07:00</published><updated>2009-10-13T09:31:16.930-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-13T09:31:16.930-07:00</app:edited><title>Mini Project - Railway Reservation System</title><content type="html">&lt;h1&gt;&lt;span class="mw-headline"&gt;Module 1 &lt;/span&gt;&lt;/h1&gt; &lt;a name="Overview" id="Overview"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Overview  &lt;/span&gt;&lt;/h2&gt; &lt;p&gt;&lt;b&gt;Railway Reservation System-(RRS)&lt;/b&gt; Project is developed to help students learn and realize capability of C programming language, and appreciate procedural approach of developing a system. &lt;/p&gt;&lt;p&gt;RRS helps users reserve/cancel berths, search for trains, check reservation/cancellation history, etc...  &lt;/p&gt;&lt;p&gt;On the other hand it helps the railway reservation officers to prepare reservation charts, organize train schedules, etc...  &lt;/p&gt;&lt;p&gt;RRS provides two types of log-ins one is the User log-in for railway customers and the other one is Admin log-in for railway reservation officers. &lt;/p&gt;&lt;p&gt;Project is divided into 10 modules of four hours duration each.  &lt;/p&gt;&lt;p&gt;All the modules will give exactly what to develop in the 4 hour time frame. Since most of the modules are inter-related it is important to keep track of the previous code. After submission it is allowed to make changes in the previous code to suit / integrate well to the system. &lt;/p&gt;&lt;p&gt;Before we start the project, we need to make the following assumptions:  &lt;/p&gt; &lt;ol&gt;&lt;li&gt;All the seats are reserved seats.  &lt;/li&gt;&lt;li&gt;There are only two classes for which users can book tickets  &lt;ol&gt;&lt;li&gt;Sleeper  &lt;/li&gt;&lt;li&gt;A/C  &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;li&gt;The number of seats in all the trains is fixed for each class  &lt;ol&gt;&lt;li&gt;Sleeper – 12 seats  &lt;/li&gt;&lt;li&gt;A/C – 8 seats  &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;li&gt;Passengers cannot book tickets before 5 days of travel date.  &lt;/li&gt;&lt;li&gt;Passengers can book tickets from 5 days before the scheduled train departure time and up to 3 hours before the scheduled train departure. &lt;/li&gt;&lt;li&gt;All the trains will not stop in between stations.  &lt;/li&gt;&lt;li&gt;Trains start from source station and ends at destination station.  &lt;/li&gt;&lt;li&gt;All the transactions for booking tickets / refunding the canceled tickets are through online electronic clearance.  &lt;/li&gt;&lt;li&gt;Maximum waiting list per each class is 4 seats.  &lt;/li&gt;&lt;li&gt;Auto upgrade waiting list to confirmed ticket.  &lt;/li&gt;&lt;li&gt;A user can book only 2 seats at a time.  &lt;/li&gt;&lt;li&gt;User can book tickets for others (Family Members, Friends etc) using personal log-in account.  &lt;/li&gt;&lt;li&gt;Each train will run on all the days of the week. &lt;/li&gt;&lt;/ol&gt; &lt;a name="Resources" id="Resources"&gt;&lt;/a&gt;&lt;p&gt;&lt;b&gt;Module 1&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;You have already developed the project in C programming language. You would have a good idea regarding the Railway Reservation System. In this module, let’s design the project according to our previous understanding of the project. &lt;/p&gt;&lt;p&gt;While designing the project, make sure that you imbibe all the important aspects of object oriented concepts that you have learnt in the last 1 week. You also need to take into account the aspect of reusability of your code. &lt;/p&gt;&lt;p&gt;Steps to be done in this module:&lt;br /&gt;1. Identify the classes that you would require in this project.&lt;br /&gt;2. Identify member variables and functions of each class that you have identified in the previous step.&lt;br /&gt;3. Identify where the various object oriented concepts could be used in the project – inheritance, polymorphism, virtual functions, friend functions, etc&lt;br /&gt;4. Draw these classes in a word document.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W2/M3/module1.JPG" alt="module1.JPG" /&gt; &lt;/p&gt;The diagram shows all the member variables and functions of each class identified for a School scenario. It also shows that Student and Teacher classes are inherited from Person class.&lt;br /&gt;Note: Draw the classes that you have identified for the project in a similar way as shown in the above example.&lt;br /&gt;5. Identify the various details that you would require to store in files. (You need to write objects into files instead of single data). Document the folder/file structure that would make your programming efficient&lt;br /&gt;6. Identify what kind of data could lead to exceptions in your project&lt;br /&gt;7. Show the documentation to your mentor and take their consent&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-5898031272474645268?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/4CyJ9dIyDrBlm8A9fQmuxE5zcX4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4CyJ9dIyDrBlm8A9fQmuxE5zcX4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/4CyJ9dIyDrBlm8A9fQmuxE5zcX4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4CyJ9dIyDrBlm8A9fQmuxE5zcX4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/egIJHalDo70" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/5898031272474645268/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/mini-project-railway-reservation-system.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/5898031272474645268?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/5898031272474645268?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/egIJHalDo70/mini-project-railway-reservation-system.html" title="Mini Project - Railway Reservation System" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/mini-project-railway-reservation-system.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0YBQHs7eyp7ImA9WxNWFEg.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-4582640464522066253</id><published>2009-10-13T09:25:00.001-07:00</published><updated>2009-10-13T09:25:51.503-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-13T09:25:51.503-07:00</app:edited><title>Standard Template Library</title><content type="html">&lt;h2&gt; &lt;span class="mw-headline"&gt; Learning Objective  &lt;/span&gt;&lt;/h2&gt; &lt;p&gt;At the end of this module, you will be able to: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Understand Standard Template Library  &lt;/li&gt;&lt;li&gt;Explain the need of STL &lt;/li&gt;&lt;li&gt;Understand and use the vector and list of STL. &lt;/li&gt;&lt;li&gt;Using real world objects with STL. &lt;/li&gt;&lt;/ul&gt; &lt;a name="Standard_Template_Library" id="Standard_Template_Library"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Standard Template Library&lt;/span&gt;&lt;/h2&gt; &lt;a name="Introduction" id="Introduction"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Introduction &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;The Standard Template Library (STL) is a fundamental part of the C++ Standard, is a C++ library of container classes, algorithms, and iterators; it provides many of the basic algorithms and data structures of computer science. The STL is a generic library, meaning that its components are heavily parameterized: almost every component in the STL is a template. You should make sure that you understand how templates work in C++ before you use the STL. &lt;/p&gt;&lt;p&gt;The STL contains several kinds of entities. The three most important are containers, algorithms,and iterators. &lt;/p&gt;&lt;p&gt;A &lt;b&gt;container&lt;/b&gt; is a way that stored data is organized in memory. In earlier chapters we’ve explored two kinds of containers: stacks and linked lists. Another container, the array, is so common that it’s built into C++ (and most other computer languages). However, there are many other kinds of containers, and the STL includes the most useful. The STL containers are implemented by template classes, so they can be easily customized to hold different kinds of data. &lt;/p&gt;&lt;p&gt;&lt;b&gt;Algorithms&lt;/b&gt; in the STL are procedures that are applied to containers to process their data in various ways. For example, there are algorithms to sort, copy, search, and merge data. Algorithms are represented by template functions. These functions are not member functions of the container classes. Rather, they are standalone functions. Indeed, one of the striking characteristics of the STL is that its algorithms are so general. You can use them not only on STL containers, but on ordinary C++ arrays and on containers you create yourself. (Containers also include member functions for more specific tasks.) &lt;/p&gt;&lt;p&gt;&lt;b&gt;Iterators&lt;/b&gt; are a generalization of the concept of pointers: they point to elements in a container. You can increment an iterator, as you can a pointer, so it points in turn to each element in a container. Iterators are a key part of the STL because they connect algorithms with containers. &lt;/p&gt;&lt;p&gt;Think of them as a software version of cables (like the cables that connect stereo components together or a computer to its peripherals). &lt;/p&gt;&lt;p&gt;Figure below shows these three main components of the STL.  &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W2/M2/stl.jpg" alt="stl.jpg" /&gt; &lt;/p&gt;&lt;p&gt;In this &lt;b&gt;Module&lt;/b&gt; we’ll discuss and learn about containers( Vectors and List). &lt;/p&gt; &lt;a name="Resources" id="Resources"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Resources&lt;/span&gt;&lt;/h2&gt; &lt;p&gt;Web Resources: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html" class="external text" title="http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html" rel="nofollow"&gt;C++ STL (Standard Template Library) Tutorial and Examples&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;STL: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.sgi.com/tech/stl/Vector.html" class="external free" title="http://www.sgi.com/tech/stl/Vector.html" rel="nofollow"&gt;http://www.sgi.com/tech/stl/Vector.html&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.sgi.com/tech/stl/List.html" class="external free" title="http://www.sgi.com/tech/stl/List.html" rel="nofollow"&gt;http://www.sgi.com/tech/stl/List.html&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;PPT: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://wiki.msitprogram.net/CPP09/W2/M2/cpphtp4_PPT_21.ppt" class="external text" title="http://wiki.msitprogram.net/CPP09/W2/M2/cpphtp4_PPT_21.ppt" rel="nofollow"&gt;Chapter 28: Standard Template Library&lt;/a&gt;--&gt; &lt;b&gt;How to Program in C++ by Dietle and Dietle&lt;/b&gt; &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;br /&gt;&lt;/p&gt; &lt;a name="Step_to_attack_the_problem_sets" id="Step_to_attack_the_problem_sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Step to attack the problem sets&lt;/span&gt;&lt;/h2&gt; &lt;ul&gt;&lt;li&gt;Step 1:  Go through the introduction part and have an understanding about STL. &lt;/li&gt;&lt;li&gt;Step 2: Go through the Web Resource 1 (Try the problem sets given) &lt;ul&gt;&lt;li&gt;C++ STL Tutorial Contents: &lt;ol&gt;&lt;li&gt; STL description &lt;/li&gt;&lt;li&gt; STL vector &lt;/li&gt;&lt;li&gt; STL list &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;Step 3: Go through the PPT given (Try the problem sets given in that) &lt;/li&gt;&lt;li&gt;Step 4: Attack the problem sets in the given order. &lt;/li&gt;&lt;li&gt;Step 5: If you are not clear with the problem sets then contact your respective mentor for clarification.  &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Spend atleast 2-3 Hours from step 1 - step 3. &lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt; &lt;a name="Problem_Sets" id="Problem_Sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Problem Sets&lt;/span&gt;&lt;/h2&gt; &lt;a name="Problem_Set_A" id="Problem_Set_A"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set A&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt; Create a vector&lt;char&gt; containing the letters of the alphabets in order. Print the elements of that vector in order and in reverse order. &lt;/p&gt; &lt;pre&gt;Name the program as: PA1_vector.cpp&lt;br /&gt;&lt;/pre&gt; &lt;a name="Problem_Set_B" id="Problem_Set_B"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set B&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt; Create a list&lt;string&gt;  and read a list of names of fruits from cin into it. Sort the list and print it. &lt;/p&gt; &lt;pre&gt;Name the program as: PB1_list_sort.cpp&lt;br /&gt;&lt;/pre&gt; &lt;a name="Problem_Set_C" id="Problem_Set_C"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set C&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt; Write two separate functions for Week 2 Module 1 Problem Set A - 1(cricket Stats).  &lt;/p&gt;&lt;p&gt;Function 1: Load all the data from the binary file into the List and return the List. &lt;/p&gt;&lt;p&gt;Function 2:  Load all the data from the binary file into the Vector and return the Vector. &lt;/p&gt;&lt;p&gt;Comparison of two instances of Player should be based on Name.  &lt;/p&gt;&lt;p&gt;Override the operators = , == and &lt;&gt;&lt;p&gt;Print all the player details sorted on Player Name Using List. &lt;/p&gt;&lt;p&gt;Print all the player details using Vector. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-4582640464522066253?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/RHn85_WLjIVYlLUCb4brOA3wtEI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RHn85_WLjIVYlLUCb4brOA3wtEI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/RHn85_WLjIVYlLUCb4brOA3wtEI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RHn85_WLjIVYlLUCb4brOA3wtEI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/yjkUV9Oz95Y" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/4582640464522066253/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/standard-template-library.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/4582640464522066253?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/4582640464522066253?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/yjkUV9Oz95Y/standard-template-library.html" title="Standard Template Library" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/standard-template-library.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0YERXo7fSp7ImA9WxNWFEg.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-6727042311151925736</id><published>2009-10-13T09:23:00.000-07:00</published><updated>2009-10-13T09:25:04.405-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-13T09:25:04.405-07:00</app:edited><title>Streams &amp; Files</title><content type="html">&lt;h1&gt; &lt;span class="mw-headline"&gt; CPP09 W2 M1 Streams and Files  &lt;/span&gt;&lt;/h1&gt; &lt;a name="Learning_Objective" id="Learning_Objective"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Learning Objective  &lt;/span&gt;&lt;/h2&gt; &lt;ul&gt;&lt;li&gt;Understanding streams  &lt;/li&gt;&lt;li&gt;Understand the stream class hierarchy  &lt;/li&gt;&lt;li&gt;Understand the concepts of stream insertion and extraction  &lt;/li&gt;&lt;li&gt;Use streams for file input and output  &lt;/li&gt;&lt;li&gt;Distinguish between text and binary file input and output  &lt;/li&gt;&lt;li&gt;Write programs for random access of data files using:  &lt;ul&gt;&lt;li&gt;get pointer  &lt;/li&gt;&lt;li&gt;put pointer  &lt;/li&gt;&lt;li&gt;seekg()  &lt;/li&gt;&lt;li&gt;tellg()  &lt;/li&gt;&lt;li&gt;seekp()  &lt;/li&gt;&lt;li&gt;tellp() &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;/ul&gt; &lt;a name="Streams_and_Files" id="Streams_and_Files"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Streams and Files  &lt;/span&gt;&lt;/h2&gt; &lt;a name="Introduction" id="Introduction"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Introduction  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;In simple words, a stream is a sequence of bytes. In input operations, the bytes are transferred from a device (a keyboard, a disk drive, a network connection etc.) to the main memory. Where as in case of output operations, this is reverse. That means, the bytes are transferred from main memory to a device such as a display screen, a printer, a disk drive, network connection, a tape( a file on tape) etc. &lt;/p&gt; &lt;a name="Streams" id="Streams"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Streams  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;In C++, a stream is a source or destination for collection of characters. Streams are of two types:  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Output stream  &lt;/li&gt;&lt;li&gt;Input Stream &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Output streams allow you to write or store characters, and input streams allow you to read or fetch character. Applications can use both these streams. &lt;/p&gt; &lt;a name="The_Stream_Class_Hierarchy" id="The_Stream_Class_Hierarchy"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; The Stream Class Hierarchy  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;The stream classes are arranged in a rather complex hierarchy. Figure below shows the arrangement of the most important of these classes. &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W2/M1/streams_h.jpg" alt="streams_h.jpg" /&gt;  &lt;/p&gt;&lt;p&gt;We’ve already made extensive use of some stream classes. The extraction operator &lt;i&gt;&gt;&gt;&lt;/i&gt; is a member of the istream class, and the insertion operator &lt;i&gt;&lt;&lt;&lt;/i&gt; is a member of the ostream class. Both of these classes are derived from the &lt;i&gt;ios&lt;/i&gt; class. The cout object, representing the standard output stream, which is usually directed to the video display, is a predefined object of the &lt;i&gt;ostream_withassign&lt;/i&gt; class, which is derived from the &lt;i&gt;ostream&lt;/i&gt; class. Similarly, &lt;i&gt;cin&lt;/i&gt; is an object of the &lt;i&gt;istream_withassign&lt;/i&gt; class, which is derived from &lt;i&gt;istream&lt;/i&gt;. &lt;/p&gt;&lt;p&gt;The istream and ostream classes are derived from ios and are dedicated to input and output, respectively. The istream class contains such functions as get(), getline(), read(), and the overloaded extraction (&gt;&gt;) operators, while ostream contains put() and write(), and the overloaded insertion (&lt;&lt;) operators. &lt;/p&gt;&lt;p&gt;The iostream class is derived from both istream and ostream by multiple inheritance. Classes derived from it can be used with devices, such as disk files, that may be opened for both input and output at the same time. Three classes—istream_withassign, ostream_withassign, and iostream_withassign—are inherited from istream, ostream, and iostream, respectively. They add assignment operators to these classes. &lt;/p&gt;&lt;p&gt;The ios class is the granddaddy of all the stream classes, and contains the majority of the features you need to operate C++ streams. The three most important features are the formatting flags, the error-status flags, and the file operation mode. &lt;/p&gt; &lt;a name="Stream_Insertion_and_Extraction" id="Stream_Insertion_and_Extraction"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Stream Insertion and Extraction&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;Stream Insertion&lt;/b&gt; &lt;/p&gt;&lt;p&gt;Stream classes have their own member data, functions definitions. Class ostream contains functions defined for output operations. These operations are called stream insertions. The &lt;&lt;&gt;&lt;p&gt;cout&lt;&lt;"Hello World\n"; &lt;/p&gt;&lt;p&gt;translates as: the text string to the right of the inserter is to be stored in the stream object on the left. The &lt;&lt;&gt;&lt;p&gt;&lt;b&gt;Stream Extraction&lt;/b&gt; &lt;/p&gt;&lt;p&gt;The opposite of insertion is extraction, which is fetching of data from an input stream. Input stream operations are defined in istream class. The &gt;&gt; operator, called the extractor, can accept any fundamental data type. The most important point to note is that the extractor skips leading white spaces(&lt;i&gt;,\n&lt;/i&gt;\t') while extracting any of these data types. cin is the predefined object if the istream class attached to standard input device which is keyboard. &lt;/p&gt; &lt;a name="User_defined_Streams" id="User_defined_Streams"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;User defined Streams&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;The input and output streams discussed so far dealt only with standard input and output. C++ also provides specific classes which deal with user-defined streams. User-defined streams are in the form of files. In C++ a files is linked to a stream. Before a file can be opened. a stream must be obtained. These streams are more powerful than the pre-defined streams. &lt;/p&gt; &lt;a name="File_I.2FO_with_Streams" id="File_I.2FO_with_Streams"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;File I/O with Streams&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;Most programs need to save data to disk files and read it back in. Working with disk files requires another set of classes: ifstream for input, fstream for both input and output, and ofstream for output. Objects of these classes can be associated with disk files, and we can use their member functions to read and write to the files. &lt;/p&gt;&lt;p&gt;Referring back to Figure previously, you can see that ifstream is derived from istream, fstream is derived from iostream, and ofstream is derived from ostream. These ancestor classes are in turn derived from ios. Thus the file-oriented classes derive many of their member functions from more general classes. The file-oriented classes are also derived, by multiple inheritance, from the fstreambase class. This class contains an object of class filebuf, which is a fileoriented buffer, and its associated member functions, derived from the more general streambuf class. You don’t usually need to worry about these buffer classes. &lt;/p&gt;&lt;p&gt;The ifstream, ofstream, and fstream classes are declared in the FSTREAM file. &lt;/p&gt; &lt;a name="Formatted_File_I.2FO" id="Formatted_File_I.2FO"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Formatted File I/O&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;In formatted I/O, numbers are stored on disk as a series of characters. Thus 6.02, rather than being stored as a 4-byte type float or an 8-byte type double, is stored as the characters ‘6’, ‘.’, ‘0’, and ‘2’. This can be inefficient for numbers with many digits, but it’s appropriate in many situations and easy to implement. Characters and strings are stored more or less normally. &lt;/p&gt; &lt;a name="Writing_Data" id="Writing_Data"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Writing Data&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;The following program writes a character, an integer, a type double, and two string objects to a disk file. There is no output to the screen. Here’s the listing for FORMATO: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// formato.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// writes formatted output to a file, using &lt;&lt;&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;fstream&gt; //for file I/O&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;string&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;char&lt;/span&gt; ch &lt;span class="sy1"&gt;=&lt;/span&gt; ‘x’&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; j &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="nu0"&gt;77&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;double&lt;/span&gt; d &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="nu16"&gt;6.02&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;string str1 &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="st0"&gt;"Kafka"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//strings without&lt;/span&gt;&lt;br /&gt;string str2 &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="st0"&gt;"Proust"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;// embedded spaces&lt;/span&gt;&lt;br /&gt;ofstream outfile&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;"fdata.txt"&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//create ofstream object&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="sy3"&gt;!&lt;/span&gt;outfile&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;&lt;span class="st0"&gt;"Error Opening File"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;outfile &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; ch &lt;span class="co1"&gt;//insert (write) data&lt;/span&gt;&lt;br /&gt;        &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; j&lt;br /&gt;        &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; ‘ ‘ &lt;span class="co1"&gt;//needs space between numbers&lt;/span&gt;&lt;br /&gt;        &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; d&lt;br /&gt;        &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; str1&lt;br /&gt;        &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; ‘ ‘ &lt;span class="co1"&gt;//needs spaces between strings&lt;/span&gt;&lt;br /&gt;        &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; str2&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"File written&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;Here we define an object called outfile to be a member of the ofstream class. At the same time, we initialize it to the file FDATA.TXT. This initialization sets aside various resources for the file, and accesses or opens the file of that name on the disk. If the file doesn’t exist, it is created. If it does exist, it is truncated and the new data replaces the old. The outfile object acts much as cout did in previous programs, so we can use the insertion operator (&lt;&lt;) to output variables of any basic type to the file. This works because the insertion operator is appropriately overloaded in ostream, from which ofstream is derived. &lt;/p&gt;&lt;p&gt;When the program terminates, the outfile object goes out of scope. This calls its destructor, which closes the file, so we don’t need to close the file explicitly. There are several potential formatting glitches. First, you must separate numbers (such as 77 and 6.02) with nonnumeric characters. Since numbers are stored as a sequence of characters, rather than as a fixed-length field, this is the only way the extraction operator will know, when the data is read back from the file, where one number stops and the next one begins. Second, strings must be separated with whitespace for the same reason. This implies that strings cannot contain imbedded blanks. In this example we use the space character (‘ ‘) for both kinds of delimiters. Characters need no delimiters, since they have a fixed length. You can verify that FORMATO has indeed written the data by examining the FDATA.TXT file with the Windows WORDPAD accessory or the DOS command TYPE. &lt;/p&gt; &lt;a name="Reading_Data" id="Reading_Data"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Reading Data&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;We can read the file generated by FORMATO by using an ifstream object, initialized to the name of the file. The file is automatically opened when the object is created. We can then read from it using the extraction (&gt;&gt;) operator. &lt;/p&gt;&lt;p&gt;Here’s the listing for the FORMATI program, which reads the data back in from the FDATA.TXT file: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// formati.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// reads formatted output from a file, using &gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;fstream&gt; //for file I/O&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;string&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;char&lt;/span&gt; ch&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; j&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;double&lt;/span&gt; d&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;string str1&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;string str2&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;ifstream infile&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;"fdata.txt"&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//create ifstream object&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="sy3"&gt;!&lt;/span&gt;infile&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;&lt;span class="st0"&gt;"File Cannot be opened"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//extract (read) data from it&lt;/span&gt;&lt;br /&gt;infile &lt;span class="sy1"&gt;&gt;&gt;&lt;/span&gt; ch &lt;span class="sy1"&gt;&gt;&gt;&lt;/span&gt; j &lt;span class="sy1"&gt;&gt;&gt;&lt;/span&gt; d &lt;span class="sy1"&gt;&gt;&gt;&lt;/span&gt; str1 &lt;span class="sy1"&gt;&gt;&gt;&lt;/span&gt; str2&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; ch &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; endl &lt;span class="co1"&gt;//display the data&lt;/span&gt;&lt;br /&gt;     &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; j &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; endl&lt;br /&gt;     &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; d &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; endl&lt;br /&gt;     &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; str1 &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; endl&lt;br /&gt;     &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; str2 &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;Here the ifstream object, which we name infile, acts much the way cin did in previous programs. Provided that we have formatted the data correctly when inserting it into the file, there’s no trouble extracting it, storing it in the appropriate variables, and displaying its contents. The program’s output looks like this: &lt;/p&gt; &lt;pre&gt;x&lt;br /&gt;77&lt;br /&gt;6.02&lt;br /&gt;Kafka&lt;br /&gt;Proust&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;Of course the numbers are converted back to their binary representations for storage in the program. That is, the 77 is stored in the variable j as a type int, not as two characters, and the 6.02 is stored as a double. &lt;/p&gt; &lt;a name="Character_I.2FO" id="Character_I.2FO"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Character I/O&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;The Character I/O functions such as get() and put() can be used when the programmer wishes to read / write white space characters also. So, with Character I/O the problem of accepting and writing character/string with white spaces is solved. &lt;/p&gt;&lt;p&gt;The put() and get() functions, which are members of ostream and istream, respectively, can be used to output and input single characters. Here’s a program, OCHAR, that outputs a string, one character at a time: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// ochar.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// file output with characters&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;fstream&gt; //for file functions&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;string&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;string str &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="st0"&gt;"Time is a great teacher, but unfortunately "&lt;/span&gt;&lt;br /&gt;             &lt;span class="st0"&gt;"it kills all its pupils. Berlioz"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;ofstream outfile&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;"TEST.TXT"&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//create file for output&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="sy3"&gt;!&lt;/span&gt;outfile&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;&lt;span class="st0"&gt;"File Cannot be opened"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw1"&gt;for&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; j&lt;span class="sy1"&gt;=&lt;/span&gt;&lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy1"&gt;&lt;&lt;/span&gt;str.&lt;span class="me1"&gt;size&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="co1"&gt;//for each character,&lt;/span&gt;&lt;br /&gt; outfile.&lt;span class="me1"&gt;put&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt; str&lt;span class="br0"&gt;[&lt;/span&gt;j&lt;span class="br0"&gt;]&lt;/span&gt; &lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//write it to file&lt;/span&gt;&lt;br /&gt;&lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"File written&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;The length of the string object str is found using the size() member function, and the characters are output using put() in a for loop. &lt;/p&gt;&lt;p&gt;We can read this file back in and display it using the ICHAR program. &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// ichar.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// file input with characters&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;fstream&gt; //for file functions&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;char&lt;/span&gt; ch&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//character to read&lt;/span&gt;&lt;br /&gt;ifstream infile&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;"TEST.TXT"&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//create file for input&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="sy3"&gt;!&lt;/span&gt;infile&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;&lt;span class="st0"&gt;"File Cannot be opened"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw1"&gt;while&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt; infile &lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="co1"&gt;//read until EOF or error&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt; infile.&lt;span class="me1"&gt;get&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;ch&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//read character&lt;/span&gt;&lt;br /&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; ch&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//display it&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;&lt;br /&gt;&lt;/p&gt; &lt;a name="Binary_Input_and_Output" id="Binary_Input_and_Output"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Binary Input and Output&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;All the input and output opeartions you have seen so far are text or character based. That is, all information is stored in the same format as it would be dispalyed on the screen. &lt;/p&gt;&lt;p&gt;You can write a few numbers to disk using formatted I/O, but if you’re storing a large amount of numerical data it’s more efficient to use binary I/O, in which numbers are stored as they are in the computer’s RAM memory, rather than as strings of characters. In binary I/O an int is stored in 4 bytes, whereas its text version might be "12345", requiring 5 bytes. Similarly, a float is always stored in 4 bytes, while its formatted version might be "6.02314e13", requiring 10 bytes. &lt;/p&gt;&lt;p&gt;Our next example shows how an array of integers is written to disk and then read back into memory, using binary format. We use two new functions: write(), a member of ofstream; and read(), a member of ifstream. These functions think about data in terms of bytes (type char). They don’t care how the data is formatted, they simply transfer a buffer full of bytes from and to a disk file. The parameters to write() and read() are the address of the data buffer and its length. The address must be cast, using reinterpret_cast, to type char*, and the length is the length in bytes (characters), not the number of data items in the buffer. Here’s the listing for BINIO: &lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// binio.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// binary input and output with integers&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;fstream&gt; //for file streams&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;const&lt;/span&gt; &lt;span class="kw4"&gt;int&lt;/span&gt; MAX &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="nu0"&gt;100&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//size of buffer&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; buff&lt;span class="br0"&gt;[&lt;/span&gt;MAX&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//buffer for integers&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;for&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; j&lt;span class="sy1"&gt;=&lt;/span&gt;&lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy1"&gt;&lt;&lt;/span&gt;MAX&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="co1"&gt;//fill buffer with data&lt;/span&gt;&lt;br /&gt;buff&lt;span class="br0"&gt;[&lt;/span&gt;j&lt;span class="br0"&gt;]&lt;/span&gt; &lt;span class="sy1"&gt;=&lt;/span&gt; j&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//(0, 1, 2, ...)&lt;/span&gt;&lt;br /&gt;             &lt;span class="co1"&gt;//create output stream&lt;/span&gt;&lt;br /&gt;ofstream os&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;"edata.dat"&lt;/span&gt;, ios&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;binary&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="sy3"&gt;!&lt;/span&gt;os&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span class="kw3"&gt;cerr&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"File cannot be opened&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//write to it&lt;/span&gt;&lt;br /&gt;os.&lt;span class="me1"&gt;write&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt; &lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;buff&lt;span class="br0"&gt;)&lt;/span&gt;, MAX&lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="kw3"&gt;sizeof&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;os.&lt;span class="me1"&gt;close&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//must close it&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;for&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;j&lt;span class="sy1"&gt;=&lt;/span&gt;&lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy1"&gt;&lt;&lt;/span&gt;MAX&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="co1"&gt;//erase buffer&lt;/span&gt;&lt;br /&gt;buff&lt;span class="br0"&gt;[&lt;/span&gt;j&lt;span class="br0"&gt;]&lt;/span&gt; &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//create input stream&lt;/span&gt;&lt;br /&gt;ifstream is&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;"edata.dat"&lt;/span&gt;, ios&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;binary&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//read from it&lt;/span&gt;&lt;br /&gt;is.&lt;span class="me1"&gt;read&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt; &lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;buff&lt;span class="br0"&gt;)&lt;/span&gt;, MAX&lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="kw3"&gt;sizeof&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;for&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;j&lt;span class="sy1"&gt;=&lt;/span&gt;&lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy1"&gt;&lt;&lt;/span&gt;MAX&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="co1"&gt;//check data&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt; buff&lt;span class="br0"&gt;[&lt;/span&gt;j&lt;span class="br0"&gt;]&lt;/span&gt; &lt;span class="sy3"&gt;!&lt;/span&gt;&lt;span class="sy1"&gt;=&lt;/span&gt; j &lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw3"&gt;cerr&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"Data is incorrect&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;1&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"Data is correct&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;You must use the ios::binary argument in the second parameter to write() and read() when working with binary data. This is because the default, text mode, takes some liberties with the data. For example, in text mode the ‘\n’ character is expanded into two bytes—a carriagereturn and a linefeed—before being stored to disk. This makes a formatted text file more readable by DOS-based utilities such as TYPE, but it causes confusion when it is applied to binary data, since every byte that happens to have the ASCII value 10 is translated into 2 bytes. The ios::binary argument is an example of a mode bit. We’ll say more about this when we discuss the open() function later in this chapter. &lt;/p&gt; &lt;a name="The_reinterpret_cast_Operator" id="The_reinterpret_cast_Operator"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;The reinterpret_cast Operator&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;In the BINIO program (and many others to follow) we use the reinterpret_cast operator to make it possible for a buffer of type int to look to the read() and write() functions like a buffer of type char. &lt;/p&gt;&lt;p&gt;is.read( reinterpret_cast&lt;char*&gt;(buff), MAX*sizeof(int) ); &lt;/p&gt;&lt;p&gt;The reinterpret_cast operator is how you tell the compiler, “I know you won’t like this, but I want to do it anyway.” It changes the type of a section of memory without caring whether it makes sense, so it’s up to you to use it judiciously. You can also use reinterpret_cast to change pointer values into integers and vice versa. This is a dangerous practice, but one which is sometimes necessary. &lt;/p&gt; &lt;a name="Closing_Files" id="Closing_Files"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Closing Files&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;So far in our example programs there has been no need to close streams explicitly because they are closed automatically when they go out of scope; this invokes their destructors and closes the associated file. However, in BINIO, since both the output stream os and the input stream is are associated with the same file, EDATA.DAT, the first stream must be closed before the second is opened. We use the close() member function for this. You may want to use an explicit close() every time you close a file, without relying on the stream’s destructor. This is potentially more reliable, and certainly makes the listing more readable. &lt;/p&gt; &lt;a name="Object_I.2FO" id="Object_I.2FO"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Object I/O&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;Since C++ is an object-oriented language, it’s reasonable to wonder how objects can be written to and read from disk. The next examples show the process. &lt;/p&gt; &lt;a name="Writing_an_Object_to_Disk" id="Writing_an_Object_to_Disk"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Writing an Object to Disk&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;When writing an object, we generally want to use binary mode. This writes the same bit configuration to disk that was stored in memory, and ensures that numerical data contained in objects is handled properly. Here’s the listing for OPERS, which asks the user for information about an object of class person, and then writes this object to the disk file PERSON.DAT: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// opers.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// saves person object to disk&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;fstream&gt; //for file streams&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; person &lt;span class="co1"&gt;//class of persons&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;protected&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;          &lt;span class="kw4"&gt;char&lt;/span&gt; name&lt;span class="br0"&gt;[&lt;/span&gt;80&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//person’s name&lt;/span&gt;&lt;br /&gt;          &lt;span class="kw4"&gt;short&lt;/span&gt; age&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//person’s age&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;       &lt;span class="kw4"&gt;void&lt;/span&gt; getData&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="co1"&gt;//get person’s data&lt;/span&gt;&lt;br /&gt;       &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “Enter name&lt;span class="sy4"&gt;:&lt;/span&gt; “&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="kw3"&gt;cin&lt;/span&gt; &lt;span class="sy1"&gt;&gt;&gt;&lt;/span&gt; name&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “Enter age&lt;span class="sy4"&gt;:&lt;/span&gt; “&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="kw3"&gt;cin&lt;/span&gt; &lt;span class="sy1"&gt;&gt;&gt;&lt;/span&gt; age&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;person pers&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//create a person&lt;/span&gt;&lt;br /&gt;pers.&lt;span class="me1"&gt;getData&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//get data for person&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//create ofstream object&lt;/span&gt;&lt;br /&gt;ofstream outfile&lt;span class="br0"&gt;(&lt;/span&gt;“PERSON.&lt;span class="me1"&gt;DAT&lt;/span&gt;”, ios&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;binary&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="sy3"&gt;!&lt;/span&gt;outfile&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span class="kw3"&gt;cerr&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"File cannot be Opened"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//write to it&lt;/span&gt;&lt;br /&gt;outfile.&lt;span class="me1"&gt;write&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;pers&lt;span class="br0"&gt;)&lt;/span&gt;, &lt;span class="kw3"&gt;sizeof&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;pers&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;The getData() member function of person is called to prompt the user for information, which it places in the pers object. Here’s some sample interaction: &lt;/p&gt; &lt;pre&gt;Enter name: Coleridge&lt;br /&gt;Enter age: 62&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;The contents of the pers object are then written to disk, using the write() function. We use the sizeof operator to find the length of the pers object. &lt;/p&gt; &lt;a name="Reading_an_Object_from_Disk" id="Reading_an_Object_from_Disk"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Reading an Object from Disk&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;Reading an object back from the PERSON.DAT file requires the read() member function. Here’s the listing for IPERS: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// ipers.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// reads person object from disk&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;fstream&gt; //for file streams&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; person &lt;span class="co1"&gt;//class of persons&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;protected&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;          &lt;span class="kw4"&gt;char&lt;/span&gt; name&lt;span class="br0"&gt;[&lt;/span&gt;80&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//person’s name&lt;/span&gt;&lt;br /&gt;          &lt;span class="kw4"&gt;short&lt;/span&gt; age&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//person’s age&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;       &lt;span class="kw4"&gt;void&lt;/span&gt; showData&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="co1"&gt;//display person’s data&lt;/span&gt;&lt;br /&gt;       &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “Name&lt;span class="sy4"&gt;:&lt;/span&gt; “ &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; name &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “Age&lt;span class="sy4"&gt;:&lt;/span&gt; “ &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; age &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;person pers&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//create person variable&lt;/span&gt;&lt;br /&gt;ifstream infile&lt;span class="br0"&gt;(&lt;/span&gt;“PERSON.&lt;span class="me1"&gt;DAT&lt;/span&gt;”, ios&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;binary&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//create stream&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="sy3"&gt;!&lt;/span&gt;infile&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span class="kw3"&gt;cerr&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"File cannot be opened"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//read stream&lt;/span&gt;&lt;br /&gt;infile.&lt;span class="me1"&gt;read&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt; &lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;pers&lt;span class="br0"&gt;)&lt;/span&gt;, &lt;span class="kw3"&gt;sizeof&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;pers&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;pers.&lt;span class="me1"&gt;showData&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//display person&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;The output from IPERS reflects whatever data the OPERS program placed in the PERSON.DAT file: &lt;/p&gt; &lt;pre&gt;Name: Coleridge&lt;br /&gt;Age: 62&lt;br /&gt;&lt;/pre&gt; &lt;a name="Compatible_Data_Structures" id="Compatible_Data_Structures"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Compatible Data Structures&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;To work correctly, programs that read and write objects to files, as do OPERS and IPERS, must be talking about the same class of objects. Objects of class person in these programs are exactly 82 bytes long: The first 80 are occupied by a string representing the person’s name, and the last 2 contain an integer of type short, representing the person’s age. If two programs thought the name field was a different length, for example, neither could accurately read a file generated by the other. &lt;/p&gt;&lt;p&gt;Notice, however, that while the person classes in OPERS and IPERS have the same data, they may have different member functions. The first includes the single function getData(), while the second has only showData(). It doesn’t matter what member functions you use, since they are not written to disk along with the object’s data. The data must have the same format, but inconsistencies in the member functions have no effect. However, this is true only in simple classes that don’t use virtual functions. &lt;/p&gt;&lt;p&gt;If you read and write objects of derived classes to a file, you must be more careful. Objects of derived classes include a mysterious number placed just before the object’s data in memory. This number helps identify the object’s class when virtual functions are used. When you write an object to disk, this number is written along with the object’s other data. If you change a class’s member functions, this number changes as well. If you write an object of one class to a file, and then read it back into an object of a class that has identical data but a different member function, you’ll encounter big trouble if you try to use virtual functions on the object. The moral: Make sure a class used to read an object is identical to the class used to write it. &lt;/p&gt;&lt;p&gt;&lt;b&gt;You should also not attempt disk I/O with objects that have pointer data members. As you might expect, the pointer values won’t be correct when the object is read back into a different place in memory.&lt;/b&gt; &lt;/p&gt; &lt;a name="I.2FO_with_Multiple_Objects" id="I.2FO_with_Multiple_Objects"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;I/O with Multiple Objects&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;The OPERS and IPERS programs wrote and read only one object at a time. Our next example opens a file and writes as many objects as the user wants. Then it reads and displays the entire contents of the file. Here’s the listing for DISKFUN: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// diskfun.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// reads and writes several objects to disk&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;fstream&gt; //for file streams&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; person &lt;span class="co1"&gt;//class of persons&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;protected&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;          &lt;span class="kw4"&gt;char&lt;/span&gt; name&lt;span class="br0"&gt;[&lt;/span&gt;80&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//person’s name&lt;/span&gt;&lt;br /&gt;          &lt;span class="kw4"&gt;int&lt;/span&gt; age&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//person’s age&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;       &lt;span class="kw4"&gt;void&lt;/span&gt; getData&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="co1"&gt;//get person’s data&lt;/span&gt;&lt;br /&gt;       &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “\n Enter name&lt;span class="sy4"&gt;:&lt;/span&gt; “&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="kw3"&gt;cin&lt;/span&gt; &lt;span class="sy1"&gt;&gt;&gt;&lt;/span&gt; name&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “ Enter age&lt;span class="sy4"&gt;:&lt;/span&gt; “&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="kw3"&gt;cin&lt;/span&gt; &lt;span class="sy1"&gt;&gt;&gt;&lt;/span&gt; age&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span class="kw4"&gt;void&lt;/span&gt; showData&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="co1"&gt;//display person’s data&lt;/span&gt;&lt;br /&gt;        &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;         &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “\n Name&lt;span class="sy4"&gt;:&lt;/span&gt; “ &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; name&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;         &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “\n Age&lt;span class="sy4"&gt;:&lt;/span&gt; “ &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; age&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;char&lt;/span&gt; ch&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;person pers&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//create person object&lt;/span&gt;&lt;br /&gt;fstream file&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//create input/output file&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//open for append&lt;/span&gt;&lt;br /&gt;file.&lt;span class="me1"&gt;open&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;“GROUP.&lt;span class="me1"&gt;DAT&lt;/span&gt;”, ios&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;app&lt;/span&gt; &lt;span class="sy3"&gt;|&lt;/span&gt; ios&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;out&lt;/span&gt; &lt;span class="sy3"&gt;|&lt;/span&gt;&lt;br /&gt;ios&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;in&lt;/span&gt; &lt;span class="sy3"&gt;|&lt;/span&gt; ios&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;binary&lt;/span&gt; &lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;do&lt;/span&gt; &lt;span class="co1"&gt;//data from user to file&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “\nEnter person’s data&lt;span class="sy4"&gt;:&lt;/span&gt;”&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;pers.&lt;span class="me1"&gt;getData&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//get one person’s data&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//write to file&lt;/span&gt;&lt;br /&gt;file.&lt;span class="me1"&gt;write&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt; &lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;pers&lt;span class="br0"&gt;)&lt;/span&gt;, &lt;span class="kw3"&gt;sizeof&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;pers&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “Enter another person &lt;span class="br0"&gt;(&lt;/span&gt;y&lt;span class="sy2"&gt;/&lt;/span&gt;n&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;?&lt;/span&gt; “&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw3"&gt;cin&lt;/span&gt; &lt;span class="sy1"&gt;&gt;&gt;&lt;/span&gt; ch&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw1"&gt;while&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;ch&lt;span class="sy1"&gt;==&lt;/span&gt;’y’&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//quit on ‘n’&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;file.&lt;span class="me1"&gt;seekg&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;0&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//reset to start of file&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;//read first person&lt;/span&gt;&lt;br /&gt;file.&lt;span class="me1"&gt;read&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt; &lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;pers&lt;span class="br0"&gt;)&lt;/span&gt;, &lt;span class="kw3"&gt;sizeof&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;pers&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;while&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt; &lt;span class="sy3"&gt;!&lt;/span&gt;file.&lt;span class="me1"&gt;eof&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="co1"&gt;//quit on EOF&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “\nPerson&lt;span class="sy4"&gt;:&lt;/span&gt;”&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//display person&lt;/span&gt;&lt;br /&gt;pers.&lt;span class="me1"&gt;showData&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//read another person&lt;/span&gt;&lt;br /&gt;file.&lt;span class="me1"&gt;read&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt; &lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;pers&lt;span class="br0"&gt;)&lt;/span&gt;, &lt;span class="kw3"&gt;sizeof&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;pers&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;Here’s some sample interaction with DISKFUN. The output shown assumes that the program has been run before and that two person objects have already been written to the file. &lt;/p&gt; &lt;pre&gt;Enter person’s data:&lt;br /&gt;Enter name: McKinley&lt;br /&gt;Enter age: 22&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;Enter another person (y/n)? n Person: Name: Whitney Age: 20 &lt;/p&gt;&lt;p&gt;Person: Name: Rainier Age: 21 &lt;/p&gt;&lt;p&gt;Person: Name: McKinley Age: 22 &lt;/p&gt;&lt;p&gt;Here one additional object is added to the file, and the entire contents, consisting of three objects, are then displayed. &lt;/p&gt; &lt;a name="The_fstream_Class" id="The_fstream_Class"&gt;&lt;/a&gt;&lt;h5&gt; &lt;span class="mw-headline"&gt;The fstream Class&lt;/span&gt;&lt;/h5&gt; &lt;p&gt;So far in this chapter the file objects we have created have been for either input or output. In DISKFUN we want to create a file that can be used for both input and output. This requires an object of the fstream class, which is derived from iostream, which is derived from both istream and ostream so it can handle both input and output. &lt;/p&gt; &lt;a name="The_open.28.29_Function" id="The_open.28.29_Function"&gt;&lt;/a&gt;&lt;h5&gt; &lt;span class="mw-headline"&gt;The open() Function&lt;/span&gt;&lt;/h5&gt; &lt;p&gt;In previous examples we created a file object and initialized it in the same statement: &lt;/p&gt;&lt;p&gt;ofstream outfile(“TEST.TXT”); &lt;/p&gt;&lt;p&gt;In DISKFUN we use a different approach: We create the file in one statement and open it in another, using the open() function, which is a member of the fstream class. This is a useful approach in situations where the open may fail. You can create a stream object once, and then try repeatedly to open it, without the overhead of creating a new stream object each time. &lt;/p&gt; &lt;a name="The_Mode_Bits" id="The_Mode_Bits"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;The Mode Bits&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;We’ve seen the mode bit ios::binary before. In the open() function we include several new mode bits. The mode bits, defined in ios, specify various aspects of how a stream object will be opened. &lt;/p&gt;&lt;p&gt;Mode bit for open function  are. &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;b&gt;in&lt;/b&gt;  Open for reading (default for ifstream) &lt;/li&gt;&lt;li&gt;&lt;b&gt;out&lt;/b&gt; Open for writing (default for ofstream) &lt;/li&gt;&lt;li&gt;&lt;b&gt;ate&lt;/b&gt; Start reading or writing at end of file (AT End) &lt;/li&gt;&lt;li&gt;&lt;b&gt;app&lt;/b&gt; Start writing at end of file (APPend) &lt;/li&gt;&lt;li&gt;&lt;b&gt;trunc&lt;/b&gt; Truncate file to zero length if it exists (TRUNCate) &lt;/li&gt;&lt;li&gt;&lt;b&gt;nocreate&lt;/b&gt; Error when opening if file does not already exist &lt;/li&gt;&lt;li&gt;&lt;b&gt;noreplace&lt;/b&gt; Error when opening for output if file already exists, unless ate or app is set &lt;/li&gt;&lt;li&gt;&lt;b&gt;binary&lt;/b&gt; Open file in binary (not text) mode &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;In DISKFUN we use ios::app because we want to preserve whatever was in the file before. That is, we can write to the file, terminate the program, and start up the program again, and whatever we write to the file will be added following the existing contents. We use ios:in and ios:out because we want to perform both input and output on the file, and we use ios:binary because we’re writing binary objects. The vertical bars between the flags cause the bits representing these flags to be logically combined into a single integer, so that several flags can apply simultaneously. &lt;/p&gt;&lt;p&gt;We write one person object at a time to the file, using the write() function. When we’ve finished writing, we want to read the entire file. Before doing this we must reset the file’s current position. We do this with the seekg() function, which we’ll examine in the next section. It ensures we’ll start reading at the beginning of the file. Then, in a while loop, we repeatedly read a person object from the file and display it on the screen. This continues until we’ve read all the person objects—a state that we discover using the eof() function, which returns the state of the ios::eofbit. &lt;/p&gt; &lt;a name="File_Pointers" id="File_Pointers"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;File Pointers&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;Each file object has associated with it two integer values called the get pointer and the put pointer. These are also called the current get position and the current put position, or—if it’s clear which one is meant—simply the current position. These values specify the byte number in the file where writing or reading will take place. (The term pointer in this context should not be confused with normal C++ pointers used as address variables.) &lt;/p&gt;&lt;p&gt;Often you want to start reading an existing file at the beginning and continue until the end. When writing, you may want to start at the beginning, deleting any existing contents, or at the end, in which case you can open the file with the ios::app mode specifier. These are the default actions, so no manipulation of the file pointers is necessary. However, there are times when you must take control of the file pointers yourself so that you can read from and write to an arbitrary location in the file. The &lt;b&gt;seekg()&lt;/b&gt; and &lt;b&gt;tellg()&lt;/b&gt; functions allow you to set and examine the get pointer, and the &lt;b&gt;seekp()&lt;/b&gt; and &lt;b&gt;tellp()&lt;/b&gt; functions perform these same actions on the put pointer. &lt;/p&gt; &lt;a name="Resources" id="Resources"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Resources &lt;/span&gt;&lt;/h2&gt; &lt;p&gt;PPT: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://wiki.msitprogram.net/CPP09/W2/M1/cpphtp4_PPT_14.ppt" class="external text" title="http://wiki.msitprogram.net/CPP09/W2/M1/cpphtp4_PPT_14.ppt" rel="nofollow"&gt;Chapter 14- File Processing&lt;/a&gt; &lt;b&gt;C++ How to program by Dietle and Dietle&lt;/b&gt; &lt;ul&gt;&lt;li&gt; 14.1   Introduction &lt;/li&gt;&lt;li&gt; 14.2   The Data Hierarchy &lt;/li&gt;&lt;li&gt; 14.3   Files and Streams &lt;/li&gt;&lt;li&gt; 14.4   Creating a Sequential-Access File &lt;/li&gt;&lt;li&gt; 14.5   Reading Data from a Sequential-Access File &lt;/li&gt;&lt;li&gt; 14.6   Updating Sequential-Access Files &lt;/li&gt;&lt;li&gt; 14.7   Random-Access Files &lt;/li&gt;&lt;li&gt; 14.8   Creating a Random-Access File &lt;/li&gt;&lt;li&gt; 14.9   Writing Data Randomly to a Random-Access File &lt;/li&gt;&lt;li&gt; 14.10   Reading Data Sequentially from a Random-Access File &lt;/li&gt;&lt;li&gt; 14.11   Example: A Transaction-Processing Program &lt;/li&gt;&lt;li&gt; 14.12   Input/Output of Objects &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Web Resources: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.arachnoid.com/cpptutor/student3.html" class="external free" title="http://www.arachnoid.com/cpptutor/student3.html" rel="nofollow"&gt;http://www.arachnoid.com/cpptutor/student3.html&lt;/a&gt; ---&gt; Output Formatting] &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/doc/tutorial/files/" class="external free" title="http://www.cplusplus.com/doc/tutorial/files/" rel="nofollow"&gt;http://www.cplusplus.com/doc/tutorial/files/&lt;/a&gt; ---&gt; Input/Output with files &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;I/O Stream Library: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/reference/iostream/ios_base/" class="external free" title="http://www.cplusplus.com/reference/iostream/ios_base/" rel="nofollow"&gt;http://www.cplusplus.com/reference/iostream/ios_base/&lt;/a&gt; ---&gt; ios_base &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/reference/iostream/ios/" class="external free" title="http://www.cplusplus.com/reference/iostream/ios/" rel="nofollow"&gt;http://www.cplusplus.com/reference/iostream/ios/&lt;/a&gt;  ---&gt; ios &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/reference/iostream/" class="external free" title="http://www.cplusplus.com/reference/iostream/" rel="nofollow"&gt;http://www.cplusplus.com/reference/iostream/&lt;/a&gt; ---&gt; IO Stream Library &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/reference/iostream/fstream/" class="external free" title="http://www.cplusplus.com/reference/iostream/fstream/" rel="nofollow"&gt;http://www.cplusplus.com/reference/iostream/fstream/&lt;/a&gt; ---&gt; fstream &lt;/li&gt;&lt;/ul&gt; &lt;a name="Steps_to_solve_the_problem_sets" id="Steps_to_solve_the_problem_sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Steps to solve the problem sets&lt;/span&gt;&lt;/h2&gt; &lt;ul&gt;&lt;li&gt; Step 1: Read about  Streams and Files given &lt;/li&gt;&lt;li&gt; Step 2: Try sample programs given (You can copy paste the programs and try your self) &lt;/li&gt;&lt;li&gt; Step 3: Go through the PPT on file processing.(you can copy paste the program and try your self) &lt;/li&gt;&lt;li&gt; Step 4: Go through the additional resources given. &lt;/li&gt;&lt;li&gt; Step 5: Attack the problem sets in the given order(Problem Set A and then Problem Set B ...). &lt;/li&gt;&lt;li&gt; Step 6: If you are not clear with the problem sets then contact your respective mentor for clarification. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Spend atleast 2 - 3 Hours from Step 1 to Step 6.  &lt;/p&gt;&lt;p&gt;Try the programs given in the PPT for Random access files. &lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt; &lt;a name="Problem_Sets" id="Problem_Sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Problem Sets&lt;/span&gt;&lt;/h2&gt; &lt;a name="Problem_Set_A" id="Problem_Set_A"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set A&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt; Write a C program that uses a binary file &lt;b&gt;&lt;a href="http://wiki.msitprogram.net/CPP09/W2/M1/cricket.binary" class="external text" title="http://wiki.msitprogram.net/CPP09/W2/M1/cricket.binary" rel="nofollow"&gt;cricket.binary&lt;/a&gt;&lt;/b&gt;. The binary file is having the information of Indian cricket player statistics. &lt;/p&gt;&lt;p&gt;The binary file is divided into 2 parts.  &lt;/p&gt; &lt;ol&gt;&lt;li&gt;1st part(Header part) is the header, having information of number of records in the file. &lt;/li&gt;&lt;li&gt;2nd part(Data part) contains the data of the players. &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;A record in binary file is the information of one player. &lt;/p&gt;&lt;p&gt;The layout of the file is  &lt;/p&gt; &lt;ol&gt;&lt;li&gt;Header Part is of 4 Bytes( Size of Integer) -- Number of records(N). &lt;/li&gt;&lt;li&gt;Data Part is of &lt;b&gt;N * Size of Player&lt;/b&gt; Bytes -- N Records. &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;The structure of the player is &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="c source-c"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;//Player Class&lt;/span&gt;&lt;br /&gt;class Player&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;Private&lt;span class="sy0"&gt;:&lt;/span&gt;&lt;br /&gt;    &lt;span class="kw4"&gt;int&lt;/span&gt; id&lt;span class="sy0"&gt;;&lt;/span&gt;       &lt;span class="co1"&gt;//#ID -- Unique ID given to each player and starts from 1.&lt;/span&gt;&lt;br /&gt;    &lt;span class="kw4"&gt;char&lt;/span&gt; name&lt;span class="br0"&gt;[&lt;/span&gt;50&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;span class="co1"&gt;//#Name -- Name of the Player&lt;/span&gt;&lt;br /&gt;    &lt;span class="kw4"&gt;int&lt;/span&gt; career&lt;span class="sy0"&gt;;&lt;/span&gt;   &lt;span class="co1"&gt;//#Career -- Career Started year&lt;/span&gt;&lt;br /&gt;    &lt;span class="kw4"&gt;int&lt;/span&gt; matches&lt;span class="sy0"&gt;;&lt;/span&gt;  &lt;span class="co1"&gt;//#Matches -- Number of Matches Played&lt;/span&gt;&lt;br /&gt;    &lt;span class="kw4"&gt;int&lt;/span&gt; runs&lt;span class="sy0"&gt;;&lt;/span&gt;     &lt;span class="co1"&gt;//#Runs -- Total runs scored&lt;/span&gt;&lt;br /&gt;    &lt;span class="kw4"&gt;int&lt;/span&gt; highest&lt;span class="sy0"&gt;;&lt;/span&gt;  &lt;span class="co1"&gt;//#Highest -- Highest score of the player&lt;/span&gt;&lt;br /&gt;    &lt;span class="kw4"&gt;int&lt;/span&gt; r50&lt;span class="sy0"&gt;;&lt;/span&gt;      &lt;span class="co1"&gt;//#50's -- Number of half centuries of the player.&lt;/span&gt;&lt;br /&gt;    &lt;span class="kw4"&gt;int&lt;/span&gt; r100&lt;span class="sy0"&gt;;&lt;/span&gt;     &lt;span class="co1"&gt;//#100's -- Number of Centuries of the player.&lt;/span&gt;&lt;br /&gt;    &lt;span class="kw4"&gt;float&lt;/span&gt; average&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;span class="co1"&gt;//#Average -- Average score of the player. (Total Runs/Number of Matches played)&lt;/span&gt;&lt;br /&gt;Public&lt;span class="sy0"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//Declare the Implementation specific functions here and define them.&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;The Program should display the Menu with the following five choices &lt;/p&gt; &lt;ol&gt;&lt;li&gt;Print all player details &lt;ol&gt;&lt;li&gt;Display the details of all the players in a tabular format &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;li&gt;Print Individual Player details &lt;ol&gt;&lt;li&gt;Display the Individual player details based on the &lt;b&gt;ID&lt;/b&gt; enter by the user. &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;li&gt;Update a Player Record for the Match. &lt;ol&gt;&lt;li&gt;User will enter the ID of the Player to be updated &lt;/li&gt;&lt;li&gt;IF Player is not found - print Player not found and redisplay the MENU &lt;/li&gt;&lt;li&gt;Display the Player details. &lt;/li&gt;&lt;li&gt;Ask for the runs scored for the recent match played by the Player. &lt;/li&gt;&lt;li&gt;Update the record of the entered player in the same file with the following details. &lt;ol&gt;&lt;li&gt;Matches Played will be increment by one &lt;/li&gt;&lt;li&gt;Runs - runs + runs scored &lt;/li&gt;&lt;li&gt;highest - Check highest score with runs scored and update whichever is higher. &lt;/li&gt;&lt;li&gt;r50 -- if runs scored is a half century then increment r50 by 1 &lt;/li&gt;&lt;li&gt;r100 -- if runs scored is a century then increment r100 by 1 &lt;/li&gt;&lt;li&gt;average -- Calculate the new average with the runs scored. &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;li&gt;Add a new player &lt;ol&gt;&lt;li&gt;Generate a new ID for the player (ID will be the next highest ID of all the records) &lt;/li&gt;&lt;li&gt;Ask for all the details of the player from the user except ID. &lt;/li&gt;&lt;li&gt;Update the number of players in the file. &lt;/li&gt;&lt;li&gt;Append the new player details at the last of the file. &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;li&gt;Quit &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Use a loop for the Menu choice and perform the operation selected by the user. The program should quit only when the user selects quit option in the menu. &lt;/p&gt;&lt;p&gt;The program should open the binary file in &lt;b&gt;Random Access Mode&lt;/b&gt; for reading and writing. Use seekg() to move the cursor position in the file. &lt;/p&gt;&lt;p&gt;You are not supposed to use &lt;b&gt;Array of Players&lt;/b&gt; or &lt;b&gt;Dynamic memory allocation&lt;/b&gt;. &lt;/p&gt;&lt;p&gt;You can use ID to seek in the file for the location of the record. (4Bytes+(ID*size of player)from start of file)   &lt;/p&gt;&lt;p&gt;The data file should be passed as &lt;b&gt;command line argument.&lt;/b&gt; &lt;/p&gt;&lt;p&gt;A sample program &lt;a href="http://wiki.msitprogram.net/CPP09/W2/M1/cricket_read.cpp" class="external text" title="http://wiki.msitprogram.net/CPP09/W2/M1/cricket_read.cpp" rel="nofollow"&gt;cricket_read.cpp&lt;/a&gt; is given.  &lt;/p&gt; &lt;ol&gt;&lt;li&gt;Compile the cricket_read.cpp &lt;/li&gt;&lt;li&gt;Execute the program as $&gt; cricket_read.exe in the directory where you saved the binary file. &lt;ol&gt;&lt;li&gt;Prints all the player details &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-6727042311151925736?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/w5cw4z6z4UREdR3Be7qERHgUAL0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/w5cw4z6z4UREdR3Be7qERHgUAL0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/w5cw4z6z4UREdR3Be7qERHgUAL0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/w5cw4z6z4UREdR3Be7qERHgUAL0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/bzCBoXUMZiE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/6727042311151925736/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/streams-files.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/6727042311151925736?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/6727042311151925736?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/bzCBoXUMZiE/streams-files.html" title="Streams &amp; Files" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/streams-files.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0cHQXgyeSp7ImA9WxNWFEg.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-6357550690312974792</id><published>2009-10-13T09:13:00.000-07:00</published><updated>2009-10-13T09:23:50.691-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-13T09:23:50.691-07:00</app:edited><title>Excveptiona Handling</title><content type="html">CPP09 - W1 - M10 - Exception Handling&lt;br /&gt;Learning Objectives&lt;br /&gt;&lt;br /&gt;At the end of this module, you will be able to&lt;br /&gt;&lt;br /&gt;   * Explain why do we need exception handling.&lt;br /&gt;   * Explain how to write C++ programs using exception handling.&lt;br /&gt;   * Explain how to declare and implement exception handing.&lt;br /&gt;   * Identify and list different exceptions.&lt;br /&gt;   * Apply exceptions for file handling, memory allocation and real world scenarios.&lt;br /&gt;&lt;br /&gt;Exception Handling&lt;br /&gt;Introduction&lt;br /&gt;&lt;br /&gt;Exceptions are errors that occur at runtime. They are caused by a wide variety of exceptional circumstance, such as running out of memory, not being able to open a file, trying to initialize an object to an impossible value, or using an out-of-bounds index to a vector.&lt;br /&gt;Why do we need exceptions ?&lt;br /&gt;&lt;br /&gt;Why do we need a new mechanism to handle errors? Let’s look at how the process was handled in the past. C-language programs often signal an error by returning a particular value from the function in which it occurred. For example, disk-file functions often return NULL or 0 to signal an error. Each time you call one of these functions you check the return value:&lt;br /&gt;&lt;br /&gt;if( somefunc() == ERROR_RETURN_VALUE )&lt;br /&gt;//handle the error or call error-handler function&lt;br /&gt;else&lt;br /&gt;//proceed normally&lt;br /&gt;if( anotherfunc() == NULL )&lt;br /&gt;//handle the error or call error-handler function&lt;br /&gt;else&lt;br /&gt;//proceed normally&lt;br /&gt;if( thirdfunc() == 0 )&lt;br /&gt;//handle the error or call error-handler function&lt;br /&gt;else&lt;br /&gt;//proceed normally&lt;br /&gt;&lt;br /&gt;The Problem with this approach is that every single call to such a function must be examined by the program. Surrounding each function call with an if...else statement, and adding statements to handle the error (or call an error-handler routine), requires a lot of code and makes the listing convoluted and hard to read.&lt;br /&gt;Exception Syntax&lt;br /&gt;&lt;br /&gt;Imagine an application that creates and interacts with objects of a certain class. Ordinarily the application’s calls to the class member functions cause no problems. Sometimes, however, the application makes a mistake, causing an error to be detected in a member function. This member function then informs the application that an error has occurred. When exceptions are used, this is called throwing an exception. In the application we install a separate section of code to handle the error. This code is called an exception handler or catch block; it catches the exceptions thrown by the member function. Any code in the application that uses objects of the class is enclosed in a try block. Errors generated in the try block will be caught in the catch block. Code that doesn’t interact with the class need not be in a try block. Below image shows the arrangement.&lt;br /&gt;&lt;br /&gt;exceptions.jpg&lt;br /&gt;&lt;br /&gt;The exception mechanism uses three new C++ keywords: throw, catch, and try. Also, we need to create a new kind of entity called an exception class. XSYNTAX is not a working program, but a skeleton program to show the syntax.&lt;br /&gt;&lt;br /&gt;  0.&lt;br /&gt;&lt;br /&gt;     // xsyntax.cpp&lt;br /&gt;&lt;br /&gt;  1.&lt;br /&gt;&lt;br /&gt;     // not a working program&lt;br /&gt;&lt;br /&gt;  2.&lt;br /&gt;&lt;br /&gt;     ////////////////////////////////////////////////////////////////&lt;br /&gt;&lt;br /&gt;  3.&lt;br /&gt;&lt;br /&gt;     class AClass //a class&lt;br /&gt;&lt;br /&gt;  4.&lt;br /&gt;&lt;br /&gt;     {&lt;br /&gt;&lt;br /&gt;  5.&lt;br /&gt;&lt;br /&gt;     public:&lt;br /&gt;&lt;br /&gt;  6.&lt;br /&gt;&lt;br /&gt;      class AnError //exception class&lt;br /&gt;&lt;br /&gt;  7.&lt;br /&gt;&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt;  8.&lt;br /&gt;&lt;br /&gt;      };&lt;br /&gt;&lt;br /&gt;  9.&lt;br /&gt;&lt;br /&gt;     &lt;br /&gt;&lt;br /&gt; 10.&lt;br /&gt;&lt;br /&gt;      void Func() //a member function&lt;br /&gt;&lt;br /&gt; 11.&lt;br /&gt;&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt; 12.&lt;br /&gt;&lt;br /&gt;       if( /* error condition */ )&lt;br /&gt;&lt;br /&gt; 13.&lt;br /&gt;&lt;br /&gt;       throw AnError(); //throw exception&lt;br /&gt;&lt;br /&gt; 14.&lt;br /&gt;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt; 15.&lt;br /&gt;&lt;br /&gt;     };&lt;br /&gt;&lt;br /&gt; 16.&lt;br /&gt;&lt;br /&gt;     &lt;br /&gt;&lt;br /&gt; 17.&lt;br /&gt;&lt;br /&gt;     ////////////////////////////////////////////////////////////////&lt;br /&gt;&lt;br /&gt; 18.&lt;br /&gt;&lt;br /&gt;     &lt;br /&gt;&lt;br /&gt; 19.&lt;br /&gt;&lt;br /&gt;     int main() //application&lt;br /&gt;&lt;br /&gt; 20.&lt;br /&gt;&lt;br /&gt;     {&lt;br /&gt;&lt;br /&gt; 21.&lt;br /&gt;&lt;br /&gt;      try //try block&lt;br /&gt;&lt;br /&gt; 22.&lt;br /&gt;&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt; 23.&lt;br /&gt;&lt;br /&gt;       AClass obj1; //interact with AClass objects&lt;br /&gt;&lt;br /&gt; 24.&lt;br /&gt;&lt;br /&gt;       obj1.Func(); //may cause error&lt;br /&gt;&lt;br /&gt; 25.&lt;br /&gt;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt; 26.&lt;br /&gt;&lt;br /&gt;      catch(AClass::AnError)  //exception handler&lt;br /&gt;&lt;br /&gt; 27.&lt;br /&gt;&lt;br /&gt;      {    //(catch block)&lt;br /&gt;&lt;br /&gt; 28.&lt;br /&gt;&lt;br /&gt;       //tell user about error, etc.&lt;br /&gt;&lt;br /&gt; 29.&lt;br /&gt;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt; 30.&lt;br /&gt;&lt;br /&gt;     return 0;&lt;br /&gt;&lt;br /&gt; 31.&lt;br /&gt;&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;We start with a class called AClass, which represents any class in which errors might occur. An exception class, AnError, is specified in the public part of AClass. In AClass’s member functions we check for errors. If we find one, we throw an exception, using the keyword throw followed by the constructor for the error class:&lt;br /&gt;&lt;br /&gt;throw AnError(); //’throw’ followed by constructor for AnError class&lt;br /&gt;&lt;br /&gt;In the main() part of the program we enclose any statements that interact with AClass in a try block. If any of these statements causes an error to be detected in an AClass member function, an exception will be thrown and control will go to the catch block that immediately follows the try block.&lt;br /&gt;A simple Exception Example&lt;br /&gt;&lt;br /&gt;Let’s look at a working program example that uses exceptions. This example creates a stack data structure in which integer data values could be stored. The application program might attempt to push too many objects onto the stack, thus exceeding the capacity of the array, or it might try to pop too many objects off the stack, thus obtaining invalid data. In the XSTAK program we use an exception to handle these two errors.&lt;br /&gt;&lt;br /&gt;  0.&lt;br /&gt;&lt;br /&gt;     // xstak.cpp&lt;br /&gt;&lt;br /&gt;  1.&lt;br /&gt;&lt;br /&gt;     // demonstrates exceptions&lt;br /&gt;&lt;br /&gt;  2.&lt;br /&gt;&lt;br /&gt;     #include &lt;iostream&gt;&lt;br /&gt;&lt;br /&gt;  3.&lt;br /&gt;&lt;br /&gt;     using namespace std;&lt;br /&gt;&lt;br /&gt;  4.&lt;br /&gt;&lt;br /&gt;     const int MAX = 3; //stack holds 3 integers&lt;br /&gt;&lt;br /&gt;  5.&lt;br /&gt;&lt;br /&gt;     ////////////////////////////////////////////////////////////////&lt;br /&gt;&lt;br /&gt;  6.&lt;br /&gt;&lt;br /&gt;     class Stack&lt;br /&gt;&lt;br /&gt;  7.&lt;br /&gt;&lt;br /&gt;     {&lt;br /&gt;&lt;br /&gt;  8.&lt;br /&gt;&lt;br /&gt;     private:&lt;br /&gt;&lt;br /&gt;  9.&lt;br /&gt;&lt;br /&gt;      int st[MAX]; //array of integers&lt;br /&gt;&lt;br /&gt; 10.&lt;br /&gt;&lt;br /&gt;     &lt;br /&gt;&lt;br /&gt; 11.&lt;br /&gt;&lt;br /&gt;      int top; //index of top of stack&lt;br /&gt;&lt;br /&gt; 12.&lt;br /&gt;&lt;br /&gt;     public:&lt;br /&gt;&lt;br /&gt; 13.&lt;br /&gt;&lt;br /&gt;      class Range //exception class for Stack&lt;br /&gt;&lt;br /&gt; 14.&lt;br /&gt;&lt;br /&gt;      { //note: empty class body&lt;br /&gt;&lt;br /&gt; 15.&lt;br /&gt;&lt;br /&gt;      };&lt;br /&gt;&lt;br /&gt; 16.&lt;br /&gt;&lt;br /&gt;     &lt;br /&gt;&lt;br /&gt; 17.&lt;br /&gt;&lt;br /&gt;      Stack() //constructor&lt;br /&gt;&lt;br /&gt; 18.&lt;br /&gt;&lt;br /&gt;      { top = -1; }&lt;br /&gt;&lt;br /&gt; 19.&lt;br /&gt;&lt;br /&gt;     &lt;br /&gt;&lt;br /&gt; 20.&lt;br /&gt;&lt;br /&gt;      void push(int var)&lt;br /&gt;&lt;br /&gt; 21.&lt;br /&gt;&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt; 22.&lt;br /&gt;&lt;br /&gt;       if(top &gt;= MAX-1) //if stack full,&lt;br /&gt;&lt;br /&gt; 23.&lt;br /&gt;&lt;br /&gt;       throw Range(); //throw exception&lt;br /&gt;&lt;br /&gt; 24.&lt;br /&gt;&lt;br /&gt;       st[++top] = var; //put number on stack&lt;br /&gt;&lt;br /&gt; 25.&lt;br /&gt;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt; 26.&lt;br /&gt;&lt;br /&gt;     &lt;br /&gt;&lt;br /&gt; 27.&lt;br /&gt;&lt;br /&gt;      int pop()&lt;br /&gt;&lt;br /&gt; 28.&lt;br /&gt;&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt; 29.&lt;br /&gt;&lt;br /&gt;       if(top &lt;&gt;&lt;br /&gt;&lt;br /&gt;  3.&lt;br /&gt;&lt;br /&gt;     using namespace std;&lt;br /&gt;&lt;br /&gt;  4.&lt;br /&gt;&lt;br /&gt;     const int MAX = 3; //stack holds 3 integers&lt;br /&gt;&lt;br /&gt;  5.&lt;br /&gt;&lt;br /&gt;     ////////////////////////////////////////////////////////////////&lt;br /&gt;&lt;br /&gt;  6.&lt;br /&gt;&lt;br /&gt;     class Stack&lt;br /&gt;&lt;br /&gt;  7.&lt;br /&gt;&lt;br /&gt;     {&lt;br /&gt;&lt;br /&gt;  8.&lt;br /&gt;&lt;br /&gt;     private:&lt;br /&gt;&lt;br /&gt;  9.&lt;br /&gt;&lt;br /&gt;      int st[MAX]; //stack: array of integers&lt;br /&gt;&lt;br /&gt; 10.&lt;br /&gt;&lt;br /&gt;      int top; //index of top of stack&lt;br /&gt;&lt;br /&gt; 11.&lt;br /&gt;&lt;br /&gt;     public:&lt;br /&gt;&lt;br /&gt; 12.&lt;br /&gt;&lt;br /&gt;      class Full { }; //exception class&lt;br /&gt;&lt;br /&gt; 13.&lt;br /&gt;&lt;br /&gt;     &lt;br /&gt;&lt;br /&gt; 14.&lt;br /&gt;&lt;br /&gt;      class Empty { }; //exception class&lt;br /&gt;&lt;br /&gt; 15.&lt;br /&gt;&lt;br /&gt;     &lt;br /&gt;&lt;br /&gt; 16.&lt;br /&gt;&lt;br /&gt;     //--------------------------------------------------------------&lt;br /&gt;&lt;br /&gt; 17.&lt;br /&gt;&lt;br /&gt;      Stack() //constructor&lt;br /&gt;&lt;br /&gt; 18.&lt;br /&gt;&lt;br /&gt;      { top = -1; }&lt;br /&gt;&lt;br /&gt; 19.&lt;br /&gt;&lt;br /&gt;     //--------------------------------------------------------------&lt;br /&gt;&lt;br /&gt; 20.&lt;br /&gt;&lt;br /&gt;      void push(int var) //put number on stack&lt;br /&gt;&lt;br /&gt; 21.&lt;br /&gt;&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt; 22.&lt;br /&gt;&lt;br /&gt;       if(top &gt;= MAX-1) //if stack full,&lt;br /&gt;&lt;br /&gt; 23.&lt;br /&gt;&lt;br /&gt;        throw Full(); //throw Full exception&lt;br /&gt;&lt;br /&gt; 24.&lt;br /&gt;&lt;br /&gt;       st[++top] = var;&lt;br /&gt;&lt;br /&gt; 25.&lt;br /&gt;&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt; 26.&lt;br /&gt;&lt;br /&gt;     &lt;br /&gt;&lt;br /&gt; 27.&lt;br /&gt;&lt;br /&gt;     //--------------------------------------------------------------&lt;br /&gt;&lt;br /&gt; 28.&lt;br /&gt;&lt;br /&gt;      int pop() //take number off stack&lt;br /&gt;&lt;br /&gt; 29.&lt;br /&gt;&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt; 30.&lt;br /&gt;&lt;br /&gt;       if(top &lt; 0) //if stack empty,&lt;br /&gt;  31.           throw Empty(); //throw Empty exception   &lt;br /&gt;32.         return st[top--];   &lt;br /&gt;33.         }&lt;br /&gt; 34.        };   &lt;br /&gt;35.        ////////////////////////////////////////////////////////////////   &lt;br /&gt;36.        int main()   &lt;br /&gt;37.        {   &lt;br /&gt;38.         Stack s1;&lt;br /&gt; 39.         try   &lt;br /&gt;40.         {   &lt;br /&gt;41.          s1.push(11);   &lt;br /&gt;42.          s1.push(22);   &lt;br /&gt;43.          s1.push(33);   &lt;br /&gt;44.          // s1.push(44); //oops: stack full&lt;br /&gt;  45.          cout &lt;&lt; “1: “ &lt;&lt; s1.pop() &lt;&lt; endl;   &lt;br /&gt;46.          cout &lt;&lt; “2: “ &lt;&lt; s1.pop() &lt;&lt; endl;   &lt;br /&gt;47.          cout &lt;&lt; “3: “ &lt;&lt; s1.pop() &lt;&lt; endl;   &lt;br /&gt;48.          cout &lt;&lt; “4: “ &lt;&lt; s1.pop() &lt;&lt; endl; //oops: stack empty   &lt;br /&gt;49.         }&lt;br /&gt; 50.         catch(Stack::Full)   &lt;br /&gt;51.         {   &lt;br /&gt;52.          cout &lt;&lt; “Exception: Stack Full” &lt;&lt; endl;&lt;br /&gt; 53.         }   &lt;br /&gt;54.         catch(Stack::Empty)   &lt;br /&gt;55.         {&lt;br /&gt; 56.          cout &lt;&lt; “Exception: Stack Empty” &lt;&lt; endl;   &lt;br /&gt;57.         }&lt;br /&gt; 58.         return 0;   &lt;br /&gt;59.        } &lt;br /&gt;&lt;br /&gt;In XSTAK2 we specify two exception classes:  class Full { }; class Empty { }; The statement throw Full();  is executed if the application calls push() when the stack is already full, and  throw Empty();  is executed if pop() is called when the stack is empty.  A separate catch block is used for each exception:  try { //code that operates on Stack objects } catch(Stack::Full) { //code to handle Full exception } catch(Stack::Empty) { //code to handle Empty exception }  All the catch blocks used with a particular try block must immediately follow the try block. In this case each catch block simply prints a message: “Stack Full” or “Stack Empty”. Only one catch block is activated for a given exception. A group of catch blocks, or a catch ladder, operates a little like a switch statement, with only the appropriate section of code being executed. When an exception has been handled, control passes to the statement following all the catch blocks. (Unlike a switch statement, you don’t need to end each catch block with a break. In this way catch blocks act more like functions.) Specifing Data in an Exception Class  What happens if the application needs more information about what caused an exception? For instance, in the XSTAK2 example, it might help the programmer to know what MAX elements the stack can have.  It’s convenient to make the data in an exception class public so it can be accessed directly by the exception handler. Here’s the specification for the new Full exception class in XSTAK2:  class Full{ //exception class  public:   int maxElements; //Max elements the stack can store   string message; //For name of the routine     Full(int max, string m){    maxElements = max; //put max in value in the object    message = m; //put string in object   } };  There are public variables for a string object, which will hold the name of the member function being called, and a type int, for the MAX stack elements.   Initializing an Exception Object  How do we initialize the data when we throw an exception? In the two-argument constructor for the Stack class we say  throw Full(MAX,"STACK PUSH --&gt; Stack is FULL"");&lt;br /&gt;&lt;br /&gt;When the exception is thrown, the handler will display the string and MAX. The string will tell us which member function is throwing the exception, and the value of MAX. This additional data will make it easier for the programmer or user to figure out what caused the error.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Resources&lt;br /&gt;&lt;br /&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-3b43bf4a2d646b56" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;
&lt;param name="bgcolor" value="#FFFFFF"&gt;
&lt;param name="allowfullscreen" value="true"&gt;
&lt;param name="flashvars" value="flvurl=http://v12.nonxt1.googlevideo.com/videoplayback?id%3D3b43bf4a2d646b56%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D5BC4E32924E1D67E96D44D2EB30B7BF4AE32D947.1C615E8FA75207C89A60D8C8406257C1D553A3D3%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D3b43bf4a2d646b56%26offsetms%3D5000%26itag%3Dw160%26sigh%3D-zGbtgObdjGrzrwxu8ifvVjNvYA&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;
&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"
width="320" height="266" bgcolor="#FFFFFF"
flashvars="flvurl=http://v12.nonxt1.googlevideo.com/videoplayback?id%3D3b43bf4a2d646b56%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D5BC4E32924E1D67E96D44D2EB30B7BF4AE32D947.1C615E8FA75207C89A60D8C8406257C1D553A3D3%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D3b43bf4a2d646b56%26offsetms%3D5000%26itag%3Dw160%26sigh%3D-zGbtgObdjGrzrwxu8ifvVjNvYA&amp;autoplay=0&amp;ps=blogger"
allowFullScreen="true" /&gt;&lt;/object&gt;
&lt;br /&gt;&lt;br /&gt;&lt;/iostream&gt;&lt;/iostream&gt;&lt;p&gt;Web Resources: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/doc/tutorial/exceptions.html" class="external free" title="http://www.cplusplus.com/doc/tutorial/exceptions.html" rel="nofollow"&gt;http://www.cplusplus.com/doc/tutorial/exceptions.html&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cprogramming.com/tutorial/exceptions.html" class="external free" title="http://www.cprogramming.com/tutorial/exceptions.html" rel="nofollow"&gt;http://www.cprogramming.com/tutorial/exceptions.html&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt; &lt;a name="Steps_to_Attack_the_problem_sets" id="Steps_to_Attack_the_problem_sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Steps to Attack the problem sets&lt;/span&gt;&lt;/h2&gt; &lt;ul&gt;&lt;li&gt;Step 1: Read about Exception and try the example programs &lt;/li&gt;&lt;li&gt;Step 2: Go through the PPT on Exceptions and try stack program from the PPT.(you can copy paste the program) &lt;/li&gt;&lt;li&gt;Step 3: Go through the additional resources given.  &lt;/li&gt;&lt;li&gt;Step 4: Attack the problem sets in the given order(Problem Set A and then Problem Set B ...). &lt;/li&gt;&lt;li&gt;Step 5: If you are not clear with the problem sets then contact your respective mentor for clarification. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Spend atleast 2 hours from step 1 to step 3 &lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt; &lt;a name="Problem_Sets" id="Problem_Sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Problem Sets&lt;/span&gt;&lt;/h2&gt; &lt;a name="Problem_Set_A" id="Problem_Set_A"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set A&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt; Write an interactive program which divides two Integer Wrapper(Declaration is given) Class Objects. Overload divide(/) operator. Handle cases such as division-by-zero using exceptions. Write a test program to test the Wrapper Integer class for division. &lt;/p&gt;&lt;p&gt;The declaration of Integer class is &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Integer&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span class="kw2"&gt;private&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw4"&gt;int&lt;/span&gt; value&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;  Integer&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; v&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;value&lt;span class="sy1"&gt;=&lt;/span&gt;v&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;             &lt;span class="co1"&gt;//Consturctor&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw4"&gt;int&lt;/span&gt; getInt&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;                       &lt;span class="co1"&gt;//returns the integer&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw4"&gt;void&lt;/span&gt; setInt&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; v&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;               &lt;span class="co1"&gt;//set the int value to the object&lt;/span&gt;&lt;br /&gt;  Integer operator&lt;span class="sy2"&gt;/&lt;/span&gt;&lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;const&lt;/span&gt; Integer &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;  &lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;pre&gt;Name the program as: PA1_Integer.cpp&lt;br /&gt;&lt;/pre&gt; &lt;a name="Problem_Set_B" id="Problem_Set_B"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set B&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;Add exceptions to the queue template you have written in Module 9. Throw two exceptions: one if the capacity of the queue is exceeded, the other if the program tries to remove an item from an empty queue. One way to handle this is to add a new data member to the queue: a count of the number of items currently in the queue. Increment the count when you insert an item, and decrement it when you remove an item. Throw an exception if this count exceeds the capacity of the queue, or if it becomes less than 0. &lt;/p&gt;&lt;p&gt;Add Exceptions in the Dynamic Array if the Memory allocation failed. &lt;/p&gt;&lt;p&gt;You might try making the main() part of this exercise interactive, so the user can put values on a queue and take them off. This makes it easier to exercise the queue. Following an exception, the program should allow the user to recover from a mistake without corrupting the contents of the queue. &lt;/p&gt; &lt;pre&gt;Name the DynamicArray program as: PB1_DynamicArray.cpp&lt;br /&gt;Name the Queue program as: PB1_QueueException.cpp&lt;br /&gt;Name the Header as: PB1_QueueException.h&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-6357550690312974792?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/FBZN-42s_ZVStczqCGJUVYrvLQw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/FBZN-42s_ZVStczqCGJUVYrvLQw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/FBZN-42s_ZVStczqCGJUVYrvLQw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/FBZN-42s_ZVStczqCGJUVYrvLQw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/-xATFk37Cvo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/6357550690312974792/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/excveptiona-handling.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/6357550690312974792?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/6357550690312974792?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/-xATFk37Cvo/excveptiona-handling.html" title="Excveptiona Handling" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/excveptiona-handling.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkQDQH0zfip7ImA9WxNWFEg.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-4069901937260154150</id><published>2009-10-13T08:20:00.000-07:00</published><updated>2009-10-13T09:12:51.386-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-13T09:12:51.386-07:00</app:edited><title>Static Polymorphism</title><content type="html">&lt;h1&gt; &lt;span class="mw-headline"&gt;CPP09 W1 M1: Static Polymorphism: Templates&lt;/span&gt;&lt;/h1&gt; &lt;a name="Learning_Objectives" id="Learning_Objectives"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Learning Objectives&lt;/span&gt;&lt;/h2&gt; &lt;p&gt;At the end of this Module, you will be able to: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Explain how to achieve static polymorphic behavior through the use of templates &lt;/li&gt;&lt;li&gt;Explain how to write generic code using templates &lt;/li&gt;&lt;li&gt;Explain how to declare and implement function templates &lt;/li&gt;&lt;li&gt;Explain how to declare and implement class templates &lt;/li&gt;&lt;li&gt;Explain how to declare and implement class member function templates &lt;/li&gt;&lt;/ul&gt; &lt;a name="Static_Polymorphism" id="Static_Polymorphism"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Static Polymorphism&lt;/span&gt;&lt;/h2&gt; &lt;a name="Introduction" id="Introduction"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Introduction&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;Static polymorphic behavior is realized at compile time. A class template acts as a generic class definition from which new class types can be defined by a simple change of parameter types. Whereas a normal class is used to create objects, a class template is used to define new class types, from which objects are then created. The compiler uses the types you supply, combined with the class template, to create a wholly new class type. &lt;/p&gt;&lt;p&gt;Understanding how to declare, implement, and use class and function templates will pay big dividends in many ways. Primarily, it provides you with a mechanism to write generic code. Learning to write generic code can potentially save you a lot of work. &lt;/p&gt; &lt;a name="Definition_of_Template" id="Definition_of_Template"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Definition of Template&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;A template defines a related set of classes or functions. The related set of classes or functions defined by a template share the same code structure and functionality. The class or function template can then be used to declare a new class or function type. &lt;/p&gt;&lt;p&gt;&lt;b&gt;Function Templates&lt;/b&gt; &lt;/p&gt;&lt;p&gt;A function template is a generic function declaration and definition from which different versions of the function can be created by the compiler based on the argument types used to call the function. If you think this sounds a lot like overloaded functions you are right. Function templates and overloaded functions are related as you will soon see. &lt;/p&gt;&lt;p&gt;&lt;b&gt;Class Templates&lt;/b&gt; &lt;/p&gt;&lt;p&gt;A class template is a generic class declaration and definition from which different, but related, class types can be created by the compiler based on type parameters. &lt;/p&gt;&lt;p&gt;&lt;b&gt;Structure Templates&lt;/b&gt; &lt;/p&gt;&lt;p&gt;A structure template is like a class template but using structures instead. &lt;/p&gt; &lt;a name="How_Templates_Work:_An_Analogy" id="How_Templates_Work:_An_Analogy"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;How Templates Work: An Analogy&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;When you declare and define a template you are creating a generic version of whatever piece of code you are writing, be it a function or a class. In the declaration and definition of the template you will use one or more identifiers as type placeholders. These type placeholders are similar in function to the placeholders in a form letter generated with a word processor. &lt;/p&gt;&lt;p&gt;The below figure illustrates a simple mail merge operation. A master letter is created with placeholders for certain data elements. The structure of the data source is mapped to the master letter by the placeholder identifiers name and age. When the mail merge function is executed, the data source is merged with the master letter to yield the finished letters. The master letter is a generic document that can be reused to generate many specific letter instances. Class and function templates work in similar fashion. A generic function or class is declared and defined. Placeholders are inserted into the code to reserve spots for actual data types. When specific versions of a template function are required the type substitutions are made based on the types of the arguments used to call the function. In the case of template classes, a special syntax is used when a new template class is declared. &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M9/mailmerge_template.jpg" alt="mailmerge_template.jpg" /&gt; &lt;/p&gt; &lt;a name="Declaring_And_Using_Function_Templates" id="Declaring_And_Using_Function_Templates"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Declaring And Using Function Templates&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;Up until now, if you wanted to create different versions of the same function to operate on different data types you would overload the function. For instance, if you wanted to declare a function named Sum() that took two arguments, added them together, and returned the result you could create several versions of the function like so: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; Sum&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; val1, &lt;span class="kw4"&gt;int&lt;/span&gt; val2&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;
&lt;br /&gt;&lt;span class="kw4"&gt;float&lt;/span&gt; Sum&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;float&lt;/span&gt; val1, &lt;span class="kw4"&gt;float&lt;/span&gt; val2&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;
&lt;br /&gt;&lt;span class="kw4"&gt;char&lt;/span&gt; Sum&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt; val1, &lt;span class="kw4"&gt;char&lt;/span&gt; val2&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;These three functions can be replaced with one function template. &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;ol&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#ifndef SUM_TEMPLATE_H&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#define SUM_TEMPLATE_H&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1 ln-xtra"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T&lt;span class="sy1"&gt;&gt;&lt;/span&gt; T Sum&lt;span class="br0"&gt;(&lt;/span&gt;T val1, T val2&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt; &lt;span class="kw1"&gt;return&lt;/span&gt; val1 &lt;span class="sy2"&gt;+&lt;/span&gt; val2&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#endif&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;The Sum() function is declared to be a template by the keyword template appearing on line 4. Following the keyword template in angle brackets is the keyword class followed by a placeholder identifier named T. The class keyword as it is used here essentially means “any type”. The placeholder T will then appear somewhere in the function. It can appear in more than one place, as it does here in the parameter list. You can use any valid identifier as a placeholder name, not just T. You can also declare more than one placeholder. &lt;/p&gt;&lt;p&gt;To use the Sum() function template The Sum() function is called the same way as normal functions are called. The example below shows a main() function using the Sum() function on different data types. &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;ol&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#ifndef SUM_TEMPLATE_H&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#define SUM_TEMPLATE_H&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li2 ln-xtra"&gt;&lt;pre class="de2"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T&lt;span class="sy1"&gt;&gt;&lt;/span&gt; T Sum&lt;span class="br0"&gt;(&lt;/span&gt;T val1, T val2&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="kw1"&gt;return&lt;/span&gt; val1 &lt;span class="sy2"&gt;+&lt;/span&gt; val2&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#endif&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;Sum&lt;span class="br0"&gt;(&lt;/span&gt;3, 25&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;span class="co1"&gt;//Integer&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;Sum&lt;span class="br0"&gt;(&lt;/span&gt;3.456, 5.786&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//Float&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;Sum&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;'a'&lt;/span&gt;, &lt;span class="st0"&gt;'b'&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//Char&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;&lt;span class="co1"&gt;//cout&lt;&lt;sum(3,&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt; &lt;a name="Using_Multiple_Placeholders" id="Using_Multiple_Placeholders"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Using Multiple Placeholders&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;The Sum() function template declared and defined in the above example used one type placeholder named T to reserve type spots in the function. Because both of the Sum() function’s parameters are reserved with the same placeholder they must be of the same type when the function is called. To illustrate, let us see what happens when the Sum() function is called with an integer and a float argument as shown in the following line of code: &lt;/p&gt;&lt;p&gt;cout&lt;&lt;sum(3,&gt;&lt;p&gt;Error : in function int main() line no 15: no matching function for call to `Sum(int, double)  &lt;/p&gt;&lt;p&gt;When the Sum() function template is called with two different argument types an error results. This error was produced using the Dev C++ 4.9.9.2. One way to eliminate this error is to declare the Sum() function template to use two different placeholders. &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;ol&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#ifndef SUM_TEMPLATE_H&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#define SUM_TEMPLATE_H&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1 ln-xtra"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T, &lt;span class="kw2"&gt;class&lt;/span&gt; U&lt;span class="sy1"&gt;&gt;&lt;/span&gt; T Sum&lt;span class="br0"&gt;(&lt;/span&gt;T val1, U val2&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;  &lt;span class="kw1"&gt;return&lt;/span&gt; val1 &lt;span class="sy2"&gt;+&lt;/span&gt; val2&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#endif&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;Sum&lt;span class="br0"&gt;(&lt;/span&gt;3, 25&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;Sum&lt;span class="br0"&gt;(&lt;/span&gt;3.456, 5.786&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;Sum&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;'a'&lt;/span&gt;, &lt;span class="st0"&gt;'b'&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;Sum&lt;span class="br0"&gt;(&lt;/span&gt;3, 3.5&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt; &lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;Notice now there are two type placeholders declared on line 5 of the above example. T and U. The U placeholder is used for the second parameter in the Sum() function while the T placeholder is used for the first parameter and the return value. This will solve one problem but introduce another. The above Example shows the revised Sum() function template in use. &lt;/p&gt;&lt;p&gt;Refer to line 14 of the above example. Notice now that the Sum() function is called with the first argument an integer and the second argument a float. By using two placeholders in the function template the error produced by using two different argument types is eliminated. However, the result type of the Sum() function is dictated by the first argument type. Since the T placeholder is used to reserve the type spot for both the first parameter and the return type of the function, whatever type the first argument to the function happens to be will also be the return type of the function. In this example it is an integer. So, the result of calling the Sum() function with the arguments 3 and 3.5 is 6, not 6.5! Notice what happens when the order of the arguments are swapped. &lt;/p&gt;&lt;p&gt;
&lt;br /&gt;To resolve the &lt;b&gt;ambiguous&lt;/b&gt; return type issue simply declare yet another type place holder used specifically to dictate the function’s return type. The below example gives the revised Sum() function with the extra type placeholder V declared and used to reserve the return type. &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;ol&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#ifndef SUM_TEMPLATE_H&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#define SUM_TEMPLATE_H&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1 ln-xtra"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T, &lt;span class="kw2"&gt;class&lt;/span&gt; U, &lt;span class="kw2"&gt;class&lt;/span&gt; V&lt;span class="sy1"&gt;&gt;&lt;/span&gt; V Sum&lt;span class="br0"&gt;(&lt;/span&gt;T val1, U val2&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt; &lt;span class="kw1"&gt;return&lt;/span&gt; val1 &lt;span class="sy2"&gt;+&lt;/span&gt; val2&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#endif&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;Sum&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt;, &lt;span class="kw4"&gt;int&lt;/span&gt;, &lt;span class="kw4"&gt;int&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;3, 25&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;Sum&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;double&lt;/span&gt;, &lt;span class="kw4"&gt;double&lt;/span&gt;, &lt;span class="kw4"&gt;double&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;3.456, 5.786&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;Sum&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt;, &lt;span class="kw4"&gt;char&lt;/span&gt;, &lt;span class="kw4"&gt;char&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;'a'&lt;/span&gt;, &lt;span class="st0"&gt;'b'&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;Sum&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;double&lt;/span&gt;, &lt;span class="kw4"&gt;int&lt;/span&gt;, &lt;span class="kw4"&gt;double&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;3.5, 3&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;Notice the Sum() function call on line 11. The function name Sum is followed by a series of type names enclosed in angle brackets. This is referred to as a template specialization. The order of type names appearing in the specialization map to the same order as the function template type parameters. On line 11 the Sum() function is being specialized to take two integers as arguments to the function and return an integer value. Following the specialization is the argument list appearing in parentheses as usual. Line 12 introduces another Sum() function specialization, as does line 13 and line 14. The specialization on line 14 says that the Sum() function will take a double as the first argument, an integer as the second argument, and return a double value. &lt;/p&gt; &lt;a name="Declaring_And_Using_Class_Templates" id="Declaring_And_Using_Class_Templates"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Declaring And Using Class Templates&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;Class templates are used to declare and define a generic class structure. The compiler uses the class template and any types supplied via specialization to build a new class type. Let us begin the discussion of class templates with a simple Foo example. Example below shows the declaration and definition of a class template named Foo. &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;ol&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#ifndef FOOTEMPLATEDEF_H&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1 ln-xtra"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#define FOOTEMPLATEDEF_H&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T&lt;span class="sy1"&gt;&gt;&lt;/span&gt; &lt;span class="kw2"&gt;class&lt;/span&gt; Foo&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	Foo&lt;span class="br0"&gt;(&lt;/span&gt;T _val&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw2"&gt;virtual&lt;/span&gt; ~Foo&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;	&lt;span class="kw4"&gt;void&lt;/span&gt; setVal&lt;span class="br0"&gt;(&lt;/span&gt;T _val&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	T getVal&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;private&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	T val&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T&lt;span class="sy1"&gt;&gt;&lt;/span&gt; Foo&lt;span class="sy1"&gt;&lt;&lt;/span&gt;T&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;Foo&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;T _val&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;val&lt;span class="br0"&gt;(&lt;/span&gt;_val&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T&lt;span class="sy1"&gt;&gt;&lt;/span&gt; Foo&lt;span class="sy1"&gt;&lt;&lt;/span&gt;T&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="sy4"&gt;::&lt;/span&gt;~Foo&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T&lt;span class="sy1"&gt;&gt;&lt;/span&gt; &lt;span class="kw4"&gt;void&lt;/span&gt; Foo&lt;span class="sy1"&gt;&lt;&lt;/span&gt;T&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;setVal&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;T _val&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	val &lt;span class="sy1"&gt;=&lt;/span&gt; _val&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T&lt;span class="sy1"&gt;&gt;&lt;/span&gt; T Foo&lt;span class="sy1"&gt;&lt;&lt;/span&gt;T&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;getVal&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;	&lt;span class="kw1"&gt;return&lt;/span&gt; val&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#endif&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	Foo&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt; f1&lt;span class="br0"&gt;(&lt;/span&gt;1&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	Foo&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt; f2&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;'d'&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;	&lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;f1.&lt;span class="me1"&gt;getVal&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;f2.&lt;span class="me1"&gt;getVal&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;The declaration of the Foo class templates begins on line 6 with the keyword template. There is only one template parameter declared named T. The T is used throughout the class declaration and definition to reserve a spot for the type declared when the Foo class is specialized. &lt;/p&gt;&lt;p&gt;Referring to line 33 of above example, notice how the Foo class template is specialized to use an int type. The important point to note in this example is that Foo&lt;int&gt; and Foo&lt;char&gt; are two distinct types. &lt;/p&gt; &lt;a name="A_More_Complex_Class_Template_Example" id="A_More_Complex_Class_Template_Example"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;A More Complex Class Template Example&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;The below Example gives the source code for a template of the DynamicArray class.  &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;ol&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#ifndef _DYNAMIC_ARRAY_H&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1 ln-xtra"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#define _DYNAMIC_ARRAY_H&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T&lt;span class="sy1"&gt;&gt;&lt;/span&gt; &lt;span class="kw2"&gt;class&lt;/span&gt; DynamicArray&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	DynamicArray&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; _size &lt;span class="sy1"&gt;=&lt;/span&gt; 5&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw2"&gt;virtual&lt;/span&gt; ~DynamicArray&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;	T&lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt; operator&lt;span class="br0"&gt;[&lt;/span&gt;&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;unsigned&lt;/span&gt; i&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw4"&gt;int&lt;/span&gt; getSize&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;private&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	T&lt;span class="sy2"&gt;*&lt;/span&gt; its_array&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw4"&gt;int&lt;/span&gt; size&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T&lt;span class="sy1"&gt;&gt;&lt;/span&gt; DynamicArray&lt;span class="sy1"&gt;&lt;&lt;/span&gt;T&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;DynamicArray&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; _size&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;size&lt;span class="br0"&gt;(&lt;/span&gt;_size&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	its_array &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="kw3"&gt;new&lt;/span&gt; T&lt;span class="br0"&gt;[&lt;/span&gt;_size&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw1"&gt;for&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; i&lt;span class="sy1"&gt;=&lt;/span&gt;&lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; i&lt;span class="sy1"&gt;&lt;&lt;/span&gt;size&lt;span class="sy4"&gt;;&lt;/span&gt; i&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;		its_array&lt;span class="br0"&gt;[&lt;/span&gt;i&lt;span class="br0"&gt;]&lt;/span&gt; &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="kw2"&gt;static_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;T&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;0&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T&lt;span class="sy1"&gt;&gt;&lt;/span&gt; DynamicArray&lt;span class="sy1"&gt;&lt;&lt;/span&gt;T&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="sy4"&gt;::&lt;/span&gt;~DynamicArray&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw3"&gt;delete&lt;/span&gt;&lt;span class="br0"&gt;[&lt;/span&gt;&lt;span class="br0"&gt;]&lt;/span&gt; its_array&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T&lt;span class="sy1"&gt;&gt;&lt;/span&gt; T&lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt; DynamicArray&lt;span class="sy1"&gt;&lt;&lt;/span&gt;T&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;operator&lt;/span&gt;&lt;span class="br0"&gt;[&lt;/span&gt;&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;unsigned&lt;/span&gt; i&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;i &lt;span class="sy1"&gt;&gt;=&lt;/span&gt; &lt;span class="br0"&gt;(&lt;/span&gt;size&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw4"&gt;int&lt;/span&gt; newsize &lt;span class="sy1"&gt;=&lt;/span&gt; size&lt;span class="sy2"&gt;+&lt;/span&gt;&lt;span class="nu0"&gt;10&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;	T&lt;span class="sy2"&gt;*&lt;/span&gt; temp &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="kw3"&gt;new&lt;/span&gt; T&lt;span class="br0"&gt;[&lt;/span&gt;size&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw1"&gt;for&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; j &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy1"&gt;&lt;&lt;/span&gt;size&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	temp&lt;span class="br0"&gt;[&lt;/span&gt;j&lt;span class="br0"&gt;]&lt;/span&gt; &lt;span class="sy1"&gt;=&lt;/span&gt; its_array&lt;span class="br0"&gt;[&lt;/span&gt;j&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw3"&gt;delete&lt;/span&gt;&lt;span class="br0"&gt;[&lt;/span&gt;&lt;span class="br0"&gt;]&lt;/span&gt; its_array&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;	its_array &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="kw3"&gt;new&lt;/span&gt; T&lt;span class="br0"&gt;[&lt;/span&gt;newsize&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw1"&gt;for&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; j &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy1"&gt;&lt;&lt;/span&gt;size&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;		its_array&lt;span class="br0"&gt;[&lt;/span&gt;j&lt;span class="br0"&gt;]&lt;/span&gt; &lt;span class="sy1"&gt;=&lt;/span&gt; temp&lt;span class="br0"&gt;[&lt;/span&gt;j&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw1"&gt;for&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; j&lt;span class="sy1"&gt;=&lt;/span&gt;size&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy1"&gt;&lt;&lt;/span&gt;newsize&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;		its_array&lt;span class="br0"&gt;[&lt;/span&gt;j&lt;span class="br0"&gt;]&lt;/span&gt; &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="kw2"&gt;static_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;T&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;0&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw3"&gt;delete&lt;/span&gt;&lt;span class="br0"&gt;[&lt;/span&gt;&lt;span class="br0"&gt;]&lt;/span&gt; temp&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	size &lt;span class="sy1"&gt;=&lt;/span&gt; newsize&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw1"&gt;return&lt;/span&gt; its_array&lt;span class="br0"&gt;[&lt;/span&gt;i&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;&lt;span class="br0"&gt;}&lt;/span&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw1"&gt;else&lt;/span&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw1"&gt;return&lt;/span&gt; its_array&lt;span class="br0"&gt;[&lt;/span&gt;i&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;&lt;span class="kw2"&gt;template&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; T&lt;span class="sy1"&gt;&gt;&lt;/span&gt; &lt;span class="kw4"&gt;int&lt;/span&gt; DynamicArray&lt;span class="sy1"&gt;&lt;&lt;/span&gt;T&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;getSize&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="kw1"&gt;return&lt;/span&gt; size&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#endif&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt; &lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	DynamicArray&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt; d1&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	DynamicArray&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;float&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt; d2&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw1"&gt;for&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; i&lt;span class="sy1"&gt;=&lt;/span&gt;&lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; i&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="nu0"&gt;6&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; i&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;		d1&lt;span class="br0"&gt;[&lt;/span&gt;i&lt;span class="br0"&gt;]&lt;/span&gt; &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="st0"&gt;'a'&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;	&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw1"&gt;for&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; i&lt;span class="sy1"&gt;=&lt;/span&gt;&lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; i&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="nu0"&gt;6&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; i&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;		d2&lt;span class="br0"&gt;[&lt;/span&gt;i&lt;span class="br0"&gt;]&lt;/span&gt; &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="br0"&gt;(&lt;/span&gt;i &lt;span class="sy2"&gt;+&lt;/span&gt; .5&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="kw1"&gt;for&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; i&lt;span class="sy1"&gt;=&lt;/span&gt;&lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; i&lt;span class="sy1"&gt;&lt;&lt;/span&gt;d1.&lt;span class="me1"&gt;getSize&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; i&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li2"&gt;&lt;pre class="de2"&gt;		&lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;d1&lt;span class="br0"&gt;[&lt;/span&gt;i&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;&lt;span class="st0"&gt;" "&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;d2&lt;span class="br0"&gt;[&lt;/span&gt;i&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;	&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li class="li1"&gt;&lt;pre class="de1"&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;Converting the DynamicArray class into a class template increased its usefulness as it can now be used to hold different types of objects, even user-defined types. The above example gives a main() function showing the DynamicArray class template in use. &lt;/p&gt; &lt;a name="Resources" id="Resources"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Resources&lt;/span&gt;&lt;/h2&gt;
&lt;br /&gt;&lt;pre&gt;/ Fig.�11.2: tstack1.h
&lt;br /&gt;// Stack class template.
&lt;br /&gt;#ifndef TSTACK1_H
&lt;br /&gt;#define TSTACK1_H
&lt;br /&gt;
&lt;br /&gt;template&lt;&gt;
&lt;br /&gt;class Stack {     
&lt;br /&gt;
&lt;br /&gt;public:
&lt;br /&gt;Stack( int = 10 );  // default constructor (stack size 10)
&lt;br /&gt;
&lt;br /&gt;// destructor
&lt;br /&gt;~Stack()
&lt;br /&gt;{
&lt;br /&gt;delete [] stackPtr;
&lt;br /&gt;
&lt;br /&gt;} // end ~Stack destructor
&lt;br /&gt;
&lt;br /&gt;bool push( const T&amp;amp; );  // push an element onto the stack
&lt;br /&gt;bool pop( T&amp;amp; );         // pop an element off the stack
&lt;br /&gt;// determine whether Stack is empty
&lt;br /&gt;bool isEmpty() const
&lt;br /&gt;{
&lt;br /&gt;return top == -1;
&lt;br /&gt;
&lt;br /&gt;} // end function isEmpty
&lt;br /&gt;
&lt;br /&gt;// determine whether Stack is full
&lt;br /&gt;bool isFull() const
&lt;br /&gt;{
&lt;br /&gt;return top == size - 1;
&lt;br /&gt;
&lt;br /&gt;} // end function isFull
&lt;br /&gt;
&lt;br /&gt;private:
&lt;br /&gt;int size;     // # of elements in the stack
&lt;br /&gt;int top;      // location of the top element
&lt;br /&gt;T *stackPtr;  // pointer to the stack
&lt;br /&gt;
&lt;br /&gt;}; // end class Stack
&lt;br /&gt;
&lt;br /&gt;// constructor
&lt;br /&gt;template&lt;&gt;      
&lt;br /&gt;Stack&lt;&gt;::Stack( int s )
&lt;br /&gt;{
&lt;br /&gt;size = s &gt; 0 ? s : 10; 
&lt;br /&gt;top = -1;  // Stack initially empty
&lt;br /&gt;stackPtr = new T[ size ]; // allocate memory for elements
&lt;br /&gt;
&lt;br /&gt;} // end Stack constructor
&lt;br /&gt;
&lt;br /&gt;// push element onto stack;
&lt;br /&gt;// if successful, return true; otherwise, return false
&lt;br /&gt;template&lt;&gt;                       
&lt;br /&gt;bool Stack&lt;&gt;::push( const T &amp;amp;pushValue )
&lt;br /&gt;{
&lt;br /&gt;if ( !isFull() ) {
&lt;br /&gt;stackPtr[ ++top ] = pushValue;  // place item on Stack
&lt;br /&gt;return true;  // push successful
&lt;br /&gt;
&lt;br /&gt;} // end if
&lt;br /&gt;
&lt;br /&gt;return false;  // push unsuccessful
&lt;br /&gt;
&lt;br /&gt;} // end function push
&lt;br /&gt;
&lt;br /&gt;// pop element off stack;
&lt;br /&gt;// if successful, return true; otherwise, return false
&lt;br /&gt;template&lt;&gt;               
&lt;br /&gt;bool Stack&lt;&gt;::pop( T &amp;amp;popValue )
&lt;br /&gt;{
&lt;br /&gt;if ( !isEmpty() ) {
&lt;br /&gt;popValue = stackPtr[ top-- ];  // remove item from Stack
&lt;br /&gt;return true;  // pop successful
&lt;br /&gt;
&lt;br /&gt;} // end if
&lt;br /&gt;
&lt;br /&gt;return false;  // pop unsuccessful
&lt;br /&gt;
&lt;br /&gt;} // end function pop
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;// Fig.�11.3: fig11_03.cpp
&lt;br /&gt;// Stack-class-template test program.
&lt;br /&gt;#include &lt;iostream&gt;
&lt;br /&gt;
&lt;br /&gt;using std::cout;
&lt;br /&gt;using std::cin;
&lt;br /&gt;using std::endl;
&lt;br /&gt;
&lt;br /&gt;#include "tstack1.h"  // Stack class template definition
&lt;br /&gt;
&lt;br /&gt;int main()
&lt;br /&gt;{
&lt;br /&gt;Stack&lt;&gt; doubleStack( 5 );
&lt;br /&gt;double doubleValue = 1.1;
&lt;br /&gt;
&lt;br /&gt;cout &lt;&lt; "Pushing elements onto doubleStack\n";
&lt;br /&gt;
&lt;br /&gt;while ( doubleStack.push( doubleValue ) ) {
&lt;br /&gt;cout &lt;&lt; doubleValue &lt;&lt; ' ';
&lt;br /&gt;doubleValue += 1.1;
&lt;br /&gt;
&lt;br /&gt;} // end while
&lt;br /&gt;
&lt;br /&gt;cout &lt;&lt; "\nStack is full. Cannot push " &lt;&lt; doubleValue
&lt;br /&gt;&lt;&lt; "\n\nPopping elements from doubleStack\n";
&lt;br /&gt;while ( doubleStack.pop( doubleValue ) )
&lt;br /&gt;cout &lt;&lt; doubleValue &lt;&lt; ' ';
&lt;br /&gt;
&lt;br /&gt;cout &lt;&lt; "\nStack is empty. Cannot pop\n";
&lt;br /&gt;
&lt;br /&gt;Stack&lt;&gt; intStack;
&lt;br /&gt;int intValue = 1;
&lt;br /&gt;cout &lt;&lt; "\nPushing elements onto intStack\n";
&lt;br /&gt;
&lt;br /&gt;while ( intStack.push( intValue ) ) {
&lt;br /&gt;cout &lt;&lt; intValue &lt;&lt; ' ';
&lt;br /&gt;++intValue;
&lt;br /&gt;
&lt;br /&gt;} // end while
&lt;br /&gt;
&lt;br /&gt;cout &lt;&lt; "\nStack is full. Cannot push " &lt;&lt; intValue
&lt;br /&gt;&lt;&lt; "\n\nPopping elements from intStack\n";
&lt;br /&gt;
&lt;br /&gt;while ( intStack.pop( intValue ) ) 
&lt;br /&gt;cout &lt;&lt; intValue &lt;&lt; ' ';
&lt;br /&gt;
&lt;br /&gt;cout &lt;&lt; "\nStack is empty. Cannot pop\n";
&lt;br /&gt;
&lt;br /&gt;return 0;
&lt;br /&gt;} // end main
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;#endif
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;2.http://www.functionx.com/cpp/Lesson07.htm
&lt;br /&gt;&lt;/pre&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Steps to attack the problem sets&lt;/span&gt;&lt;/h2&gt; &lt;ul&gt;&lt;li&gt;Step 1: Read about &lt;a href="http://wiki.msitprogram.net/index.php/CPP09:W1-M9#Static_Polymorphism" title="CPP09:W1-M9"&gt; Static Polymorphism&lt;/a&gt; and try sample programs(DynamicArray) given in Static Polymorphism(You can copy paste the programs) &lt;/li&gt;&lt;li&gt;Step 2: Go through the PPT on Templates and try stack program from the PPT.(you can copy paste the program) &lt;/li&gt;&lt;li&gt;Step 3: Go through the additional resources given.  &lt;/li&gt;&lt;li&gt;Step 4: Attack the problem sets in the given order(Problem Set A and then Problem Set B ...). &lt;/li&gt;&lt;li&gt;Step 5: If you are not clear with the problem sets then contact your respective mentor for clarification. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Spend atleast 2 hours from Step 1 to Step 3. &lt;/p&gt; &lt;a name="Problem_Sets" id="Problem_Sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Problem Sets&lt;/span&gt;&lt;/h2&gt; &lt;a name="Problem_Set_A" id="Problem_Set_A"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set A&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;Create a function called swaps() that interchanges the values of the two arguments sent to it. (You will probably want to pass these arguments by reference.) Make the function into a template, so it can be used with all numerical data types (char, int, float, and so on). Write a main() program to exercise the function with several types. &lt;/p&gt; Name the program as: PA1_swaps.cpp  &lt;a name="Problem_Set_B" id="Problem_Set_B"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set B&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;Create a function called amax() that returns the value of the largest element in an array. The arguments to the function should be the address of the array and its size. Make this function into a template so it will work with an array of any numerical type. Write a main() program that applies this function to arrays of various types. &lt;/p&gt; Name the program as:PB1_amax.cpp  &lt;a name="Problem_Set_C" id="Problem_Set_C"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set C&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;3. &lt;/b&gt;A queue is a data-storage device. It’s like a stack, except that, instead of being last-in first-out, it’s first-in-first-out, like the line at a bank teller’s window. If you put in 1, 2, 3,you get back 1, 2, 3 in that order. &lt;/p&gt;&lt;p&gt;A stack needs only one index to an array (i.e top of the array). A queue, on the other hand, must keep track of two indexes to an array: one to the tail, where new items are added, and one to the head, where old items are removed. The tail follows the head through the array as items are added and removed. If either the tail or the head reaches the end of the array, it is reset to the beginning. &lt;/p&gt;&lt;p&gt;Write a class template for a queue class. Assume that the programmer using the queue won’t make any mistakes, like exceeding the capacity of the queue or trying to remove an item when the queue is empty. Define several queues of different data types and insert and remove data from them. &lt;/p&gt;&lt;p&gt;Use the &lt;a href="http://wiki.msitprogram.net/index.php/CPP09:W1-M9#A_More_Complex_Class_Template_Example" title="CPP09:W1-M9"&gt; dynamicarray&lt;/a&gt; discussed previously for implementing the queue. &lt;/p&gt;&lt;p&gt;Write the declaration and defining of Queue template in separate header file and a Test program to test the queue in separate source file. &lt;/p&gt;&lt;pre&gt;
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-4069901937260154150?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/gifgvV2Jk8EEyhF5f2L1oISLTJE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gifgvV2Jk8EEyhF5f2L1oISLTJE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/gifgvV2Jk8EEyhF5f2L1oISLTJE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gifgvV2Jk8EEyhF5f2L1oISLTJE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/qS5gkhvcPTQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/4069901937260154150/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/static-polymorphism.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/4069901937260154150?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/4069901937260154150?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/qS5gkhvcPTQ/static-polymorphism.html" title="Static Polymorphism" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/static-polymorphism.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0YBRXg-eyp7ImA9WxNWFEk.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-2834112665844514774</id><published>2009-10-12T09:04:00.000-07:00</published><updated>2009-10-13T08:52:34.653-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-13T08:52:34.653-07:00</app:edited><title>Friend Functions</title><content type="html">&lt;h1&gt; &lt;span class="mw-headline"&gt;CPP09 - W1 - M8: Friend Functions&lt;/span&gt;&lt;/h1&gt; &lt;a name="Learning_Objectives" id="Learning_Objectives"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Learning Objectives&lt;/span&gt;&lt;/h2&gt; &lt;p&gt;At the end of this module, you will able to: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Explain and use friend functions in C++ programs &lt;/li&gt;&lt;li&gt;Implement software objects as friends to one another &lt;/li&gt;&lt;/ul&gt; &lt;a name="Introduction" id="Introduction"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Introduction&lt;/span&gt;&lt;/h2&gt; &lt;a name="Friend" id="Friend"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Friend &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;The concepts of encapsulation and data hiding dictate that nonmember functions should not be able to access an object’s private or protected data. The policy is, if you’re not a member, you can’t get in. However, there are situations where such rigid discrimination leads to considerable inconvenience. in order to access the non-public members of a class, C++ provides the &lt;b&gt;friend&lt;/b&gt; facility. &lt;/p&gt;&lt;p&gt;The accessibility of class members in various forms is shown in the below figure &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M8/friends.jpg" alt="friends.jpg" /&gt; &lt;/p&gt; &lt;a name="Friend_Functions" id="Friend_Functions"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Friend Functions&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;Imagine that you want a function to operate on objects of two different classes. Perhaps the function will take objects of the two classes as arguments, and operate on their private data. In this situation there’s nothing like a friend function. Here’s a simple example, FRIEND, that shows how friend functions can act as a bridge between two classes: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// friend.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// friend functions&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; beta&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//needed for frifunc declaration&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; alpha&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;private&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; data&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;alpha&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="sy4"&gt;:&lt;/span&gt; data&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="nu0"&gt;3&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt; &lt;span class="co1"&gt;//no-arg constructor&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;friend&lt;/span&gt; &lt;span class="kw4"&gt;int&lt;/span&gt; frifunc&lt;span class="br0"&gt;(&lt;/span&gt;alpha, beta&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//friend function&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; beta&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;private&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; data&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;beta&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="sy4"&gt;:&lt;/span&gt; data&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="nu0"&gt;7&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt; &lt;span class="co1"&gt;//no-arg constructor&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;friend&lt;/span&gt; &lt;span class="kw4"&gt;int&lt;/span&gt; frifunc&lt;span class="br0"&gt;(&lt;/span&gt;alpha, beta&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//friend function&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; frifunc&lt;span class="br0"&gt;(&lt;/span&gt;alpha a, beta b&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="co1"&gt;//function definition&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt; a.&lt;span class="me1"&gt;data&lt;/span&gt; &lt;span class="sy2"&gt;+&lt;/span&gt; b.&lt;span class="me1"&gt;data&lt;/span&gt; &lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//--------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;alpha aa&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;beta bb&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; frifunc&lt;span class="br0"&gt;(&lt;/span&gt;aa, bb&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; endl&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//call the function&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;In this program, the two classes are alpha and beta. The constructors in these classes initialize their single data items to fixed values (3 in alpha and 7 in beta). &lt;/p&gt;&lt;p&gt;We want the function frifunc() to have access to both of these private data members, so we make it a friend function. It’s declared with the friend keyword in both classes: &lt;/p&gt;&lt;p&gt;friend int frifunc(alpha, beta); &lt;/p&gt;&lt;p&gt;This declaration can be placed anywhere in the class; it doesn’t matter whether it goes in the public or the private section. &lt;/p&gt;&lt;p&gt;An object of each class is passed as an argument to the function frifunc(), and it accesses the private data member of both classes through these arguments. The function doesn’t do much: It adds the data items and returns the sum. The main() program calls this function and prints the result. &lt;/p&gt;&lt;p&gt;A minor point: Remember that a class can’t be referred to until it has been declared. Class beta is referred to in the declaration of the function frifunc() in class alpha, so beta must be declared before alpha. Hence the declaration class beta; at the beginning of the program. &lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt; &lt;a name="Friend_Classes" id="Friend_Classes"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Friend Classes&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;The member functions of a class can all be made friends at the same time when you make the entire class a friend. The program FRICLASS shows how this looks. &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// friclass.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// friend classes&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; alpha&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;private&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; data1&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;alpha&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="sy4"&gt;:&lt;/span&gt; data1&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="nu0"&gt;99&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt; &lt;span class="co1"&gt;//constructor&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;friend&lt;/span&gt; &lt;span class="kw2"&gt;class&lt;/span&gt; beta&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//beta is a friend class&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; beta&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="co1"&gt;//all member functions can&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="co1"&gt;//access private alpha data&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; func1&lt;span class="br0"&gt;(&lt;/span&gt;alpha a&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “\ndata1&lt;span class="sy1"&gt;=&lt;/span&gt;” &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; a.&lt;span class="me1"&gt;data1&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; func2&lt;span class="br0"&gt;(&lt;/span&gt;alpha a&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “\ndata1&lt;span class="sy1"&gt;=&lt;/span&gt;” &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; a.&lt;span class="me1"&gt;data1&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;alpha a&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;beta b&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;b.&lt;span class="me1"&gt;func1&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;a&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;b.&lt;span class="me1"&gt;func2&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;a&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;In class alpha the entire class beta is proclaimed a friend. Now all the member functions of beta can access the private data of alpha (in this program, the single data item data1). Note that in the friend declaration we specify that beta is a class using the class keyword: &lt;/p&gt;&lt;p&gt;friend class beta; &lt;/p&gt;&lt;p&gt;We could have also declared beta to be a class before the alpha class specifier, as in previous examples &lt;/p&gt;&lt;p&gt;class beta; &lt;/p&gt;&lt;p&gt;and then, within alpha, referred to beta without the class keyword: &lt;/p&gt;&lt;p&gt;friend beta; &lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt; &lt;a name="Resources" id="Resources"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Resources&lt;/span&gt;&lt;/h2&gt;&lt;br /&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-926906bb507ceb35" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;
&lt;param name="bgcolor" value="#FFFFFF"&gt;
&lt;param name="allowfullscreen" value="true"&gt;
&lt;param name="flashvars" value="flvurl=http://v6.nonxt7.googlevideo.com/videoplayback?id%3D926906bb507ceb35%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3DA467F7A16387E71407B2E2D20E92B78A2C4E35A.6144F3653212C27D90AC9D08064C6A3DE2F4F279%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D926906bb507ceb35%26offsetms%3D5000%26itag%3Dw160%26sigh%3Dxb_FH-1jFLa8jZ49ACUtipG5Jj8&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;
&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"
width="320" height="266" bgcolor="#FFFFFF"
flashvars="flvurl=http://v6.nonxt7.googlevideo.com/videoplayback?id%3D926906bb507ceb35%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3DA467F7A16387E71407B2E2D20E92B78A2C4E35A.6144F3653212C27D90AC9D08064C6A3DE2F4F279%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D926906bb507ceb35%26offsetms%3D5000%26itag%3Dw160%26sigh%3Dxb_FH-1jFLa8jZ49ACUtipG5Jj8&amp;autoplay=0&amp;ps=blogger"
allowFullScreen="true" /&gt;&lt;/object&gt;
&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;http://xoax.net/comp/cpp/console/Lesson9.php&lt;br /&gt;&lt;br /&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Steps to solve the problems sets&lt;/span&gt;&lt;/h2&gt;  &lt;ul&gt;&lt;li&gt;Read the introduction part of this module &lt;/li&gt;&lt;li&gt;View the video and read the supporting material &lt;/li&gt;&lt;li&gt;Think about how to write the tic tac toe program with objects. &lt;/li&gt;&lt;/ul&gt;  &lt;p&gt;Spend atleast 1 hour on the resources and Other modules covered so far. &lt;/p&gt; &lt;p&gt;&lt;br /&gt;&lt;/p&gt;  &lt;a name="Problem_Sets" id="Problem_Sets"&gt;&lt;/a&gt; &lt;h2&gt; &lt;span class="mw-headline"&gt;Problem Sets&lt;/span&gt;&lt;/h2&gt;  &lt;a name="Problem_Set_A" id="Problem_Set_A"&gt;&lt;/a&gt; &lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set A&lt;/span&gt;&lt;/h3&gt;  &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;Game Program: The object of tic-tac-toe is to get three X’s or three O’s in a row either horizontally, vertically or diagonally. The tic-tac-toe game board is a 3 x 3 grid and looks something like this: &lt;/p&gt; &lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M8/board.jpg" alt="board.jpg" /&gt; &lt;/p&gt; &lt;p&gt;During a typical game, players takes turns placing their X’s and O’s on the game board. The first to get their pattern in a row wins. The follow game board represents what the results of a typical game might look like: &lt;/p&gt; &lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M8/board_players.jpg" alt="board_players.jpg" /&gt; &lt;/p&gt; &lt;p&gt;Use a 3 x 3 array of cell objects to track player turns. Check the array after each player’s turn to see if they’ve won. Draw the game board on the screen after each turn so players can see the game’s progress. Make it a two player game. &lt;/p&gt; &lt;p&gt;Use Object Oriented Programming approach to solve this problem, thought the problem can be solved simple as presented in the video given. &lt;/p&gt; &lt;p&gt;Write a game program called tic-tac-toe by Identifying the Game as a class consists of Player Objects and Board Object. Board consists of Cell Objects representing the cell in the board. &lt;/p&gt; &lt;p&gt;Identify the Objects in the game with attributes and behavior.  &lt;/p&gt; &lt;p&gt;Player can have player Name as an attribute and behavior as playing the game. &lt;/p&gt; &lt;p&gt;Identify and the write friend functions\classes in the board class, cell class, player class and Game class. &lt;/p&gt; &lt;p&gt;Use your imagination to identify the objects, It's not limited to the classes which we have defined. &lt;/p&gt;  &lt;pre&gt;Name the program as:- PA1_TicTacToe.cpp&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-2834112665844514774?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/dlSNxhSKPGAMVE22mEq0opJ3YYI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dlSNxhSKPGAMVE22mEq0opJ3YYI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/dlSNxhSKPGAMVE22mEq0opJ3YYI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dlSNxhSKPGAMVE22mEq0opJ3YYI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/5Jgfhl0LV_U" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/2834112665844514774/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/friend-functions.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/2834112665844514774?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/2834112665844514774?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/5Jgfhl0LV_U/friend-functions.html" title="Friend Functions" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/friend-functions.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0YBQHk7eyp7ImA9WxNXFEw.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-6889684806364407112</id><published>2009-10-01T08:29:00.000-07:00</published><updated>2009-10-01T08:32:31.703-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-01T08:32:31.703-07:00</app:edited><title>Polymorphism</title><content type="html">&lt;h2&gt; &lt;span class="mw-headline"&gt;Introduction&lt;/span&gt;&lt;/h2&gt; &lt;a name="Polymorphism" id="Polymorphism"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Polymorphism&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;Polymorphism is the ability to associate more than one implementation with the same operation. The binding of the operation with the implementation, can be decided at compile time or at run time. In object orientation, polymorphism can exist in two ways. &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;b&gt;Hierarchy independent&lt;/b&gt; - This takes the form of overloading, where the same name of an operation, has different implementations in different classes and these classes need not be related by class hierarchy. &lt;/li&gt;&lt;li&gt;&lt;b&gt;Hierarchy dependent&lt;/b&gt;- Here, an operation defined in the superclass can have any number of implementations along the class hierarchy. This type of polymorphism is referred to as universal polymorphism or overriding. The operation can be bound to any of the implementations. This resolution can take place, at compile time or at run time. &lt;/li&gt;&lt;/ul&gt; &lt;a name="Pointers" id="Pointers"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Pointers&lt;/span&gt;&lt;/h3&gt; &lt;a name="Address_and_Pointers" id="Address_and_Pointers"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Address and Pointers&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;The ideas behind pointers are not complicated. Here’s the first key concept: Every byte in the computer’s memory has an address. Addresses are numbers, just as they are for houses on a street. The numbers start at 0 and go up from there—1, 2, 3, and so on. If you have 1MB of memory, the highest address is 1,048,575. (Of course you have much more.) &lt;/p&gt;&lt;p&gt;Your program, when it is loaded into memory, occupies a certain range of these addresses. That means that every &lt;b&gt;variable&lt;/b&gt; and every &lt;b&gt;function&lt;/b&gt; in your program starts at a particular address. &lt;/p&gt; &lt;a name="Pointers_to_void" id="Pointers_to_void"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Pointers to void&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;Before we go on to see pointers at work, we should note one peculiarity of pointer data types.Ordinarily, the address that you put in a pointer must be the same type as the pointer. You can’t assign the address of a float variable to a pointer to int, for example: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw4"&gt;float&lt;/span&gt; flovar &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="nu16"&gt;98.6&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt; ptrint &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;flovar&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//ERROR: can’t assign float* to int*&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;However, there is an exception to this. There is a sort of general-purpose pointer that can point to any data type. This is called a pointer to void, and is defined like this: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw4"&gt;void&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt; ptr&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//ptr can point to any data type&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;Such pointers have certain specialized uses, such as passing pointers to functions that operate independently of the data type pointed to. &lt;/p&gt;&lt;p&gt;The next example uses a pointer to void and also shows that, if you don’t use void, you must be careful to assign pointers an address of the same type as the pointer. Here’s the listing for PTRVOID: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// ptrvoid.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// pointers to type void&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; intvar&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//integer variable&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;float&lt;/span&gt; flovar&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//float variable&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt; ptrint&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//define pointer to int&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;float&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt; ptrflo&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//define pointer to float&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt; ptrvoid&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//define pointer to void&lt;/span&gt;&lt;br /&gt;ptrint &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;intvar&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//ok, int* to int*&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// ptrint = &amp;flovar; //error, float* to int*&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// ptrflo = &amp;intvar; //error, int* to float*&lt;/span&gt;&lt;br /&gt;ptrflo &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;flovar&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//ok, float* to float*&lt;/span&gt;&lt;br /&gt;ptrvoid &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;intvar&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//ok, int* to void*&lt;/span&gt;&lt;br /&gt;ptrvoid &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;flovar&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//ok, float* to void*&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;You can assign the address of intvar to ptrint because they are both type int*, but you can’t assign the address of flovar to ptrint because the first is type float* and the second is type int*. However, ptrvoid can be given any pointer value, such as int*, because it is a pointer to void. &lt;/p&gt;&lt;p&gt;If for some unusual reason you really need to assign one kind of pointer type to another, you can use the reinterpret_cast. For the lines commented out in PTRVOID, that would look like this: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;ptrint &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;flovar&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;ptrflo &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;&lt;span class="kw4"&gt;float&lt;/span&gt;&lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;intvar&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;The use of reinterpret_cast in this way is not recommended, but occasionally it’s the only way out of a difficult situation. Static casts won’t work with pointers. Old-style C casts can be used, but are always a bad idea in C++. &lt;/p&gt; &lt;a name="Base_class_Pointers" id="Base_class_Pointers"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Base class Pointers&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;In C++ a pointer of a base class can point to an object of any of its derived class. &lt;/p&gt;&lt;p&gt;In the example given below shows what happens when a base class and derived classes all have functions with the same name, and you access these functions using pointers. Here is the listing of BASEPOINT &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// basepoint.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// normal functions accessed from pointer&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Base &lt;span class="co1"&gt;//base class&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;      &lt;span class="kw4"&gt;void&lt;/span&gt; show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"Base&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Derv1 &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; Base &lt;span class="co1"&gt;//derived class 1&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;      &lt;span class="kw4"&gt;void&lt;/span&gt; show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"Derv1&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Derv2 &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; Base &lt;span class="co1"&gt;//derived class 2&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;      &lt;span class="kw4"&gt;void&lt;/span&gt; show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;           &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"Derv2&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;Derv1 dv1&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//object of derived class 1&lt;/span&gt;&lt;br /&gt;Derv2 dv2&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//object of derived class 2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Base&lt;span class="sy2"&gt;*&lt;/span&gt; ptr&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//pointer to base class&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Derv1 &lt;span class="sy2"&gt;*&lt;/span&gt;pdv1&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//pointer to Derv1&lt;/span&gt;&lt;br /&gt;Derv2 &lt;span class="sy2"&gt;*&lt;/span&gt;pdv2&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//pointer to Derv2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ptr &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;dv1&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//put address of dv1 in pointer&lt;/span&gt;&lt;br /&gt;ptr&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//execute show()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;ptr &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;dv2&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//put address of dv2 in pointer&lt;/span&gt;&lt;br /&gt;ptr&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//execute show()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;ptr &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;dv1&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//put address of dv1 in pointer&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;Derv1 &lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;ptr&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="sy3"&gt;!&lt;/span&gt;&lt;span class="sy1"&gt;=&lt;/span&gt;0&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;  pdv1 &lt;span class="sy1"&gt;=&lt;/span&gt;  &lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;Derv1 &lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;ptr&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  pdv1&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;ptr &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;dv2&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//put address of dv2 in pointer&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;Derv2 &lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;ptr&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="sy3"&gt;!&lt;/span&gt;&lt;span class="sy1"&gt;=&lt;/span&gt;0&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;  pdv2 &lt;span class="sy1"&gt;=&lt;/span&gt;  &lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;Derv2 &lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;ptr&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  pdv2&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;The &lt;i&gt;Derv1&lt;/i&gt; and &lt;i&gt;Derv2&lt;/i&gt; classes are derived from class Base. Each of these three classes has a member function show(). In main() we create objects of class Derv1 and Derv2, and a pointer to class Base. Then we put the address of a derived class object in the base class pointer in the line &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;ptr &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;dv1&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;// derived class address in base class pointer&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;But wait—how can we get away with this? Doesn’t the compiler complain that we’re assigning an address of one type (Derv1) to a pointer of another (Base)? On the contrary, the compiler is perfectly happy, because type checking has been relaxed in this situation, for reasons that will become apparent soon. The rule is that pointers to objects of a derived class are type-compatible with pointers to objects of the base class. &lt;/p&gt;&lt;p&gt;Now the question is, when you execute the line &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;ptr&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;what function is called? Is it Base::show() or Derv1::show()? Again, in the last two lines of BASEPOINT we put the address of an object of class Derv2 in the pointer, and again execute &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;ptr&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;Which of the show() functions is called here? The output from the program answers these questions: &lt;/p&gt;&lt;p&gt;Base &lt;/p&gt;&lt;p&gt;Base &lt;/p&gt;&lt;p&gt;As you can see, the function in the base class is always executed. The compiler ignores the contents of the pointer ptr and chooses the member function that matches the type of the pointer, as shown in Figure below. &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M7/pointeraccess.jpg" alt="pointeraccess.jpg" /&gt; &lt;/p&gt;&lt;p&gt;Sometimes this is what we want, but it doesn’t solve the problem posed at the beginning of this section: accessing objects of different classes using the same statement. &lt;/p&gt;&lt;p&gt;You can use reinterpret_cast to cast the pointer of it's type and use it. But that doesn't solve the problem if u have hundreds of derived class from the base class. &lt;/p&gt; &lt;a name="Virtual_Functions" id="Virtual_Functions"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Virtual Functions&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;Virtual means &lt;i&gt;existing in appearance but not in reality&lt;/i&gt;. When virtual functions are used, a program that appears to be calling a function of one class may in reality be calling a function of a different class. Why are virtual functions needed? Suppose you have a number of objects of different classes but you want to put them all in an array and perform a particular operation on them using the same function call. For example, suppose a geometry program includes several different shapes: a triangle, a circle, a square, and so on. Each of these classes has a member function &lt;i&gt;getArea()&lt;/i&gt; that returns the area of the object. &lt;/p&gt;&lt;p&gt;Now suppose you plan to calculate the area by grouping a number of these elements together, and you want to calculate in a convenient way. One approach is to create an array that holds pointers to all the different objects. The array might be defined like this. &lt;/p&gt;&lt;p&gt;shape *ptrarr[100]; //array of 100 pointers to shapes &lt;/p&gt;&lt;p&gt;If you insert pointers to all the shapes into this array, you can then calculate area using a simple loop: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt; &lt;span class="kw1"&gt;for&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; j&lt;span class="sy1"&gt;=&lt;/span&gt;&lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy1"&gt;&lt;&lt;/span&gt;N&lt;span class="sy4"&gt;;&lt;/span&gt; j&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; ptrarr&lt;span class="br0"&gt;[&lt;/span&gt;j&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;getArea&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;This is an amazing capability: Completely different functions are executed by the same function call. If the pointer in &lt;i&gt;ptrarr&lt;/i&gt; points to a square, the square function that returns area is called; if it points to a triangle, the triangle area function is called. This is called &lt;b&gt;polymorphism&lt;/b&gt;, which means different forms. The functions have the same functionality, the getArea(), but different actual functions are called, depending on the contents of ptrarr[j]. Polymorphism is one of the key features of object-oriented programming, after classes and inheritance. &lt;/p&gt;&lt;p&gt;For the polymorphic approach to work, several conditions must be met.  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;First, all the different classes of shapes, such as circle and triangles, must be descended from a single base class (called shape in Geometry). &lt;/li&gt;&lt;li&gt;Second, the getArea() function must be declared to be virtual in the base class. &lt;/li&gt;&lt;/ul&gt; &lt;a name="Virtual_Member_Functions_Accessed_with_Pointers" id="Virtual_Member_Functions_Accessed_with_Pointers"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Virtual Member Functions Accessed with Pointers&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;Let’s make a single change in our program (BASEPOINT): We’ll place the keyword virtual in front of the declarator for the show() function in the base class. Here’s the listing for the resulting program,VIRTPOINT: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// virtpoint.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// virtual functions accessed from pointer&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Base &lt;span class="co1"&gt;//base class&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;      &lt;span class="kw2"&gt;virtual&lt;/span&gt; &lt;span class="kw4"&gt;void&lt;/span&gt; show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"Base&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Derv1 &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; Base &lt;span class="co1"&gt;//derived class 1&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;      &lt;span class="kw4"&gt;void&lt;/span&gt; show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"Derv1&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Derv2 &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; Base &lt;span class="co1"&gt;//derived class 2&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;      &lt;span class="kw4"&gt;void&lt;/span&gt; show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;           &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"Derv2&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;Derv1 dv1&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//object of derived class 1&lt;/span&gt;&lt;br /&gt;Derv2 dv2&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//object of derived class 2&lt;/span&gt;&lt;br /&gt;Derv1 &lt;span class="sy2"&gt;*&lt;/span&gt;pdv1&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;Derv2 &lt;span class="sy2"&gt;*&lt;/span&gt;pdv2&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Base&lt;span class="sy2"&gt;*&lt;/span&gt; ptr&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//pointer to base class&lt;/span&gt;&lt;br /&gt;ptr &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;dv1&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//put address of dv1 in pointer&lt;/span&gt;&lt;br /&gt;ptr&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//execute show()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;ptr &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;dv2&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//put address of dv2 in pointer&lt;/span&gt;&lt;br /&gt;ptr&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//execute show()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;ptr &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;dv1&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//put address of dv1 in pointer&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw2"&gt;dynamic_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;Derv1 &lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;ptr&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="sy3"&gt;!&lt;/span&gt;&lt;span class="sy1"&gt;=&lt;/span&gt;0&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;  pdv1 &lt;span class="sy1"&gt;=&lt;/span&gt;  &lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;Derv1 &lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;ptr&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  pdv1&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;ptr &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;dv2&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//put address of dv2 in pointer&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;if&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw2"&gt;dynamic_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;Derv2 &lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;ptr&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="sy3"&gt;!&lt;/span&gt;&lt;span class="sy1"&gt;=&lt;/span&gt;0&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;  pdv2 &lt;span class="sy1"&gt;=&lt;/span&gt;  &lt;span class="kw2"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;/span&gt;Derv2 &lt;span class="sy2"&gt;*&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;ptr&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  pdv2&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw3"&gt;system&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;"PAUSE"&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;The output of this program is &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Derv1 &lt;/li&gt;&lt;li&gt;Derv2 &lt;/li&gt;&lt;li&gt;Derv1 &lt;/li&gt;&lt;li&gt;Derv2 &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Now, as you can see, the member functions of the derived classes, not the base class, are executed. We change the contents of ptr from the address of Derv1 to that of Derv2, and the particular instance of show() that is executed also changes. So the same function call &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;ptr&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;executes different functions, depending on the contents of ptr. The rule is that the compiler selects the function based on the contents of the pointer ptr, not on the type of the pointer, as in BASEPOINT. This is shown in Figure below. &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M7/vpointeraccess.jpg" alt="vpointeraccess.jpg" /&gt; &lt;/p&gt;&lt;p&gt;You can use &lt;b&gt;dynamic_cast&lt;/b&gt; operator to cast to a particular derived type for the polymorphic classes. &lt;/p&gt; &lt;a name="Late_Binding" id="Late_Binding"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Late Binding&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;The astute reader may wonder how the compiler knows what function to compile. In BASEPOINT the compiler has no problem with the expression &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;ptr&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;It always compiles a call to the show() function in the base class. But in VIRTPOINT the compiler doesn’t know what class the contents of ptr may contain. It could be the address of an object of the Derv1 class or of the Derv2 class. Which version of draw() does the compiler call? In fact the compiler doesn’t know what to do, so it arranges for the decision to be deferred until the program is running. At runtime, when it is known what class is pointed to by ptr, the appropriate version of draw will be called. This is called &lt;b&gt;late binding or dynamic binding&lt;/b&gt;.(Choosing functions in the normal way, during compilation, is called early binding or static binding.) Late binding requires some overhead but provides increased power and flexibility. &lt;/p&gt;&lt;p&gt;We’ll put these ideas to use in a moment, but first let’s consider a refinement to the idea of virtual functions. &lt;/p&gt; &lt;a name="Abstract_Classes_and_Pure_Virtual_Functions" id="Abstract_Classes_and_Pure_Virtual_Functions"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Abstract Classes and Pure Virtual Functions&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;Think of the shape class. We’ll never make an object of the shape class; we’ll only make specific shapes such as circles and triangles. When we will never want to instantiate objects of a base class, we call it an &lt;b&gt;abstract class&lt;/b&gt;. Such a class exists only to act as a parent of derived classes that will be used to instantiate objects. It may also provide an interface for the class hierarchy. &lt;/p&gt;&lt;p&gt;How can we make it clear to someone using our family of classes that we don’t want anyone to instantiate objects of the base class? We could just say this in the documentation, and count on the users of the class to remember it, but of course it’s much better to write our classes so that such instantiation is impossible. How can we can do that? By placing at least one pure virtual function in the base class. A pure virtual function is one with the expression =0 added to the declaration. This is shown in the VIRTPURE example. &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co1"&gt;// virtpure.cpp&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// pure virtual function&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Base &lt;span class="co1"&gt;//base class&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt; &lt;span class="kw2"&gt;virtual&lt;/span&gt; &lt;span class="kw4"&gt;void&lt;/span&gt; show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//pure virtual function&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Derv1 &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; Base &lt;span class="co1"&gt;//derived class 1&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt; &lt;span class="kw4"&gt;void&lt;/span&gt; show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “Derv1\n”&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Derv2 &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; Base &lt;span class="co1"&gt;//derived class 2&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt; &lt;span class="kw4"&gt;void&lt;/span&gt; show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt; &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; “Derv2\n”&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// Base bad; //can’t make object from abstract class&lt;/span&gt;&lt;br /&gt;Base&lt;span class="sy2"&gt;*&lt;/span&gt; arr&lt;span class="br0"&gt;[&lt;/span&gt;2&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//array of pointers to base class&lt;/span&gt;&lt;br /&gt;Derv1 dv1&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//object of derived class 1&lt;/span&gt;&lt;br /&gt;Derv2 dv2&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//object of derived class 2&lt;/span&gt;&lt;br /&gt;arr&lt;span class="br0"&gt;[&lt;/span&gt;0&lt;span class="br0"&gt;]&lt;/span&gt; &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;dv1&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//put address of dv1 in array&lt;/span&gt;&lt;br /&gt;arr&lt;span class="br0"&gt;[&lt;/span&gt;1&lt;span class="br0"&gt;]&lt;/span&gt; &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;dv2&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//put address of dv2 in array&lt;/span&gt;&lt;br /&gt;arr&lt;span class="br0"&gt;[&lt;/span&gt;0&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//execute show() in both objects&lt;/span&gt;&lt;br /&gt;arr&lt;span class="br0"&gt;[&lt;/span&gt;1&lt;span class="br0"&gt;]&lt;/span&gt;&lt;span class="sy2"&gt;-&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&lt;/span&gt;show&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;Here the virtual function show() is declared as &lt;/p&gt;&lt;p&gt;virtual void show() = 0; // pure virtual function &lt;/p&gt;&lt;p&gt;The equal sign here has nothing to do with assignment; the value 0 is not assigned to anything. The =0 syntax is simply how we tell the compiler that a virtual function will be pure. Now if in main() you attempt to create objects of class Base, the compiler will complain that you’re trying to instantiate an object of an abstract class. It will also tell you the name of the pure virtual function that makes it an abstract class. Notice that, although this is only a declaration, you never need to write a definition of the base class show(), although you can if you need to. &lt;/p&gt;&lt;p&gt;Once you’ve placed a pure virtual function in the base class, you must override it in all the derived classes from which you want to instantiate objects. If a class doesn’t override the pure virtual function, it becomes an abstract class itself, and you can’t instantiate objects from it (although you might from classes derived from it). For consistency, you may want to make all the virtual functions in the base class pure. As you can see, we’ve made another, unrelated, change in VIRTPURE: The addresses of the member functions are stored in an array of pointers and accessed using array elements. This works in just the same way as using a single pointer. The output of VIRTPURE is the same as &lt;/p&gt;&lt;p&gt;VIRT: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Derv1 &lt;/li&gt;&lt;li&gt;Derv2 &lt;/li&gt;&lt;/ul&gt; &lt;a name="Virtual_Destructors" id="Virtual_Destructors"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt;Virtual Destructors&lt;/span&gt;&lt;/h4&gt; &lt;p&gt;Base class &lt;i&gt;destructors&lt;/i&gt; should always be &lt;b&gt;virtual&lt;/b&gt;. Suppose you use &lt;b&gt;delete&lt;/b&gt; with a &lt;b&gt;base class pointer&lt;/b&gt; to a &lt;b&gt;derived class object&lt;/b&gt; to &lt;b&gt;destroy the derived-class object&lt;/b&gt;. If the base-class destructor is &lt;b&gt;not virtual&lt;/b&gt; then delete, like a normal member function, calls the destructor for the base class, &lt;b&gt;not&lt;/b&gt; the destructor for the derived class. &lt;/p&gt; &lt;a name="Points_to_remember" id="Points_to_remember"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Points to remember&lt;/span&gt;&lt;/h3&gt; &lt;ul&gt;&lt;li&gt;The process of resolving a function call to an object is called binding &lt;/li&gt;&lt;li&gt;Associating an object to a function call at run-time is referred to as late binding. &lt;/li&gt;&lt;li&gt;A pointer of a base class can point to an object of any of its derived class. &lt;/li&gt;&lt;li&gt;The virtual keyword is used to declare a function as virtual. &lt;/li&gt;&lt;li&gt;Virtual functions that have no body are called pure virtual functions. &lt;/li&gt;&lt;li&gt;A class containing one or more pure virtual functions is called an abstract class. &lt;/li&gt;&lt;li&gt;Instances of an abstract class cannot be created but pointers to it can created. &lt;/li&gt;&lt;li&gt;Static polymorphism involves binding of a function on the basis of the number, type and sequence of its arguments. &lt;/li&gt;&lt;li&gt;Function overloading is an example of static polymorphism because functions can be bound to the calls at compile time. &lt;/li&gt;&lt;li&gt;A function exhibits dynamic polymorphism when it exists in more than one form, and calls to its various forms are resolved dynamically when the program executed. &lt;/li&gt;&lt;li&gt;Static binding is considered to be more efficient while dynamic binding is more flexible. &lt;/li&gt;&lt;/ul&gt; &lt;a name="Steps_to_attack_the_problem_sets" id="Steps_to_attack_the_problem_sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Steps to attack the problem sets  &lt;/span&gt;&lt;/h2&gt; &lt;ul&gt;&lt;li&gt;Step 1: Read the &lt;a href="http://wiki.msitprogram.net/index.php/CPP09:W1-M7#Introduction" title="CPP09:W1-M7"&gt;Introduction&lt;/a&gt; and try sample programs given in the introduction(You can copy paste the programs) &lt;/li&gt;&lt;li&gt;Step 2: Go through the given PPT on Polymorphism and try sample problems given in the PPT.(you can copy paste the programs from the PPT)&lt;br /&gt; &lt;/li&gt;&lt;li&gt;Step 3: Go through the additional resources give for further clarification.  &lt;/li&gt;&lt;li&gt;Step 4: Attack the problem sets in the given order(Problem Set A and then Problem Set B).  &lt;/li&gt;&lt;li&gt;Step 5: If you are not clear with the problem sets then contact your respective mentor for clarification. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Spend at least 2-3 hours from Step 1 to Step 3. &lt;/p&gt;&lt;p&gt;Don't get confused with the example given in PPT and in the introduction part of this Module for shapes. In PPT, Shapes are used with respect to point in a plane and in introduction part shapes is used with respect to Geometry(area, sides, circumference etc). &lt;/p&gt; &lt;a name="Resources" id="Resources"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Resources&lt;/span&gt;&lt;/h2&gt; &lt;p&gt;PPT: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://wiki.msitprogram.net/CPP09/W1/M7/cpphtp4_PPT_10.ppt" class="external text" title="http://wiki.msitprogram.net/CPP09/W1/M7/cpphtp4_PPT_10.ppt" rel="nofollow"&gt;Object-Oriented Programming: Polymorphism&lt;/a&gt; -- '&lt;b&gt;How to C++ by Ditel and Associates'&lt;/b&gt; &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;References: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/doc/tutorial/polymorphism/" class="external text" title="http://www.cplusplus.com/doc/tutorial/polymorphism/" rel="nofollow"&gt;Polymorphism -- C Plus Plus .com&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=/com.ibm.vacpp7l.doc/language/ref/clrc05keyword_dynamic_cast.htm" class="external text" title="http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=/com.ibm.vacpp7l.doc/language/ref/clrc05keyword_dynamic_cast.htm" rel="nofollow"&gt;Reinterpret_Cast and Dynamic_Cast&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;br /&gt;&lt;/p&gt; &lt;a name="Problem_Sets" id="Problem_Sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Problem Sets&lt;/span&gt;&lt;/h2&gt; &lt;a name="Problem_Set_A" id="Problem_Set_A"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set A&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt; Create a set of classes of Shapes (triangle, quadrilateral, circle) with the following inheritance relationship. &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M7/shapes.jpg" alt="shapes.jpg" /&gt; &lt;/p&gt;&lt;p&gt;Declare and define the classes as &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Shapes-- Abstract base class. &lt;ul&gt;&lt;li&gt;Triangle -- Abstract derived class with base class as Shapes &lt;ul&gt;&lt;li&gt;IsoscelesTriangle -- derived class with base class as Triangle.(Attribute with two sides) &lt;/li&gt;&lt;li&gt;EquilateralTriangle -- derived class with base class as Triangle(Attribute with one side) &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;Quadrilateral --Abstract derived class with base class as Shapes. &lt;ul&gt;&lt;li&gt;Rectangle -- derived class with base class as Quadrilateral.(Attribute with two sides-- length and breadth) &lt;/li&gt;&lt;li&gt;Square -- derived class with base class as Quadrilateral.(Attribute with one side) &lt;/li&gt;&lt;li&gt;Rhombus -- derived class with base class as Quadrilateral. (Attribute with two sides-- length and breadth) &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;Circle -- Derived class with base class as Shapes. (Attribute with radius) &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Use the following declarations for the abstract class and the test main function. Write the code where ever necessary. &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#include &lt;iostream&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Shapes &lt;span class="co1"&gt;//Abstract base class&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;      &lt;span class="kw2"&gt;virtual&lt;/span&gt; &lt;span class="kw4"&gt;double&lt;/span&gt; getArea&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;=&lt;/span&gt;&lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//Calculates the Area and returns the area&lt;/span&gt;&lt;br /&gt;      &lt;span class="kw2"&gt;virtual&lt;/span&gt; &lt;span class="kw4"&gt;double&lt;/span&gt; getPerimeter&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;=&lt;/span&gt;&lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//Calculates the perimeter and return the perimeter.&lt;/span&gt;&lt;br /&gt;      &lt;span class="kw2"&gt;virtual&lt;/span&gt; &lt;span class="kw4"&gt;void&lt;/span&gt; getData&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy1"&gt;=&lt;/span&gt;&lt;span class="nu0"&gt;0&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;//Take input from the user and store in the state of the object.&lt;/span&gt;&lt;br /&gt;      &lt;span class="kw2"&gt;virtual&lt;/span&gt; ~Shapes&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Triangle &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; Shapes  &lt;span class="co1"&gt;//Derived Abstract class&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//Write your code here&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Quadrilateral &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; Shapes &lt;span class="co1"&gt;//Derived Abstract class&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//Write your code here&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;//Write your code here to declare and define other classes stated&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;//Write your code here to declare an array pointer of Shapes[6].&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;//Write a Loop for the Menu to create an instance of the object &lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;// andget the input from the user for the selected Shape &lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//and add the pointer to the Shapes array.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="co1"&gt;//Write a Loop to Print the Area and Perimeter of the array shapes.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span class="kw3"&gt;system&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;"PAUSE"&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;       &lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;Display a Menu Interface to the user to Create an instance of the Object. The Menu should be like. &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Create an Instance of &lt;ul&gt;&lt;li&gt;Triangle &lt;ul&gt;&lt;li&gt;Create IsoscelesTriangle &lt;/li&gt;&lt;li&gt;Create EquilateralTriangle &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;Quadrilateral &lt;ul&gt;&lt;li&gt;Create Rectangle &lt;/li&gt;&lt;li&gt;Create Square &lt;/li&gt;&lt;li&gt;Create Rhombus &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;Create Circle &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;br /&gt;Create should create an instance of the derived class selected and then call the getData() function of that class and store that instance in the array of Shapes(Base Class). &lt;/p&gt;&lt;p&gt;&lt;b&gt;You can add constructors for the derived classes with default values&lt;/b&gt;. &lt;/p&gt;&lt;p&gt;Write a test main function to take the input from the user for all the shapes and store them in a array, then print the area and perimeter of the class. &lt;/p&gt;&lt;p&gt;Refer: &lt;a href="http://www.math.com/tables/geometry/index.htm" class="external free" title="http://www.math.com/tables/geometry/index.htm" rel="nofollow"&gt;http://www.math.com/tables/geometry/index.htm&lt;/a&gt; &lt;/p&gt;&lt;p&gt;&lt;b&gt;All the measurements is in Centimeters&lt;/b&gt; &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-6889684806364407112?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Q7iNcjqgZUsX8fYGuqMFar5MlE4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Q7iNcjqgZUsX8fYGuqMFar5MlE4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Q7iNcjqgZUsX8fYGuqMFar5MlE4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Q7iNcjqgZUsX8fYGuqMFar5MlE4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/pZVD0hwO20Q" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/6889684806364407112/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/polymorphism.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/6889684806364407112?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/6889684806364407112?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/pZVD0hwO20Q/polymorphism.html" title="Polymorphism" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/polymorphism.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ck4BR3wzfyp7ImA9WxNXFEw.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-3625302085448054805</id><published>2009-10-01T08:27:00.000-07:00</published><updated>2009-10-01T08:29:16.287-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-01T08:29:16.287-07:00</app:edited><title>Multiple Inheritance</title><content type="html">&lt;h2&gt; &lt;span class="mw-headline"&gt; Learning Objectives  &lt;/span&gt;&lt;/h2&gt; &lt;p&gt;At the end of this Module, you will be able to:  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Understand the concept of multiple inheritance  &lt;/li&gt;&lt;li&gt;Identify and solve the ambiguities that arise in multiple inheritance.  &lt;/li&gt;&lt;li&gt;Define Virtual base class  &lt;/li&gt;&lt;li&gt;State the order of invocation of constructors and destructors &lt;/li&gt;&lt;/ul&gt; &lt;a name="Introduction" id="Introduction"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Introduction  &lt;/span&gt;&lt;/h2&gt; &lt;a name="Multiple_Inheritance" id="Multiple_Inheritance"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Multiple Inheritance  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;Multiple Inheritance&lt;/b&gt; is the concept where a subclass inherits properties from multiple base classes. A familiar example of multiple inheritance is the child inheriting the characteristics of the parents. &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M6/father_mother.jpg" alt="father_mother.jpg" /&gt;  &lt;/p&gt;&lt;p&gt;You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance. &lt;/p&gt;&lt;p&gt;In the following example, classes A, B, and C are direct base classes for the derived class X:  &lt;/p&gt;&lt;p&gt;The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; A &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; B &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; C &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; X &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; A, &lt;span class="kw2"&gt;private&lt;/span&gt; B, &lt;span class="kw2"&gt;public&lt;/span&gt; C &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M6/abc_x.jpg" alt="abc_x.jpg" /&gt;  &lt;/p&gt; &lt;a name="Ambiguities_in_Multiple_Inheritance" id="Ambiguities_in_Multiple_Inheritance"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Ambiguities in Multiple Inheritance  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;When a class inherits from multiple base classes, a whole lot of ambiguities creep in. For example , what happens when two base classes contain a function of the same name? &lt;/p&gt;&lt;p&gt;For example given below --&gt; Class A , Class B has Member functions disp() &lt;b&gt;(to display the attributes of the class)&lt;/b&gt; and Class C inherits A and B.  &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; A &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw4"&gt;void&lt;/span&gt; disp&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;void&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; B &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt;&lt;br /&gt; &lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw4"&gt;void&lt;/span&gt; disp&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;void&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; C &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; A, &lt;span class="kw2"&gt;private&lt;/span&gt; B &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt; C c&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt; C.&lt;span class="me1"&gt;disp&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;span class="co1"&gt;//Ambiguous&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;Here the reference to disp() is ambiguous because the compiler does not know whether disp() refers to the member in class base1 or base2. This ambiguity can be resolved using the scope resolution operator as illustrated hereunder. &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; C &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; A, &lt;span class="kw2"&gt;private&lt;/span&gt; B &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;   &lt;span class="kw4"&gt;void&lt;/span&gt; disp&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;void&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;      A.&lt;span class="me1"&gt;disp&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;      B.&lt;span class="me1"&gt;disp&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;   &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt; C c&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt; c.&lt;span class="me1"&gt;disp&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  This ambiguity can also be resolved by overriding as illustrated hereunder. &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt; C c&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt; c.&lt;span class="me1"&gt;A&lt;/span&gt;&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;disp&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt; c.&lt;span class="me1"&gt;B&lt;/span&gt;.&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;disp&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;Another &lt;b&gt;ambiguity that arises in multiple inheritance&lt;/b&gt; is the possibility of the derived class having &lt;b&gt;multiple copies of the same base class&lt;/b&gt;. Consider the following diagram.  &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M6/bl_d.jpg" alt="bl_d.jpg" /&gt;  &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; L &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;                  &lt;span class="co1"&gt;// indirect base class&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; B2 &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; L &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; B3 &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; L &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; D &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; B2, &lt;span class="kw2"&gt;public&lt;/span&gt; B3 &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;// valid&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;In the above example, class D inherits the indirect base class L once through class B2 and once through class B3. However, this may lead to ambiguities because two objects of class L exist, and both are accessible through class D. You can avoid this ambiguity by referring to class L using a qualified class name. For example: &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;B2&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;L&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;or  &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;B3&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;L&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;You can also avoid this ambiguity by using the base specifier virtual to declare a base class.  &lt;/p&gt; &lt;a name="Virtual_Base_Classes" id="Virtual_Base_Classes"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Virtual Base Classes  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;The principle behind virtual base classes is very simple. When the same class is inherited more than once via multiple paths, multiple copies of the base class members are created in memory. By declaring the base class inheritance as &lt;b&gt;virtual&lt;/b&gt;, only one copy of the base class is inherited. A base class inheritance can be specified as virtual using &lt;b&gt;virutal qualifier&lt;/b&gt;.  &lt;/p&gt;&lt;p&gt;The class definitions would be modified as follows  &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; L &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;// indirect base class&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; B1 &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;virtual&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; L &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; B2 &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;virtual&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; L &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; D &lt;span class="sy4"&gt;:&lt;/span&gt; &lt;span class="kw2"&gt;public&lt;/span&gt; B1, &lt;span class="kw2"&gt;public&lt;/span&gt; B2 &lt;span class="br0"&gt;{&lt;/span&gt; &lt;span class="coMULTI"&gt;/* ... */&lt;/span&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt; &lt;span class="co1"&gt;// valid&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  &lt;a name="Invocation_of_Constructors_and_Destructors" id="Invocation_of_Constructors_and_Destructors"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Invocation of Constructors and Destructors  &lt;/span&gt;&lt;/h3&gt; &lt;ul&gt;&lt;li&gt;Constructors are invoked in the following order:  &lt;ul&gt;&lt;li&gt;Virtual base class constructors -- in the order of inheritance.  &lt;/li&gt;&lt;li&gt;Non-Virtual base class constructors -- in the order of inheritance.  &lt;/li&gt;&lt;li&gt;Member objects constructors -- in the order of declaration  &lt;/li&gt;&lt;li&gt;Derived class constructor.  &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;Destructor are invoked in the reverse order &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;br /&gt; &lt;/p&gt; &lt;a name="Resources" id="Resources"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Resources  &lt;/span&gt;&lt;/h2&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr134.htm" class="external text" title="http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr134.htm" rel="nofollow"&gt;Multiple Inheritance&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;br /&gt;&lt;/p&gt; &lt;a name="Problem_Sets" id="Problem_Sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Problem Sets  &lt;/span&gt;&lt;/h2&gt; &lt;a name="Problem_Set_A" id="Problem_Set_A"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Problem Set A  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1.&lt;/b&gt; Create a set of classes named &lt;b&gt;A, B, C, D, E, F, &amp;amp; G&lt;/b&gt; with the following inheritance relationship:  &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M6/pa1.jpg" alt="pa1.jpg" /&gt;  &lt;/p&gt;&lt;p&gt;Give each class a constructor and destructor only that print simple trace messages to the screen('&lt;i&gt;like&lt;/i&gt; Constructor A or Destructor A ). &lt;b&gt;Use non-virtual inheritance&lt;/b&gt;. Test each of the classes individually by creating objects of each type in a Test Program. Study the constructor and destructor trace messages and submit the trace. &lt;/p&gt;&lt;p&gt;&lt;br /&gt;The Trace document should contain the trace output for each of the classes individually by creating objects of each type.  &lt;/p&gt; &lt;pre&gt;Name the program as: PA1_NonVirtualInheritance.cpp&lt;br /&gt;Name the Trace Document as: PA1_NonVirtualInheritance.doc&lt;br /&gt;&lt;/pre&gt; &lt;a name="Problem_Set_B" id="Problem_Set_B"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Problem Set B  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1.&lt;/b&gt; Create a set of classes named &lt;b&gt;A, B, C, D, E, F, &amp;amp; G&lt;/b&gt; with the following inheritance relationship:  &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M6/pa1.jpg" alt="pa1.jpg" /&gt;  &lt;/p&gt;&lt;p&gt;Give each class a constructor and destructor only that print simple trace messages to the screen('&lt;i&gt;like&lt;/i&gt; Constructor A or Destructor A ). &lt;b&gt;Use virtual qualifier in the inheritance&lt;/b&gt; wherever necessary. Test each of the classes individually by creating objects of each type in a Test Program. Study the constructor and destructor trace messages and submit the trace. &lt;/p&gt;&lt;p&gt;&lt;br /&gt;The Trace document should contain the trace output for each of the classes individually by creating objects of each type.  &lt;/p&gt; &lt;pre&gt;Name the program as: PB1_VirtualInheritance.cpp&lt;br /&gt;Name the Trace Document as: PB1_VirtualInheritance.doc&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;&lt;br /&gt; &lt;/p&gt; &lt;a name="Problem_Set_C" id="Problem_Set_C"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Problem Set C  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt; &lt;b&gt;Thomson&lt;/b&gt; publishing company markets both books and video-cassette. Create a class called &lt;b&gt;Publication&lt;/b&gt; that stores the &lt;i&gt;title&lt;/i&gt;(string) and &lt;i&gt;price&lt;/i&gt;(float) of a publication. From this class derive two classes: &lt;b&gt;Book&lt;/b&gt; which adds a page &lt;b&gt;count&lt;/b&gt; (type int); and &lt;b&gt;VideoCassette&lt;/b&gt;, which adds a &lt;i&gt;&lt;b&gt;playing time&lt;/b&gt;&lt;/i&gt; (type int) in minutes. Each of these classes should have a &lt;i&gt;&lt;b&gt;getdata()&lt;/b&gt;&lt;/i&gt; function to get its data from the user and a &lt;i&gt;&lt;b&gt;putdata()&lt;/b&gt;&lt;/i&gt;function to display its data.  &lt;/p&gt;&lt;p&gt;Add another base class &lt;b&gt;Sales&lt;/b&gt; that holds an array of &lt;b&gt;three floats&lt;/b&gt; so that it can record the sales of a particular publication for the last three months. Include a &lt;b&gt;getdata()&lt;/b&gt; function to get three sales amounts from the user, and a &lt;b&gt;putdata()&lt;/b&gt; function to display the sales figures. Alter the &lt;b&gt;book &lt;/b&gt;and&lt;b&gt;tape&lt;/b&gt; classes so that they are derived from both &lt;b&gt;Publication&lt;/b&gt; and &lt;b&gt;Sales&lt;/b&gt;. An object of class book or tape should input or output sales along with its other data. Write a &lt;b&gt;main()&lt;/b&gt; function to create a book object and a tape object and which will update their corresponding member data along with the sales made. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-3625302085448054805?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/K36VYx5K72nDK1NtMHMSj_RpLNg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/K36VYx5K72nDK1NtMHMSj_RpLNg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/K36VYx5K72nDK1NtMHMSj_RpLNg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/K36VYx5K72nDK1NtMHMSj_RpLNg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/syLEYhVQxyk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/3625302085448054805/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/multiple-inheritance.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/3625302085448054805?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/3625302085448054805?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/syLEYhVQxyk/multiple-inheritance.html" title="Multiple Inheritance" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/multiple-inheritance.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ck8AQHozcCp7ImA9WxNXFEw.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-1733792704967239298</id><published>2009-10-01T08:14:00.000-07:00</published><updated>2009-10-01T08:27:21.488-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-01T08:27:21.488-07:00</app:edited><title>Inheritance</title><content type="html">&lt;h1&gt; &lt;span class="mw-headline"&gt; Week 1 - M5 : Inheritance  &lt;/span&gt;&lt;/h1&gt; &lt;a name="Learning_Objectives" id="Learning_Objectives"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Learning Objectives  &lt;/span&gt;&lt;/h2&gt; &lt;p&gt;At the end of this module, you will be able to  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Understand the implementation of inheritance in C++  &lt;/li&gt;&lt;li&gt;Override the base class members  &lt;/li&gt;&lt;li&gt;Access overridden base class members using the scope resolution operator  &lt;/li&gt;&lt;li&gt;Understand the concept of base class initialization &lt;/li&gt;&lt;/ul&gt; &lt;a name="Introduction" id="Introduction"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Introduction  &lt;/span&gt;&lt;/h2&gt; &lt;a name="Inheritance" id="Inheritance"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Inheritance  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;The philosophy behind inheritance is to portray things as they exist in the real world. As inheritance is found in real world, it is important feature of OO programming. Inheritance has many advantages, the most important of them being the &lt;i&gt;resuability&lt;/i&gt; of code. Once a class is defined and debugged, it can be used to create new subclasses. The reuse of existing class saves time and effort. &lt;/p&gt;&lt;p&gt;The Class from which another class is derived is called the &lt;b&gt;base class&lt;/b&gt;. The class which inherits the properties of the base class is called the &lt;b&gt;derived class&lt;/b&gt;. Each instance of the derived class includes all the members of the base class. Since the derived class inherits all properties of the base class, the derived class has a larger set of properties than its base class. However, the derived class may override some or all the properties of the base class. &lt;/p&gt;&lt;p&gt;Any class can be a base class. More than one class can be derived from a single base class, and a derived class can be a base class to another. &lt;/p&gt;&lt;p&gt;if class d1 is inherited from class b1, then the syntax for declaring class derived is  &lt;/p&gt;&lt;p&gt;class d1::public b1  &lt;/p&gt;&lt;p&gt;An object defined outside the class can access only the public members of the class. Therefore, private members of a class cannot be directly accessed from outside the class. This holds true even for a derived class. Even though the derived class inherits all the members data and functions from the base class, private members of the base class are not directly accessible from the derived class. The idea behind this is never to compromise on encapsulation implemented through data hiding. &lt;/p&gt;&lt;p&gt;The protected members of a class can be accessed by its members functions, or within any class derived from it. Protected members behave like public members with respect to the derived class and like private members with respect to the rest of the program. &lt;/p&gt; &lt;a name="Overriding_Base_Class_Members" id="Overriding_Base_Class_Members"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt; Overriding Base Class Members  &lt;/span&gt;&lt;/h4&gt; &lt;p&gt;A base class members can be overridden by defining a derived class member with the same name as that of the base class member.  &lt;/p&gt;&lt;p&gt;If the function exists in both the derived class and the base class then.  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;If the function is invoked from an Object of the derived class, then the function in the derived class is executed.  &lt;/li&gt;&lt;li&gt;If the function is invoked from an Object of the base class, then the base class member function is invoked. &lt;/li&gt;&lt;/ul&gt; &lt;a name="Inheritance_Example_in_Real_World" id="Inheritance_Example_in_Real_World"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt; Inheritance Example in Real World  &lt;/span&gt;&lt;/h4&gt; &lt;p&gt;In a Banking System, Manager, Officers, Clerk have some common and as well as special behavior (specific tasks/actions). The commonality is that they are all employees of the Bank. A generalized class “Employee_GeneralizedClass” could be used to represent the commonality among Object classes. &lt;/p&gt;&lt;p&gt;The generalization is often referred to as inheritance, and the generalized class as parent class. inheritance is a means of specifying hierarchical relationships between object classes. &lt;/p&gt; &lt;a name="Points_to_Remember" id="Points_to_Remember"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt; Points to Remember  &lt;/span&gt;&lt;/h4&gt; &lt;ul&gt;&lt;li&gt;The class from which a subclass is derived is referred to as the &lt;i&gt;base class'.&lt;/i&gt;  &lt;/li&gt;&lt;li&gt;Base class constructors and destructors are not inherited by the derived class.  &lt;/li&gt;&lt;li&gt;Derived class constructors must explicitly call the base class constructors with appropriate parameters.  &lt;/li&gt;&lt;li&gt;Destructors are invoked in the reverse order. &lt;/li&gt;&lt;/ul&gt; &lt;a name="Resources" id="Resources"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Resources  &lt;/span&gt;&lt;/h2&gt; &lt;p&gt;Walk Through Video (here)&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-26bce90660a90f45" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;
&lt;param name="bgcolor" value="#FFFFFF"&gt;
&lt;param name="allowfullscreen" value="true"&gt;
&lt;param name="flashvars" value="flvurl=http://v16.nonxt2.googlevideo.com/videoplayback?id%3D26bce90660a90f45%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D4E0FBAD10D2F4D163A9B6B6F60A4E3DD807EBAF6.852FB5519D55872ED6D59A4B90D5B128DBFA717B%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D26bce90660a90f45%26offsetms%3D5000%26itag%3Dw160%26sigh%3DfQZoVq5-HHc91IjndRIGr5vQfzI&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;
&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"
width="320" height="266" bgcolor="#FFFFFF"
flashvars="flvurl=http://v16.nonxt2.googlevideo.com/videoplayback?id%3D26bce90660a90f45%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D4E0FBAD10D2F4D163A9B6B6F60A4E3DD807EBAF6.852FB5519D55872ED6D59A4B90D5B128DBFA717B%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D26bce90660a90f45%26offsetms%3D5000%26itag%3Dw160%26sigh%3DfQZoVq5-HHc91IjndRIGr5vQfzI&amp;autoplay=0&amp;ps=blogger"
allowFullScreen="true" /&gt;&lt;/object&gt;
&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Inheritance:  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/doc/tutorial/inheritance.html" class="external free" title="http://www.cplusplus.com/doc/tutorial/inheritance.html" rel="nofollow"&gt;http://www.cplusplus.com/doc/tutorial/inheritance.html&lt;/a&gt;  &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.csse.monash.edu.au/%7Ejonmc/CSE2305/Topics/05.09.Inheritance/html/text.html#inheritance_in_c" class="external free" title="http://www.csse.monash.edu.au/~jonmc/CSE2305/Topics/05.09.Inheritance/html/text.html#inheritance_in_c" rel="nofollow"&gt;http://www.csse.monash.edu.au/~jonmc/CSE2305/Topics/05.09.Inheritance/html/text.html#inheritance_in_c&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Protected:  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.6" class="external free" title="http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.6" rel="nofollow"&gt;http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.6&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt; &lt;a name="Steps_to_attack_the_problem_sets" id="Steps_to_attack_the_problem_sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Steps to attack the problem sets  &lt;/span&gt;&lt;/h2&gt; &lt;ul&gt;&lt;li&gt;Step 1: Read the &lt;a href="http://wiki.msitprogram.net/index.php/CPP09:W1-M5#Introduction" title="CPP09:W1-M5"&gt;Introduction&lt;/a&gt;  &lt;/li&gt;&lt;li&gt;Step 2: Go through the Videos given in the &lt;a href="http://wiki.msitprogram.net/index.php/CPP09:W1-M5#Resources" title="CPP09:W1-M5"&gt;Resources&lt;/a&gt;.  &lt;/li&gt;&lt;li&gt;Step 3: Go through the given PPT on Inheritance and try sample problems given in the PPT.(you can copy paste the programs from the PPT)&lt;br /&gt; &lt;/li&gt;&lt;li&gt;Step 4: Go through the additional resources give for further clarification.  &lt;/li&gt;&lt;li&gt;Step 5: Attack the problem sets in the given order(Problem Set A and then Problem Set B).  &lt;/li&gt;&lt;li&gt;Step 6: If you are not clear with the problem sets then contact your respective mentor for clarification. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Spend at least 2 hours from Step 1 to Step 4.  &lt;/p&gt; &lt;a name="Problem_Sets" id="Problem_Sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Problem Sets  &lt;/span&gt;&lt;/h2&gt; &lt;a name="Problem_Set_A" id="Problem_Set_A"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Problem Set A  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt; Draw an inheritance hierarchy for classes &lt;b&gt;Quadrilateral&lt;/b&gt;, &lt;i&gt;TRapezoid&lt;/i&gt;, &lt;i&gt;Parallelogram&lt;/i&gt;, &lt;i&gt;Rectangle&lt;/i&gt; and &lt;i&gt;Square&lt;/i&gt;. Use &lt;b&gt;Quadrilateral&lt;/b&gt; as the base class of the hierarchy. Make the hierarchy as deep as possible.  &lt;/p&gt;&lt;p&gt;The diagrams should include the state and behavior for each class.  &lt;/p&gt;&lt;p&gt;Example Image for Inheritance is:  &lt;/p&gt;&lt;p&gt;&lt;img src="http://wiki.msitprogram.net/CPP09/W1/M5/InheritanceExample.jpg" alt="InheritanceExample.jpg" /&gt;  &lt;/p&gt; &lt;pre&gt;Name the document as : PA1_Quadrilateral.doc&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;Draw an inheritance hierarchy for classes &lt;b&gt;Shapes&lt;/b&gt;. &lt;i&gt;Circle, Square, Triangle and Quadrilateral&lt;/i&gt;(described in previous task). Use the Shapes as the base class for the hierarchy. Make the hierarchy as deep as possible.  &lt;/p&gt;&lt;p&gt;The diagrams should include the state and behavior for each class. Refer to the image given in PA1.  &lt;/p&gt;&lt;p&gt;Refer: To the PPT given in the resources for inheritance.  &lt;/p&gt; &lt;pre&gt;Name the document as : PA2_Shapes.doc&lt;br /&gt;&lt;/pre&gt; &lt;a name="Problem_Set_B" id="Problem_Set_B"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Problem Set B  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt; (&lt;b&gt;Account&lt;/b&gt; Inheritance Hierarchy) Create an inheritance hierarchy that a bank might use to represent customers' bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e., debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, for instance, charge a fee per transaction (i.e., credit or debit). Current accounts, on other hand, has overdraft(Negative balance) facility to withdraw the money.&lt;br /&gt; &lt;/p&gt;&lt;p&gt;Create an inheritance hierarchy containing base class &lt;b&gt;Account&lt;/b&gt; and derived classes &lt;b&gt;SavingsAccount&lt;/b&gt;&lt;span style="font-weight: bold;"&gt;, &lt;/span&gt;&lt;b&gt;CheckingAccount&lt;/b&gt; and &lt;b&gt;CurrentAccount&lt;/b&gt; that inherit from class &lt;b&gt;Account&lt;/b&gt;. Base class &lt;b&gt;Account&lt;/b&gt; should include &lt;b&gt;one data member&lt;/b&gt; of type double to represent the &lt;b&gt;account balance&lt;/b&gt;. The class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0.0. If not, the balance should be set to 0.0 and the constructor should display an error message, indicating that the initial balance was invalid. The class should provide three member functions. Member function &lt;b&gt;credit&lt;/b&gt; should add an amount to the current balance. Member function &lt;b&gt;debit&lt;/b&gt; should withdraw money from the Account and ensure that the debit amount does not exceed the Account's balance. If it does, the balance should be left unchanged and the function should print the message "Debit amount exceeded account balance." Member function &lt;b&gt;getBalance&lt;/b&gt; should return the current balance.  &lt;/p&gt;&lt;p&gt;Derived class &lt;b&gt;SavingsAccount&lt;/b&gt; should inherit the functionality of an &lt;b&gt;Account&lt;/b&gt;, but also include a &lt;b&gt;data member&lt;/b&gt; of type double indicating the &lt;b&gt;interest rate&lt;/b&gt; (percentage) assigned to the Account. &lt;b&gt;SavingsAccount's &lt;/b&gt;constructor should receive the initial balance, as well as an initial value for the &lt;b&gt;SavingsAccount's&lt;/b&gt; interest rate. &lt;b&gt;SavingsAccount&lt;/b&gt; should provide a &lt;b&gt;public&lt;/b&gt; member function &lt;b&gt;calculateInterest&lt;/b&gt; that returns a double indicating the amount of interest earned by an account. Member function &lt;b&gt;calculateInterest&lt;/b&gt; should determine this amount by multiplying the interest rate by the account balance. [Note: SavingsAccount should inherit member functions credit and debit as is without redefining them.] &lt;/p&gt;&lt;p&gt;Derived class &lt;b&gt;CheckingAccount&lt;/b&gt; should inherit from base class &lt;b&gt;Account&lt;/b&gt; and include an additional &lt;b&gt;data member&lt;/b&gt; of type double that represents the &lt;b&gt;fee charged per&lt;/b&gt; transaction. &lt;b&gt;CheckingAccount's&lt;/b&gt; constructor should receive the initial balance, as well as a parameter indicating a fee amount. Class &lt;b&gt;CheckingAccount&lt;/b&gt; should redefine member functions &lt;b&gt;credit&lt;/b&gt; and &lt;b&gt;debit&lt;/b&gt; so that they subtract the fee from the account balance whenever either transaction is performed successfully. &lt;b&gt;CheckingAccount's&lt;/b&gt; versions of these functions should invoke the base-class &lt;b&gt;Account&lt;/b&gt; version to perform the updates to an &lt;b&gt;account balance&lt;/b&gt;. &lt;b&gt;CheckingAccount's&lt;/b&gt; debit function should charge a fee only if money is actually withdrawn (i.e., the debit amount does not exceed the account balance). [Hint: Define Account's debit function so that it returns a bool indicating whether money was withdrawn. Then use the return value to determine whether a fee should be charged.] &lt;/p&gt;&lt;p&gt;Derived class &lt;b&gt;CurrentAccount&lt;/b&gt; should inherit the functionality of an &lt;b&gt;Account&lt;/b&gt;, but also include a &lt;b&gt;data member&lt;/b&gt; of type double indicating the &lt;b&gt;maximum overdraft limit&lt;/b&gt; (percentage) assigned to the Account. &lt;b&gt;CurrentAccount's &lt;/b&gt;constructor should receive the initial balance, as well as an initial value for the &lt;b&gt;CurrentAccount's&lt;/b&gt; maximum overdraft limit. &lt;b&gt;CurrentAccount&lt;/b&gt; should redefine member function &lt;b&gt;debit, &lt;/b&gt;so that the account holder can withdraw money upto a maximum of overdraft limit if the balance is below 0. [Note: An &lt;b&gt;overdraft&lt;/b&gt; occurs when withdrawals from a bank account exceed the available balance which gives the account a negative balance]  &lt;/p&gt;&lt;p&gt;After defining the classes in this hierarchy, write a Test program that creates objects of &lt;b&gt;each class&lt;/b&gt; and &lt;b&gt;tests their member functions&lt;/b&gt;. For Example: &lt;b&gt;Add interest&lt;/b&gt; to the SavingsAccount object by first invoking its calculateInterest function, then passing the returned interest amount to the object's &lt;b&gt;credit function&lt;/b&gt;.  &lt;/p&gt;&lt;p&gt;You can write the &lt;b&gt;class declarations in separate header files'&lt;i&gt;as given in the example of the PPT and&lt;/i&gt;&lt;/b&gt;&lt;i&gt;definitions in separate CPP file&lt;b&gt;.&lt;br /&gt;&lt;/b&gt;&lt;/i&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Use the Project in Dev C++ (File-&gt;New-&gt;Project) to compile and execute the programs&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;  &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-1733792704967239298?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/vSsBhV4LphiUEzy2jHA5_RijEQ0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vSsBhV4LphiUEzy2jHA5_RijEQ0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/vSsBhV4LphiUEzy2jHA5_RijEQ0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vSsBhV4LphiUEzy2jHA5_RijEQ0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/xpZZWQif4vw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/1733792704967239298/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/inheritance.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/1733792704967239298?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/1733792704967239298?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/xpZZWQif4vw/inheritance.html" title="Inheritance" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/inheritance.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkcCQH4yfyp7ImA9WxNXFEw.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-1930715577094734229</id><published>2009-10-01T08:12:00.000-07:00</published><updated>2009-10-01T08:14:21.097-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-01T08:14:21.097-07:00</app:edited><title>Creation and Behavior of software Objects</title><content type="html">&lt;h2&gt; &lt;span class="mw-headline"&gt; Learning Objectives  &lt;/span&gt;&lt;/h2&gt; &lt;p&gt;Objective is to better understand &lt;/p&gt; &lt;ol&gt;&lt;li&gt;Constructor &lt;/li&gt;&lt;li&gt;Destructor &lt;/li&gt;&lt;li&gt;Operator Overloading &lt;/li&gt;&lt;li&gt;Dynamic Memory Allocation &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;and also gain additional practice with classes &lt;/p&gt; &lt;a name="Resources" id="Resources"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Resources&lt;/span&gt;&lt;/h2&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.fredosaurus.com/notes-cpp/newdelete/50dynamalloc.html" class="external free" title="http://www.fredosaurus.com/notes-cpp/newdelete/50dynamalloc.html" rel="nofollow"&gt;http://www.fredosaurus.com/notes-cpp/newdelete/50dynamalloc.html&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.fredosaurus.com/notes-cpp/newdelete/55dynexample.html" class="external free" title="http://www.fredosaurus.com/notes-cpp/newdelete/55dynexample.html" rel="nofollow"&gt;http://www.fredosaurus.com/notes-cpp/newdelete/55dynexample.html&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt; &lt;a name="Problem_Sets" id="Problem_Sets"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Problem Sets  &lt;/span&gt;&lt;/h2&gt;Complete the following task with in 2 hours&lt;br /&gt;&lt;a name="Problem_Set_A" id="Problem_Set_A"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Problem Set A  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;Consider a polynomial with integer coefficients, for instance 5x&lt;sup&gt;3&lt;/sup&gt; -4 x&lt;sup&gt;2&lt;/sup&gt; + 5. We can represent this polynomial by array of integers. The 0&lt;sup&gt;th&lt;/sup&gt; element of the array is the coefficient of the x&lt;sup&gt;0&lt;/sup&gt; term (5 in our example), the 1st element of the array is the coefficient of the x&lt;sup&gt;1&lt;/sup&gt; term (0 in our example), the 2&lt;sup&gt;nd&lt;/sup&gt; element of the array is the coefficient of the x&lt;sup&gt;2&lt;/sup&gt; term (-4 in our example), etc. The size of the array determines how large a degree the polynomial can have: a polynomial with degree d will need an array with at least d+ 1 elements. Note that the actual degree of the polynomial may be smaller than (size of vector - 1), since some of the higher order terms may have zero coefficients. &lt;/p&gt;&lt;p&gt;&lt;br /&gt;In this problem set, you will implement a Polynomial class. The polynomial class is having two members: array of integers and degree of polynomial as integer. It should support the following member functions. &lt;/p&gt; &lt;ol&gt;&lt;li&gt;&lt;b&gt;Int degree()&lt;/b&gt; – Return the degree the of the polynomial  &lt;/li&gt;&lt;li&gt;&lt;b&gt;Polynomial addition(const Polynomial &amp;amp;p) &lt;/b&gt;– Returns the sum q + p, where q is the polynomial object whose member function is called. The degree of this sum will be at most max (degree(q), degree(p)). &lt;/li&gt;&lt;li&gt;&lt;b&gt;Polynomial sub(const Polynomial &amp;amp;p) &lt;/b&gt;– Returns the subtraction q - p, where q is the polynomial object whose member function is called. The degree of this subtract will be at most max (degree(q), degree(p)). &lt;/li&gt;&lt;li&gt;&lt;b&gt;Polynomial multiply(const Polynomial&amp;amp;p)&lt;/b&gt; - Returns the product qp, where q is the polynomial object whose member function is called. The degree of this sum will be exactly degree(q) + degree(p). &lt;/li&gt;&lt;li&gt;&lt;b&gt;bool equal(const Polynomial&amp;amp;p)&lt;/b&gt; - Returns true if q is the same polynomial as p, and false otherwise, where q is the polynomial object whose member function is called. Two polynomials are equal if and only if (1) they have the same degree and (2) the coefficient of the terms of the same degree are equal. &lt;/li&gt;&lt;li&gt;&lt;b&gt;void read()&lt;/b&gt; - Reads the polynomial from cin. Your code should prompt the user for the degree of the polynomial, create a array with the correct size, and then use a loop to prompt and input each coefficient. &lt;/li&gt;&lt;li&gt;&lt;b&gt;void print()&lt;/b&gt; - Outputs the polynomial to cout. The polynomial should be printed out using a format that resembles the following: 5x^3 -4x^2 + 5. &lt;/li&gt;&lt;li&gt;&lt;b&gt;int evaluate(int x) &lt;/b&gt;- method to compute and return the polynomial evaluated at x  &lt;/li&gt;&lt;li&gt;Additionally, the class should have the following constructors:  &lt;ol&gt;&lt;li&gt;&lt;b&gt;Polynomial ()&lt;/b&gt; - The polynomial is initialized to be a degree 1 polynomial whose only coefficient is zero.  &lt;/li&gt;&lt;li&gt;&lt;b&gt;Polynomial (int d)&lt;/b&gt; - The polynomial is initialized to be a degree d polynomial. That is, the array should have size d+1 whose coefficients are all zero. &lt;/li&gt;&lt;li&gt;&lt;b&gt;Polynomial (string s)&lt;/b&gt; – The polynomial is initialized to the string.  &lt;/li&gt;&lt;li&gt;&lt;b&gt;Polynomial (const Polynomial &amp;amp;p)&lt;/b&gt; – The polynomial is initialized to the polynomial passed (Know as copy constructor).  &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;li&gt;The class should have the following operators overloaded  &lt;ol&gt;&lt;li&gt;&lt;b&gt;=&lt;/b&gt;  &lt;/li&gt;&lt;li&gt;&lt;b&gt;==&lt;/b&gt;  &lt;/li&gt;&lt;li&gt;&lt;b&gt;+=&lt;/b&gt;  &lt;/li&gt;&lt;li&gt;&lt;b&gt;+&lt;/b&gt;  &lt;/li&gt;&lt;li&gt;&lt;b&gt;–&lt;/b&gt;  &lt;/li&gt;&lt;li&gt;&lt;b&gt;-=&lt;/b&gt;  &lt;/li&gt;&lt;li&gt;&lt;b&gt;*&lt;/b&gt;  &lt;/li&gt;&lt;li&gt;&lt;b&gt;*=&lt;/b&gt; &lt;/li&gt;&lt;li&gt;&lt;b&gt;&lt;&lt;&lt;/b&gt; (as cout &lt;&lt; href="http://gethelp.devx.com/techtips/cpp_pro/10min/10min0400.asp" class="external free" title="http://gethelp.devx.com/techtips/cpp_pro/10min/10min0400.asp" rel="nofollow"&gt;http://gethelp.devx.com/techtips/cpp_pro/10min/10min0400.asp &lt;/li&gt;&lt;li&gt;&lt;b&gt;&gt;&gt;&lt;/b&gt; (as cin &gt;&gt; p) Refer: &lt;a href="http://www.csse.monash.edu.au/%7Ejonmc/CSE2305/Topics/10.19.OpOverload/html/text.html#an_important_use_of_operator_overloading" class="external free" title="http://www.csse.monash.edu.au/~jonmc/CSE2305/Topics/10.19.OpOverload/html/text.html#an_important_use_of_operator_overloading" rel="nofollow"&gt;http://www.csse.monash.edu.au/~jonmc/CSE2305/Topics/10.19.OpOverload/html/text.html#an_important_use_of_operator_overloading&lt;/a&gt; &lt;/li&gt;&lt;/ol&gt; &lt;/li&gt;&lt;li&gt;&lt;b&gt;~Polynomial()&lt;/b&gt; - The class should have destructor to delete the memory allocated to the integer array &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Write a short test main function for polynomial class with the menu option like  &lt;/p&gt; &lt;pre&gt;"What would you like to do?"&lt;br /&gt;#Give a value to a polynomial(print the polynomial)&lt;br /&gt;#Add the two polynomials&lt;br /&gt;#Multiply the two polynomials&lt;br /&gt;#Subtract the two polynomials&lt;br /&gt;#Test equality of the two polynomial&lt;br /&gt;#Evaluate the two polynomials for a given value of x&lt;br /&gt;#Quit&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;You can add few more option to the menu if you like  &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-1930715577094734229?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/5fPRUj3ekclKg2tYwGjTdncY7YU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/5fPRUj3ekclKg2tYwGjTdncY7YU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/5fPRUj3ekclKg2tYwGjTdncY7YU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/5fPRUj3ekclKg2tYwGjTdncY7YU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/gqCKMy61Hlw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/1930715577094734229/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/creation-and-behavior-of-software_01.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/1930715577094734229?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/1930715577094734229?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/gqCKMy61Hlw/creation-and-behavior-of-software_01.html" title="Creation and Behavior of software Objects" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/creation-and-behavior-of-software_01.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUMARnoyfSp7ImA9WxNWE0s.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-5953117402788561603</id><published>2009-10-01T08:10:00.000-07:00</published><updated>2009-10-12T09:04:07.495-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-12T09:04:07.495-07:00</app:edited><title>Creation and Behavior of software Objects</title><content type="html">&lt;h2&gt; &lt;span class="mw-headline"&gt; Learning Objectives  &lt;/span&gt;&lt;/h2&gt; &lt;p&gt;On successful completion of this module you should be able to:  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;explain the purpose of a class constructor and identify a default constructor for a class  &lt;/li&gt;&lt;li&gt;explain the purpose of a class destructor and identify a default destructor for a class  &lt;/li&gt;&lt;li&gt;Apply function overloading  &lt;/li&gt;&lt;li&gt;Apply operator overloading  &lt;/li&gt;&lt;li&gt;code and use simple C++ classes using the following facilities:  &lt;ul&gt;&lt;li&gt;public member functions and private data members  &lt;/li&gt;&lt;li&gt;one or more constructors, including where appropriate, a default constructor  &lt;/li&gt;&lt;li&gt;Overloading of functions  &lt;/li&gt;&lt;li&gt;Operator overloading &lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Introduction  &lt;/span&gt;&lt;/h2&gt; &lt;a name="The_Class" id="The_Class"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; The Class  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;The class is a data type which specifies the data and the permissible operations on the instance of this class. The set of operations define the interface of the objects of the class. The class definition consists of zero or more data members, which together define the data part and zero or more member functions, which together define the interface. In addition, there are three levels of &lt;b&gt;program access&lt;/b&gt;, that are associated with a class.  &lt;/p&gt;&lt;p&gt;The members (data or functions) can be declared to be  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;b&gt;Private&lt;/b&gt; members of the class are hidden and can be accessed only with in the class. Access to them members is denied from out side the class &lt;/li&gt;&lt;li&gt;&lt;b&gt;Protected&lt;/b&gt; members are accessible within the class and to the derived class(to be covered in next modules)  &lt;/li&gt;&lt;li&gt;&lt;b&gt;Public&lt;/b&gt; members are visible outside the class and can be accessed from anywhere within the program. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;A class looks just like a &lt;b&gt;struct&lt;/b&gt;, and indeed they are almost equivalent. By convention we always use class for &lt;b&gt;abstract data types&lt;/b&gt; and only use &lt;b&gt;struct&lt;/b&gt; for &lt;b&gt;‘ordinary data structures’&lt;/b&gt; that don’t have member functions. The only difference between &lt;b&gt;class&lt;/b&gt; and &lt;b&gt;struct&lt;/b&gt; is that the default access control for members is &lt;b&gt;private&lt;/b&gt; in the former and &lt;b&gt;public&lt;/b&gt; in the latter.  &lt;/p&gt;&lt;p&gt;A class consists of &lt;i&gt;data memebers&lt;/i&gt; and &lt;i&gt;member functions&lt;/i&gt;. Each having different program access level.  &lt;/p&gt;&lt;p&gt;The &lt;b&gt;data memebers&lt;/b&gt;, specify the attributes/state of the objects of the class. The data members define the name of the attributes and their type.  &lt;/p&gt;&lt;p&gt;The &lt;b&gt;member function&lt;/b&gt; describes behavior of an object-class and act on state (variables/fields) of an object-class. The member functions collectively define the interface of an object of a class. The special trait about member functions is they can access the private/protected data members (state variables/fields) of their class and manipulate them. No external functions can access the private/protected data members of a class. &lt;/p&gt; &lt;a name="The_Class_Scope" id="The_Class_Scope"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; The Class Scope  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;Every class defines the scope for the members in it. The data members of a class may be defined, before they are used in a member functions, or can be used in the member functions, and may be defined later in the class. The data members have the class scope, immaterial of where they are defined in the class. &lt;/p&gt; &lt;a name="Constructor" id="Constructor"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Constructor  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;A constructor is also a member function, the first function called while creating an object. It could contain initializations to the variables of the object or calling other functions etc. The constructor by nature cannot return any value. The return has to be void. It can however take an argument list. The constructor has the same name as the class but having different argument list. &lt;/p&gt;&lt;p&gt;The constructor can be defined within the class definition or outside of it, just as any other member function definition. It can also be defined outside the class and made inline, just as other member functions can be &lt;/p&gt;&lt;p&gt;The constructor which takes no arguments is called the default constructor.  &lt;/p&gt;&lt;p&gt;&lt;b&gt;Overloading constructor:&lt;/b&gt;When constructor are overloaded, the constructor must vary in the argument list. Upon object creation, that constructor for which there is match between the formal and the actual parameters, is invoked. &lt;/p&gt; &lt;a name="Copy_constructor" id="Copy_constructor"&gt;&lt;/a&gt;&lt;h4&gt; &lt;span class="mw-headline"&gt; Copy constructor  &lt;/span&gt;&lt;/h4&gt; &lt;p&gt;A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object. First argument of such constructor is a reference to an object of the same type as being constructed (const or non-const), which might be followed by parameters of any type (all having default values). &lt;/p&gt;&lt;p&gt;Normally the compiler automatically creates a copy constructor for each class (known as an implicit copy constructor) but for special cases the programmer creates the copy constructor, known as an explicit copy constructor. In such cases, the compiler doesn't create one. &lt;/p&gt;&lt;p&gt;Refer: &lt;a href="http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html" class="external free" title="http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html" rel="nofollow"&gt;http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html&lt;/a&gt;  &lt;/p&gt; &lt;a name="Destructor" id="Destructor"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Destructor  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;The destructor destroys a previously created object. The destructor takes no arguments and cannot return a value. The destructor has the same name as of the class, except that the name is preceded by a ~ &lt;i&gt;(tilda)&lt;/i&gt;.  &lt;/p&gt; &lt;a name="Composite_Class" id="Composite_Class"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Composite Class  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;A Composite object can be defined as one which consists of other objects.  &lt;/p&gt; As an example, consider the class &lt;i&gt;Circle&lt;/i&gt; as composite object containing an instance of Point, representing the centre and radius which is of type, &lt;i&gt;float&lt;/i&gt;. The definition of Point and Circle in this case would be. &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Point&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;float&lt;/span&gt; x&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;float&lt;/span&gt; y&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Circle&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;float&lt;/span&gt; radius&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;Point centre&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  &lt;a name="Function_Overloading" id="Function_Overloading"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Function Overloading  &lt;/span&gt;&lt;/h3&gt; In order to overload a function, a different signature should be used for each overloaded version. A function signature consists of the list of types of its arguments as well as their order. Here's a set of valid overloaded versions of a function named f: &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; f&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt; c, &lt;span class="kw4"&gt;int&lt;/span&gt; i&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; f&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; i, &lt;span class="kw4"&gt;char&lt;/span&gt; c&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; f&lt;span class="br0"&gt;(&lt;/span&gt;string &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt; s&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; f&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; f&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; i&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; f&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;char&lt;/span&gt; c&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; Note, however, that a function differing only by return type is illegal (it is legal to use different a return type with a different argument list, though) : &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; f&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;  &lt;span class="co1"&gt;//error; differs from void f(); above only by return type &lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; f&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;float&lt;/span&gt; f&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;  &lt;span class="co1"&gt;//fine. unique signature&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;You can overload the member functions in a class.  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Refer: &lt;a href="http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc11cplr312.htm" class="external free" title="http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc11cplr312.htm" rel="nofollow"&gt;http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc11cplr312.htm&lt;/a&gt; Function Overloading  &lt;/li&gt;&lt;li&gt;Refer for example: &lt;a href="http://smart2help.com/e-books/ticpp-2nd-ed-vol-one/Chapter07.html" class="external free" title="http://smart2help.com/e-books/ticpp-2nd-ed-vol-one/Chapter07.html" rel="nofollow"&gt;http://smart2help.com/e-books/ticpp-2nd-ed-vol-one/Chapter07.html&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt; &lt;a name="Operator_Overloading" id="Operator_Overloading"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Operator Overloading  &lt;/span&gt;&lt;/h3&gt; When a class is defined, a set of operations are associated with it which together define the behavior of the instance of the class. So far, mnemonics have been used to name the operation. In some situations, it may be desirable to use operators which have conventional meaning rather than use a name for it. For example, consider the class IntSet, a set of integers class. To check if two instances are equal, one way is to define a member function &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; IntSet&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;equal&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;IntSet &lt;span class="sy3"&gt;&amp;amp;&lt;/span&gt;s2&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  in the class IntSet, which could be used as follows: &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw1"&gt;if&lt;/span&gt; s1.&lt;span class="me1"&gt;equal&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;s2&lt;span class="br0"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; Another way of doing this is to overload the C++ "==" operator in the class IntSet. The same functionalily can be coded as: &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="kw1"&gt;if&lt;/span&gt; &lt;span class="br0"&gt;(&lt;/span&gt;s1&lt;span class="sy1"&gt;==&lt;/span&gt;s2&lt;span class="br0"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;The C++ Operators which can be overloaded are arithmetic, logical, relational operators, as well as call(), subscripting[], dereferencing -&gt;, assignment =, the extraction &lt;&lt;&gt;&gt; operators. &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Refer: &lt;a href="http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc11cplr318.htm" class="external free" title="http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc11cplr318.htm" rel="nofollow"&gt;http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc11cplr318.htm&lt;/a&gt; Operator Overloading  &lt;/li&gt;&lt;li&gt;Refer for example: &lt;a href="http://www.cplusplus.com/doc/tutorial/classes2/" class="external free" title="http://www.cplusplus.com/doc/tutorial/classes2/" rel="nofollow"&gt;http://www.cplusplus.com/doc/tutorial/classes2/&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt; &lt;a name="The_keyword_this" id="The_keyword_this"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; The keyword this  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;The keyword this represents a pointer to the object whose member function is being executed. It is a pointer to the object itself. &lt;/p&gt;&lt;p&gt;One of its uses can be to check if a parameter passed to a member function is the object itself.  &lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt; &lt;a name="Resources" id="Resources"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Resources  &lt;/span&gt;&lt;/h2&gt;&lt;br /&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-d025233dd9462195" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;
&lt;param name="bgcolor" value="#FFFFFF"&gt;
&lt;param name="allowfullscreen" value="true"&gt;
&lt;param name="flashvars" value="flvurl=http://v3.nonxt6.googlevideo.com/videoplayback?id%3Dd025233dd9462195%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D1F5070838445810FB2D76D34971B9F9C1F147392.437597C4A490861FDF174421CE02E416CA1D518A%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3Dd025233dd9462195%26offsetms%3D5000%26itag%3Dw160%26sigh%3Dlseg6mNRwcOb89nzNSNYk2vYrlA&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;
&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"
width="320" height="266" bgcolor="#FFFFFF"
flashvars="flvurl=http://v3.nonxt6.googlevideo.com/videoplayback?id%3Dd025233dd9462195%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D1F5070838445810FB2D76D34971B9F9C1F147392.437597C4A490861FDF174421CE02E416CA1D518A%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3Dd025233dd9462195%26offsetms%3D5000%26itag%3Dw160%26sigh%3Dlseg6mNRwcOb89nzNSNYk2vYrlA&amp;autoplay=0&amp;ps=blogger"
allowFullScreen="true" /&gt;&lt;/object&gt;
&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-38f572e4bd330ae5" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;
&lt;param name="bgcolor" value="#FFFFFF"&gt;
&lt;param name="allowfullscreen" value="true"&gt;
&lt;param name="flashvars" value="flvurl=http://v23.nonxt3.googlevideo.com/videoplayback?id%3D38f572e4bd330ae5%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D7101E794A38440E2EBB1F91EF19FEFBD97EDE3F.2B302C8BD2B0095F487F85E6D675A56354255D9B%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D38f572e4bd330ae5%26offsetms%3D5000%26itag%3Dw160%26sigh%3DtDOugVWIXYskUgJ4Cv_hxqn2C4U&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;
&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"
width="320" height="266" bgcolor="#FFFFFF"
flashvars="flvurl=http://v23.nonxt3.googlevideo.com/videoplayback?id%3D38f572e4bd330ae5%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D7101E794A38440E2EBB1F91EF19FEFBD97EDE3F.2B302C8BD2B0095F487F85E6D675A56354255D9B%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D38f572e4bd330ae5%26offsetms%3D5000%26itag%3Dw160%26sigh%3DtDOugVWIXYskUgJ4Cv_hxqn2C4U&amp;autoplay=0&amp;ps=blogger"
allowFullScreen="true" /&gt;&lt;/object&gt;
&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-9e97e576e7edfabf" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;
&lt;param name="bgcolor" value="#FFFFFF"&gt;
&lt;param name="allowfullscreen" value="true"&gt;
&lt;param name="flashvars" value="flvurl=http://v5.nonxt7.googlevideo.com/videoplayback?id%3D9e97e576e7edfabf%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D6F4B688D47D01134C75888A4495589A4997CA049.43119611472224F5C26F06D19B24E7F1F2CB2F81%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D9e97e576e7edfabf%26offsetms%3D5000%26itag%3Dw160%26sigh%3DQTvzmDVnkbaLcYRmcSd7CIi4eJc&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;
&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"
width="320" height="266" bgcolor="#FFFFFF"
flashvars="flvurl=http://v5.nonxt7.googlevideo.com/videoplayback?id%3D9e97e576e7edfabf%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D6F4B688D47D01134C75888A4495589A4997CA049.43119611472224F5C26F06D19B24E7F1F2CB2F81%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D9e97e576e7edfabf%26offsetms%3D5000%26itag%3Dw160%26sigh%3DQTvzmDVnkbaLcYRmcSd7CIi4eJc&amp;autoplay=0&amp;ps=blogger"
allowFullScreen="true" /&gt;&lt;/object&gt;
&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-a847a4ddda774a01" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;
&lt;param name="bgcolor" value="#FFFFFF"&gt;
&lt;param name="allowfullscreen" value="true"&gt;
&lt;param name="flashvars" value="flvurl=http://v23.nonxt3.googlevideo.com/videoplayback?id%3Da847a4ddda774a01%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D241773026FEDC92AED24E1720DDEE377C4BC58CB.760C3D2C4AC2ACD2DD57B18475D69CE8D6B44FA2%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3Da847a4ddda774a01%26offsetms%3D5000%26itag%3Dw160%26sigh%3DeqTBNcXLK6KCmqgi0cfphWUgnM4&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;
&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"
width="320" height="266" bgcolor="#FFFFFF"
flashvars="flvurl=http://v23.nonxt3.googlevideo.com/videoplayback?id%3Da847a4ddda774a01%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D241773026FEDC92AED24E1720DDEE377C4BC58CB.760C3D2C4AC2ACD2DD57B18475D69CE8D6B44FA2%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3Da847a4ddda774a01%26offsetms%3D5000%26itag%3Dw160%26sigh%3DeqTBNcXLK6KCmqgi0cfphWUgnM4&amp;autoplay=0&amp;ps=blogger"
allowFullScreen="true" /&gt;&lt;/object&gt;
&lt;br /&gt;&lt;br /&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-afe9b5c1bf1aab48" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;
&lt;param name="bgcolor" value="#FFFFFF"&gt;
&lt;param name="allowfullscreen" value="true"&gt;
&lt;param name="flashvars" value="flvurl=http://v10.nonxt7.googlevideo.com/videoplayback?id%3Dafe9b5c1bf1aab48%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D6CF80B7FEB0E7AA4DBF030132E0566F8D341E7E4.A05001742EA2D0942E904F9B0D44410728F3613%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3Dafe9b5c1bf1aab48%26offsetms%3D5000%26itag%3Dw160%26sigh%3DCeGnnw0-bB6qulamlPH5nwt0aQM&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;
&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"
width="320" height="266" bgcolor="#FFFFFF"
flashvars="flvurl=http://v10.nonxt7.googlevideo.com/videoplayback?id%3Dafe9b5c1bf1aab48%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D6CF80B7FEB0E7AA4DBF030132E0566F8D341E7E4.A05001742EA2D0942E904F9B0D44410728F3613%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3Dafe9b5c1bf1aab48%26offsetms%3D5000%26itag%3Dw160%26sigh%3DCeGnnw0-bB6qulamlPH5nwt0aQM&amp;autoplay=0&amp;ps=blogger"
allowFullScreen="true" /&gt;&lt;/object&gt;
&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-52e3651925c83446" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;
&lt;param name="bgcolor" value="#FFFFFF"&gt;
&lt;param name="allowfullscreen" value="true"&gt;
&lt;param name="flashvars" value="flvurl=http://v4.nonxt6.googlevideo.com/videoplayback?id%3D52e3651925c83446%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D3112A5383FD6FB355085CFCB10B5750DC9DDA0A.730BE127840243F14CC7B24D1773A16672FE363B%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D52e3651925c83446%26offsetms%3D5000%26itag%3Dw160%26sigh%3DdevCmQfpZ9uo-LlwTY8Qc9gX134&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;
&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"
width="320" height="266" bgcolor="#FFFFFF"
flashvars="flvurl=http://v4.nonxt6.googlevideo.com/videoplayback?id%3D52e3651925c83446%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D3112A5383FD6FB355085CFCB10B5750DC9DDA0A.730BE127840243F14CC7B24D1773A16672FE363B%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D52e3651925c83446%26offsetms%3D5000%26itag%3Dw160%26sigh%3DdevCmQfpZ9uo-LlwTY8Qc9gX134&amp;autoplay=0&amp;ps=blogger"
allowFullScreen="true" /&gt;&lt;/object&gt;
&lt;br /&gt;&lt;br /&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Problem Sets  &lt;/span&gt;&lt;/h2&gt;Complete following tasks In 2 hours&lt;br /&gt;&lt;a name="Problem_Set_A" id="Problem_Set_A"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Problem Set A  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt; Define a class Random to provide random numbers within a range that is specified through the constructor. The class should provide a draw function to get a random number. &lt;/p&gt; Use the code given below for the Random Class. &lt;b&gt;You need to write the code in the given program wherever it is commented as write the code here&lt;/b&gt; o &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#include&lt;iostream&gt; //For I/O&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;ctime&gt;    // For time()&lt;/span&gt;&lt;br /&gt;&lt;span class="co2"&gt;#include &lt;cstdlib&gt;  // For srand() and rand()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw2"&gt;class&lt;/span&gt; Random&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;     &lt;span class="kw2"&gt;private&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;             &lt;span class="kw4"&gt;int&lt;/span&gt; max&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;             &lt;span class="kw4"&gt;int&lt;/span&gt; min&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;     &lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;            Random&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;                 &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"Default Random Constructor is called and default values set with Min 10 and Max 20&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;           &lt;br /&gt;                    &lt;span class="co1"&gt;//Write the code here to set the default values for min 10 and max 20 &lt;/span&gt;&lt;br /&gt;            &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;            Random&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; minA, &lt;span class="kw4"&gt;int&lt;/span&gt; maxA&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;                 &lt;span class="kw3"&gt;cout&lt;/span&gt; &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;"Random Constructor called with argument of Min"&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; minA &lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt; &lt;span class="st0"&gt;" and Max"&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;maxA&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;&lt;span class="st0"&gt;"&lt;span class="es1"&gt;\n&lt;/span&gt;"&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="co1"&gt;//Write the code here to set the max and min &lt;/span&gt;&lt;br /&gt;            &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;span class="kw4"&gt;int&lt;/span&gt; draw&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span class="kw4"&gt;int&lt;/span&gt; r&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;                &lt;span class="kw3"&gt;srand&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw3"&gt;time&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;0&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;  &lt;span class="co1"&gt;// Initialize random number generator.&lt;/span&gt;&lt;br /&gt;                r &lt;span class="sy1"&gt;=&lt;/span&gt; &lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw3"&gt;rand&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="sy2"&gt;%&lt;/span&gt; &lt;span class="br0"&gt;(&lt;/span&gt;max &lt;span class="sy2"&gt;-&lt;/span&gt; min&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="sy2"&gt;+&lt;/span&gt; min&lt;span class="sy4"&gt;;&lt;/span&gt;                &lt;br /&gt;                &lt;span class="kw1"&gt;return&lt;/span&gt; r&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;void&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span class="co1"&gt;//Write the code here to declare two Instances of Random class and by calling different constructors.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; &lt;span class="co1"&gt;//Use cout to call the draw function for each of the Random instance&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; Refer: &lt;a href="http://www.fredosaurus.com/notes-cpp/misc/random.html" class="external free" title="http://www.fredosaurus.com/notes-cpp/misc/random.html" rel="nofollow"&gt;http://www.fredosaurus.com/notes-cpp/misc/random.html&lt;/a&gt;  &lt;pre&gt;Name the program as: PA1_random.cpp&lt;br /&gt;&lt;/pre&gt; &lt;a name="Problem_Set_B" id="Problem_Set_B"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Problem Set B  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;Write a class called Point that is capable of representing the coordinates of a point on a two-dimensional graph. You must permit creation of variables as follows: &lt;/p&gt;&lt;p&gt;Point p, q(2.1, -4.3);  &lt;/p&gt;&lt;p&gt;The declaration of p above will be for the point (0,0).  &lt;/p&gt;&lt;p&gt;Provide the following operations:  &lt;/p&gt; &lt;ol&gt;&lt;li&gt;Return the x coordinate or the y coordinate  &lt;/li&gt;&lt;li&gt;Set the x coordinate or the y coordinate  &lt;/li&gt;&lt;li&gt;Return the distance from the origin, formula is sqrt(x&lt;sup&gt;2&lt;/sup&gt; + y&lt;sup&gt;2&lt;/sup&gt;)  &lt;/li&gt;&lt;li&gt;Input a point as two floating point numbers  &lt;/li&gt;&lt;li&gt;Output a point in the format (2.1,-4.3) &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Write a short test main function for your class.  &lt;/p&gt;&lt;p&gt;Refer: &lt;a href="http://www.cppreference.com/wiki/c/math/start" class="external free" title="http://www.cppreference.com/wiki/c/math/start" rel="nofollow"&gt;http://www.cppreference.com/wiki/c/math/start&lt;/a&gt;  &lt;/p&gt; &lt;pre&gt;Name the program as PB1_Point.cpp&lt;br /&gt;&lt;/pre&gt; &lt;a name="Problem_Set_C" id="Problem_Set_C"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt; Problem Set C  &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;Write a class to hold the time of day. You must permit the creation of variables as follows:  &lt;/p&gt;&lt;p&gt;Time t1, t2(3600), t3(12,00), t4(23, 59, 59);  &lt;/p&gt;&lt;p&gt;In this case t1 is set to 00:00:00, t2 to 01:00:00 (3600 seconds), t3 to 12:00:00 and t4 to 23:59:59.  &lt;/p&gt;&lt;p&gt;Provide the following operations:  &lt;/p&gt; &lt;ol&gt;&lt;li&gt;Return the hours, minutes or seconds  &lt;/li&gt;&lt;li&gt;Return the total seconds using a cast operator overload to type long  &lt;/li&gt;&lt;li&gt;Set the hours, minutes or seconds  &lt;/li&gt;&lt;li&gt;Automatically convert an integer to a time, interpreting the integer value as the number of seconds  &lt;/li&gt;&lt;li&gt;Input a time as three integers in the format hh:mm:ss  &lt;/li&gt;&lt;li&gt;Output the time in the format hh:mm:ss  &lt;/li&gt;&lt;li&gt;Add a specified number of seconds to a time &lt;/li&gt;&lt;li&gt;Overload the operator = for assignment and == for equality. &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Write a short test main function for your class.  &lt;/p&gt; &lt;pre&gt;Name the program as: Time.cpp&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-5953117402788561603?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/mWVecOd0RLurD-9-kPTsthztTDI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/mWVecOd0RLurD-9-kPTsthztTDI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/mWVecOd0RLurD-9-kPTsthztTDI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/mWVecOd0RLurD-9-kPTsthztTDI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/iLR4Qe7zZA0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/5953117402788561603/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/creation-and-behavior-of-software.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/5953117402788561603?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/5953117402788561603?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/iLR4Qe7zZA0/creation-and-behavior-of-software.html" title="Creation and Behavior of software Objects" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/creation-and-behavior-of-software.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ANQn84eCp7ImA9WxNXFE0.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-8215108195348864978</id><published>2009-10-01T08:03:00.000-07:00</published><updated>2009-10-01T08:09:53.130-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-01T08:09:53.130-07:00</app:edited><title>Identifying Objects and Creating Software Objects</title><content type="html">&lt;h2&gt; &lt;span class="mw-headline"&gt;Learning Objective&lt;/span&gt;&lt;/h2&gt; &lt;ol&gt;&lt;li&gt;Identifying Real world Objects  &lt;/li&gt;&lt;li&gt;Creating Software Objects using class. &lt;/li&gt;&lt;li&gt;Implement state and behavior of software objects. &lt;/li&gt;&lt;/ol&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt; Instructional Activities &lt;/span&gt;&lt;/h2&gt;Reference Reading: &lt;ul&gt;&lt;li&gt;&lt;a href="http://java.sun.com/docs/books/tutorial/java/concepts/object.html" class="external text" title="http://java.sun.com/docs/books/tutorial/java/concepts/object.html" rel="nofollow"&gt;What is an Object&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Object-oriented_programming" class="external text" title="http://en.wikipedia.org/wiki/Object-oriented_programming" rel="nofollow"&gt;Object Oriented Programming&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/doc/tutorial/classes.html" class="external text" title="http://www.cplusplus.com/doc/tutorial/classes.html" rel="nofollow"&gt;CPlusPlus -&gt; Classes Basic tutorial&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Problem Sets&lt;/span&gt;&lt;/h2&gt; &lt;a name="Problem_Set_A" id="Problem_Set_A"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set A&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;Identify 5 objects in real world &lt;/p&gt; &lt;ol&gt;&lt;li&gt;List the state of these objects &lt;/li&gt;&lt;li&gt;List the behavior of these objects &lt;/li&gt;&lt;li&gt;Note: Your list of objects should be unique from the entire class. Otherwise you would be give zero marks for the whole of this module &lt;/li&gt;&lt;/ol&gt;  &lt;a name="Problem_Set_B" id="Problem_Set_B"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set B &lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;Classes and Objects: Using the following class create an object for it in the main program &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;   &lt;span class="kw2"&gt;class&lt;/span&gt; Integer &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;     &lt;span class="kw2"&gt;private&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;         &lt;span class="kw4"&gt;int&lt;/span&gt; integer&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;     &lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;         &lt;span class="kw4"&gt;void&lt;/span&gt; readInteger&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;         &lt;span class="kw4"&gt;void&lt;/span&gt; printMessage&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; &lt;span class="kw4"&gt;void&lt;/span&gt; Integer&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;readInteger&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;"enter the value of N"&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kw3"&gt;cin&lt;/span&gt;&lt;span class="sy1"&gt;&gt;&gt;&lt;/span&gt;N&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span class="kw4"&gt;void&lt;/span&gt; Integer&lt;span class="sy4"&gt;::&lt;/span&gt;&lt;span class="me2"&gt;printMessage&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;              &lt;span class="kw1"&gt;for&lt;/span&gt; &lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; i &lt;span class="sy1"&gt;=&lt;/span&gt; 0 &lt;span class="sy4"&gt;;&lt;/span&gt; i &lt;span class="sy1"&gt;&lt;&lt;/span&gt; N &lt;span class="sy4"&gt;;&lt;/span&gt; i&lt;span class="sy2"&gt;++&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt; &lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;                  &lt;span class="kw3"&gt;cout&lt;/span&gt;&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;Hello&lt;span class="sy1"&gt;&lt;&lt;&lt;/span&gt;endl&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;              &lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;  &lt;ol&gt;&lt;li&gt; &lt;b&gt;Create a object called “intobj”&lt;/b&gt; for the class “Integer” in the main program &lt;ul&gt;&lt;li&gt;Call intobj.readInteger() and intobj.printMessage() from the main program &lt;/li&gt;&lt;li&gt;Add another member function called int addInteger(int no) to the class Integer.  &lt;/li&gt;&lt;li&gt;The function addInteger() returns the sum of the value of “N + no”.  &lt;/li&gt;&lt;li&gt;Call intobj.addInteger() from the main program &lt;/li&gt;&lt;/ul&gt;  &lt;/li&gt;&lt;li&gt; &lt;b&gt;Create a class called "class Circle {  };" &lt;/b&gt; &lt;ul&gt;&lt;li&gt;Add members function to readRadius &lt;/li&gt;&lt;li&gt;Add a member function to displayArea  &lt;/li&gt;&lt;li&gt;Create an object for the class Circle  &lt;/li&gt;&lt;li&gt;Call the member functions readRadius() and displayArea() from the main program. &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;/ol&gt;  &lt;p&gt;&lt;b&gt;2. &lt;/b&gt; Create the class called 'Math'. &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;  &lt;span class="kw2"&gt;class&lt;/span&gt; Math&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class="kw2"&gt;private&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;    &lt;span class="kw4"&gt;int&lt;/span&gt; ival&lt;br /&gt;  &lt;span class="kw2"&gt;public&lt;/span&gt;&lt;span class="sy4"&gt;:&lt;/span&gt;&lt;br /&gt;     &lt;span class="kw4"&gt;int&lt;/span&gt; sum&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; x, &lt;span class="kw4"&gt;int&lt;/span&gt; y&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt; &lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;ol&gt;&lt;li&gt; &lt;b&gt;Create a class called “Math” &lt;/b&gt; &lt;ul&gt;&lt;li&gt;Complete the member function int sum(int x, int y);  &lt;ul&gt;&lt;li&gt;return the sum of x and y; &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;Write a main program which creates an object say “m” for the class “Math”  &lt;ul&gt;&lt;li&gt;From the main program read two integers i1, i2.  &lt;/li&gt;&lt;li&gt;Call the member function “total = m.sum(i1, i2)” to get the sum of i1 + i2 &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt; Accessing private member in member functions:  &lt;ul&gt;&lt;li&gt;Modify the member functions so that sum() stores the output in ival.  &lt;/li&gt;&lt;li&gt;ival = x + y;  return ival; &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;/ol&gt;  &lt;a name="Problem_Set_C" id="Problem_Set_C"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set C&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1. &lt;/b&gt;Create a class called State() with the following member functions &lt;/p&gt; &lt;ul&gt;&lt;li&gt;readDistricts(); //read N = no of districts, dynamically allocated memory for N strings, read each district name from the stdin. &lt;/li&gt;&lt;li&gt;printDistricts();   //print the district names on the standard output (display-screen); &lt;/li&gt;&lt;li&gt;Use dynamic allocation of strings in the classes is compulsory: &lt;/li&gt;&lt;/ul&gt; &lt;ol&gt;&lt;li&gt; Create an object for the class State in the main program  &lt;/li&gt;&lt;li&gt; Call read_districts() and printDistricts() from the main program &lt;/li&gt;&lt;/ol&gt;  &lt;p&gt;&lt;b&gt;2. &lt;/b&gt;Create class called Interest &lt;/p&gt; &lt;ol&gt;&lt;li&gt; &lt;b&gt;Create class called Interest&lt;/b&gt; with the following attributes and behavior  &lt;ul&gt;&lt;li&gt;Attributes &lt;ul&gt;&lt;li&gt;Principle Amount &lt;/li&gt;&lt;li&gt;Number of Years &lt;/li&gt;&lt;li&gt;Rate of Interest &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;Behavior &lt;ul&gt;&lt;li&gt;readPrincipleAmount();  &lt;/li&gt;&lt;li&gt;readNoOfYears(); &lt;/li&gt;&lt;li&gt;readInterestRatePY();  &lt;/li&gt;&lt;li&gt;calcuateSimpleInterest(); &lt;/li&gt;&lt;li&gt;calculateCompoundInterest(); &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt; Create an object for the class Interest in the main program  &lt;/li&gt;&lt;li&gt; call member functions, read* to read principle amount, no. of years and interest rate  &lt;/li&gt;&lt;li&gt; call calcuateSimpleInterest() and calculateCompoundInterest() to display the amount+interest separately. &lt;/li&gt;&lt;/ol&gt;  &lt;p&gt;&lt;b&gt;3. &lt;/b&gt;Create class for 3 Objects identified in Problem Set A 1 &lt;/p&gt;&lt;p&gt;Create 3 classes with state and behavior identified. &lt;/p&gt;&lt;p&gt;Create an object for the 3 classes in the main program and test them. &lt;/p&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/b&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-8215108195348864978?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Pe0SUOg9lVeF2c_mMu0fKIhNZkc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Pe0SUOg9lVeF2c_mMu0fKIhNZkc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Pe0SUOg9lVeF2c_mMu0fKIhNZkc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Pe0SUOg9lVeF2c_mMu0fKIhNZkc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/ALq_u1woXso" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/8215108195348864978/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/10/identifying-objects-and-creating.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/8215108195348864978?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/8215108195348864978?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/ALq_u1woXso/identifying-objects-and-creating.html" title="Identifying Objects and Creating Software Objects" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/10/identifying-objects-and-creating.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0QARXY7eSp7ImA9WxNXFE0.&quot;"><id>tag:blogger.com,1999:blog-7343566691466781160.post-9055186067668160194</id><published>2009-09-22T11:02:00.000-07:00</published><updated>2009-10-01T08:02:24.801-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-01T08:02:24.801-07:00</app:edited><title>Basics</title><content type="html">&lt;h1&gt; &lt;span class="mw-headline"&gt;Introduction to C++ and differences between C&lt;/span&gt;&lt;/h1&gt; &lt;a name="Learning_Objective" id="Learning_Objective"&gt;&lt;/a&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Learning Objective&lt;/span&gt;&lt;/h2&gt; &lt;ul&gt;&lt;li&gt;To know differences between C  and C++ &lt;/li&gt;&lt;li&gt;Demonstrate the capability of programming in C++ &lt;/li&gt;&lt;li&gt;Apply C++ syntax, New/Delete and Reference operations &lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;&lt;span class="mw-headline"&gt;Resources&lt;/span&gt;&lt;/h2&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-5c00438ccba33726" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;
&lt;param name="bgcolor" value="#FFFFFF"&gt;
&lt;param name="allowfullscreen" value="true"&gt;
&lt;param name="flashvars" value="flvurl=http://v12.nonxt4.googlevideo.com/videoplayback?id%3D5c00438ccba33726%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D769751F2D04967A1A94FC38A40344DDB23A7DE0D.6200BE1C7B049234D5C393ECD5E64F25658A68E3%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D5c00438ccba33726%26offsetms%3D5000%26itag%3Dw160%26sigh%3DDHaR6TvdT19PQTW_YiQ1rPqFol4&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;
&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"
width="320" height="266" bgcolor="#FFFFFF"
flashvars="flvurl=http://v12.nonxt4.googlevideo.com/videoplayback?id%3D5c00438ccba33726%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D769751F2D04967A1A94FC38A40344DDB23A7DE0D.6200BE1C7B049234D5C393ECD5E64F25658A68E3%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D5c00438ccba33726%26offsetms%3D5000%26itag%3Dw160%26sigh%3DDHaR6TvdT19PQTW_YiQ1rPqFol4&amp;autoplay=0&amp;ps=blogger"
allowFullScreen="true" /&gt;&lt;/object&gt;
&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-e51b687681b177a2" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;
&lt;param name="bgcolor" value="#FFFFFF"&gt;
&lt;param name="allowfullscreen" value="true"&gt;
&lt;param name="flashvars" value="flvurl=http://v8.nonxt3.googlevideo.com/videoplayback?id%3De51b687681b177a2%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D35DD9B4F40480338F2796C533F2CA8C994A347B6.3262BF41D28575CA57F13321CAB090DCAEC92012%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3De51b687681b177a2%26offsetms%3D5000%26itag%3Dw160%26sigh%3DE5JOwEN_3o1DFex7B9GqKoz7-w4&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;
&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"
width="320" height="266" bgcolor="#FFFFFF"
flashvars="flvurl=http://v8.nonxt3.googlevideo.com/videoplayback?id%3De51b687681b177a2%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D35DD9B4F40480338F2796C533F2CA8C994A347B6.3262BF41D28575CA57F13321CAB090DCAEC92012%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3De51b687681b177a2%26offsetms%3D5000%26itag%3Dw160%26sigh%3DE5JOwEN_3o1DFex7B9GqKoz7-w4&amp;autoplay=0&amp;ps=blogger"
allowFullScreen="true" /&gt;&lt;/object&gt;
&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;object width="320" height="266" class="BLOG_video_class" id="BLOG_video-66d80d09725b85cd" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"&gt;&lt;param name="movie" value="http://www.youtube.com/get_player"&gt;
&lt;param name="bgcolor" value="#FFFFFF"&gt;
&lt;param name="allowfullscreen" value="true"&gt;
&lt;param name="flashvars" value="flvurl=http://v4.nonxt7.googlevideo.com/videoplayback?id%3D66d80d09725b85cd%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D45F4F0BFA65A1D7E5D37D695D754258C47A96584.8AFD89A5B110FD2DCCB127C5D69D7AC51DE17E5%26key%3Dck1&amp;amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D66d80d09725b85cd%26offsetms%3D5000%26itag%3Dw160%26sigh%3DsWbQjzG06XSzF04W7wQR_-yDVBY&amp;amp;autoplay=0&amp;amp;ps=blogger"&gt;
&lt;embed src="http://www.youtube.com/get_player" type="application/x-shockwave-flash"
width="320" height="266" bgcolor="#FFFFFF"
flashvars="flvurl=http://v4.nonxt7.googlevideo.com/videoplayback?id%3D66d80d09725b85cd%26itag%3D5%26app%3Dblogger%26ip%3D0.0.0.0%26ipbits%3D0%26expire%3D1332569416%26sparams%3Did,itag,ip,ipbits,expire%26signature%3D45F4F0BFA65A1D7E5D37D695D754258C47A96584.8AFD89A5B110FD2DCCB127C5D69D7AC51DE17E5%26key%3Dck1&amp;iurl=http://video.google.com/ThumbnailServer2?app%3Dblogger%26contentid%3D66d80d09725b85cd%26offsetms%3D5000%26itag%3Dw160%26sigh%3DsWbQjzG06XSzF04W7wQR_-yDVBY&amp;autoplay=0&amp;ps=blogger"
allowFullScreen="true" /&gt;&lt;/object&gt;
&lt;br /&gt;&lt;h2&gt; &lt;span class="mw-headline"&gt;Problem Sets&lt;/span&gt;&lt;/h2&gt;Complete the following problem sets with in 2 hours&lt;br /&gt;&lt;a name="Problem_Set_A" id="Problem_Set_A"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set A&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1.&lt;/b&gt; Write a simple ‘Hello, new world!’ program in C and CPP edited, compiled and running on your chosen development platform. Change the program to take a command line argument (or multiple command line arguments) and to say ‘Hello name!’ &lt;/p&gt;  &lt;p&gt;&lt;b&gt;2.&lt;/b&gt; Tabulate all the basic C/C++ types, eg. Char, int, float, double. Don’t forget the unsigned, signed, short, long etc. Write a simple C and C++ program to print out the sizes in bytes of each type on your system. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;3.&lt;/b&gt; Find out on your system what the largest and smallest values are for the basic types above? Write a program in C and C++ to print out the Limits of each data type described in previous program. &lt;/p&gt;   &lt;a name="Problem_Set_B" id="Problem_Set_B"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set B&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;1.&lt;/b&gt; Write a function to print the date, for the structure &lt;b&gt;Date&lt;/b&gt; in C and C++. &lt;/p&gt;&lt;p&gt;You need to write the code wherever necessary for the given program in C and C++.  &lt;/p&gt;&lt;p&gt;&lt;b&gt;Program to print date in C&lt;/b&gt; &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="c source-c"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#include &lt;stdio.h&gt;//Standard Input and Output Library&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw4"&gt;struct&lt;/span&gt; DATE&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; day&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; month&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; year&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; showDate&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;struct&lt;/span&gt; DATE d&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; setDate&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; d&lt;span class="sy0"&gt;,&lt;/span&gt; &lt;span class="kw4"&gt;int&lt;/span&gt; m&lt;span class="sy0"&gt;,&lt;/span&gt; &lt;span class="kw4"&gt;int&lt;/span&gt; year&lt;span class="sy0"&gt;,&lt;/span&gt; &lt;span class="kw4"&gt;struct&lt;/span&gt; DATE &lt;span class="sy0"&gt;*&lt;/span&gt; date&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;void&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;struct&lt;/span&gt; DATE date&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;br /&gt;setDate&lt;span class="br0"&gt;(&lt;/span&gt;24&lt;span class="sy0"&gt;,&lt;/span&gt;8&lt;span class="sy0"&gt;,&lt;/span&gt;2009&lt;span class="sy0"&gt;,&amp;amp;&lt;/span&gt;date&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;br /&gt;showDate&lt;span class="br0"&gt;(&lt;/span&gt;date&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;1&lt;/span&gt;&lt;span class="sy0"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; showDate&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;struct&lt;/span&gt; DATE d&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//Write your code here &lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; setDate&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; d&lt;span class="sy0"&gt;,&lt;/span&gt; &lt;span class="kw4"&gt;int&lt;/span&gt; m&lt;span class="sy0"&gt;,&lt;/span&gt; &lt;span class="kw4"&gt;int&lt;/span&gt; y&lt;span class="sy0"&gt;,&lt;/span&gt; &lt;span class="kw4"&gt;struct&lt;/span&gt; DATE &lt;span class="sy0"&gt;*&lt;/span&gt; date&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="co1"&gt;//Write your code here&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;&lt;b&gt;Program to print date in C++&lt;/b&gt; &lt;/p&gt; &lt;div dir="ltr" style="text-align: left;"&gt;&lt;div class="cpp source-cpp"&gt;&lt;pre class="de1"&gt;&lt;span class="co2"&gt;#include&lt;iostream&gt;//Input/Output stream Library&lt;/span&gt;&lt;br /&gt;&lt;span class="kw2"&gt;using&lt;/span&gt; &lt;span class="kw2"&gt;namespace&lt;/span&gt; std&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw4"&gt;struct&lt;/span&gt; Date&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; day&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; month&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; year&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; showDate&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span class="co1"&gt;//Write your code here&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;void&lt;/span&gt; setDate&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; d, &lt;span class="kw4"&gt;int&lt;/span&gt; m, &lt;span class="kw4"&gt;int&lt;/span&gt; y&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span class="co1"&gt;//Write your code here&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kw4"&gt;int&lt;/span&gt; main&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="kw4"&gt;void&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="br0"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="kw4"&gt;struct&lt;/span&gt; Date date&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;date.&lt;span class="me1"&gt;setDate&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;24,8,2009&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;date.&lt;span class="me1"&gt;showDate&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw3"&gt;system&lt;/span&gt;&lt;span class="br0"&gt;(&lt;/span&gt;&lt;span class="st0"&gt;"PAUSE"&lt;/span&gt;&lt;span class="br0"&gt;)&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="kw1"&gt;return&lt;/span&gt; &lt;span class="nu0"&gt;1&lt;/span&gt;&lt;span class="sy4"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class="br0"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt; &lt;p&gt;Resources: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.cs.uregina.ca/Links/class-info/cplusplus/Structure.html" class="external free" title="http://www.cs.uregina.ca/Links/class-info/cplusplus/Structure.html" rel="nofollow"&gt;http://www.cs.uregina.ca/Links/class-info/cplusplus/Structure.html&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;  &lt;p&gt;&lt;b&gt;2.&lt;/b&gt; Write the struct Date(of previous program) in separate namespace in C++ and use the Date structure in main to print the date. &lt;/p&gt;  &lt;a name="Problem_Set_C" id="Problem_Set_C"&gt;&lt;/a&gt;&lt;h3&gt; &lt;span class="mw-headline"&gt;Problem Set C&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;b&gt;Write the code in C++ for all the problems given in this problem set.&lt;/b&gt; &lt;/p&gt;&lt;p&gt;&lt;b&gt;1.&lt;/b&gt; STDIO: Read an integer value N from standard input.  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Print “Hai” for N times using a for-loop. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Resources:  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/doc/tutorial/program_structure.html" class="external free" title="http://www.cplusplus.com/doc/tutorial/program_structure.html" rel="nofollow"&gt;http://www.cplusplus.com/doc/tutorial/program_structure.html&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/doc/tutorial/basic_io.html" class="external free" title="http://www.cplusplus.com/doc/tutorial/basic_io.html" rel="nofollow"&gt;http://www.cplusplus.com/doc/tutorial/basic_io.html&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/doc/tutorial/control.html" class="external free" title="http://www.cplusplus.com/doc/tutorial/control.html" rel="nofollow"&gt;http://www.cplusplus.com/doc/tutorial/control.html&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;  &lt;p&gt;&lt;b&gt;2.&lt;/b&gt; STRINGS: use “string” variable  &lt;/p&gt; &lt;ul&gt;&lt;li&gt;Read your firstName  &lt;/li&gt;&lt;li&gt;Read your lastName &lt;/li&gt;&lt;li&gt;Use “+” string operator and contenate firstName and lastName with “_” in between.  &lt;/li&gt;&lt;li&gt;Display firstName_lastName on the screen &lt;/li&gt;&lt;li&gt;Use length() function of string class and display the length of “firstName_lastName” &lt;/li&gt;&lt;li&gt;Access the characters in the string “firstName_lastName” and display one by one separated by a single space &lt;/li&gt;&lt;li&gt;Ex: Display “Kishore_Prahallad” as “K i s h o r e _ P r a h a l l a d” &lt;/li&gt;&lt;li&gt;Display the characters in reverse order &lt;/li&gt;&lt;li&gt;Display as “d a l l a h a r P _ e r o h s i K” &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Resources &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html" class="external free" title="http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html" rel="nofollow"&gt;http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.cppreference.com/wiki/string/start" class="external free" title="http://www.cppreference.com/wiki/string/start" rel="nofollow"&gt;http://www.cppreference.com/wiki/string/start&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;  &lt;p&gt;&lt;b&gt;3.&lt;/b&gt; Using new and delete in C++ &lt;/p&gt; &lt;ol&gt;&lt;li&gt;New and Delete for integer:  &lt;/li&gt;&lt;/ol&gt; &lt;ul&gt;&lt;li&gt;Read an integer N &lt;/li&gt;&lt;li&gt;Create a 1-dimensional dynamic integer array using new[] for N integers &lt;/li&gt;&lt;li&gt;Read N integers from the standard input &lt;/li&gt;&lt;li&gt;Delete the allocated memory using delete[] &lt;/li&gt;&lt;/ul&gt; &lt;ol&gt;&lt;li&gt;New and Delete for Strings:  &lt;/li&gt;&lt;/ol&gt; &lt;ul&gt;&lt;li&gt;Read an integer N &lt;/li&gt;&lt;li&gt;Create a 1-dimensional dynamic string array using new[] for N strings &lt;/li&gt;&lt;li&gt;Read N strings from the standard input &lt;/li&gt;&lt;li&gt;Delete the allocated memory using delete[] &lt;/li&gt;&lt;/ul&gt; &lt;ol&gt;&lt;li&gt;New and Delete for 2-dimensional Matrix: &lt;/li&gt;&lt;/ol&gt; &lt;ul&gt;&lt;li&gt;Read two integers N and M &lt;/li&gt;&lt;li&gt;Create a 2-dimensional dynamic integer array using new[] &lt;/li&gt;&lt;li&gt;Read N x M integers from standard input &lt;/li&gt;&lt;li&gt;Delete using delete[] &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Use separate functions for integer, strings and 2D Matrix. &lt;/p&gt;&lt;p&gt;Resources: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.cplusplus.com/doc/tutorial/dynamic.html" class="external free" title="http://www.cplusplus.com/doc/tutorial/dynamic.html" rel="nofollow"&gt;http://www.cplusplus.com/doc/tutorial/dynamic.html&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7343566691466781160-9055186067668160194?l=knowaboutcpp.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/shEkR6xpLjPh7qciAAUp6KdOHtE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/shEkR6xpLjPh7qciAAUp6KdOHtE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/shEkR6xpLjPh7qciAAUp6KdOHtE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/shEkR6xpLjPh7qciAAUp6KdOHtE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/LearnCProgrammingWithin2Weeks/~4/YsAuAVjDGrM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://knowaboutcpp.blogspot.com/feeds/9055186067668160194/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://knowaboutcpp.blogspot.com/2009/09/blog-post.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/9055186067668160194?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7343566691466781160/posts/default/9055186067668160194?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/LearnCProgrammingWithin2Weeks/~3/YsAuAVjDGrM/blog-post.html" title="Basics" /><author><name>aditya</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total><feedburner:origLink>http://knowaboutcpp.blogspot.com/2009/09/blog-post.html</feedburner:origLink></entry></feed>

