Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > BOOK: Professional C++, 2nd Edition
|
BOOK: Professional C++, 2nd Edition
This is the forum to discuss the Wrox book Professional C++, 2nd Edition by Marc Gregoire, Nicholas A. Solter, Scott J. Kleper ; ISBN: 978-1-1181-6995-7
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C++, 2nd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 19th, 2016, 09:10 AM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 8 page 233, class SpreadsheetCell

The code for the SpreadsheetCell is as follows:
Code:
// SpreadsheetCell.cpp

#include <string>

class SpreadsheetCell
{
public:
	SpreadsheetCell();		    // default constructor
	virtual ~SpreadsheetCell();	   // destructor
	virtual void set(const std::string& inString) = 0; // setter for mString
	virtual std::string getString() const = 0;	     // getter for mString

};
The text indicates that the constructor is implemented just as a placeholder in case initialization needs to happen in the future. Why wasn't the constructor declared as virtual or pure virtual?

The text indicates that "SpreadsheetCell cell;" will not compile, and this is confirmed because I see the red squiggly line under "cell". But, why so, when there is a constructor?
 
Old April 20th, 2016, 03:43 PM
Wrox Author
 
Join Date: Mar 2011
Posts: 49
Thanks: 0
Thanked 4 Times in 4 Posts
Default

A constructor in C++ cannot be virtual and so also not pure virtual.
Since in this example, the constructor is not actually doing anything useful, you might as well remove it in this case.
However, as the text explains, the virtual destructor is required, even though it is empty.

SpreadsheetCell cell; will not compile because SpreadsheetCell is an abstract class because it contains pure virtual methods, namely getString() and set().
So, you can only create instances of subclasses that have an implementation for these pure virtual methods.
 
Old April 20th, 2016, 10:30 PM
Authorized User
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 8 page 233, class SpreadsheetCell

Thank you for the explanation.





Similar Threads
Thread Thread Starter Forum Replies Last Post
chapter two ...because it does not extend class 'System.Web.UI.Page' andres.gonzalez BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 4 September 25th, 2016 09:07 PM
Chapter 17 Defining a Sketch Class. Page 1043 porsche993997 BOOK: Ivor Horton's Beginning Visual C++ 2010 0 October 23rd, 2014 03:39 PM
Chapter 14, page 510 VB: Picture class is not recognized ASP4student BOOK: Beginning ASP.NET 4 : in C# and VB 7 November 25th, 2012 05:31 PM
Chapter 11 Deep copy Card Class,page 301 dampyr BOOK: Beginning Visual C# 2010 1 November 19th, 2011 03:59 PM
Chapter 7 example, p232-233, barkingdog BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 3 April 24th, 2009 03:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.