8.6 CONSTRUCTORS FOR TYPE STRING

The basic thing in any class is the constructor. We have studied constructor overloading. Hence, let us develop a class string with two constructors.

 

images

 

Figure 8.2 Pictorial view of our own class STRING

 

Problem: Develop two suitable constructors for class STRING.

Solution: In this class, we should be able to accept strings from the program. In C++, strings are handled with pointer to char. Hence we must have one constructor with standard string as input. There can be many other ways to construct STRING. Let us choose our second constructor as one which creates a null string. See Program 8.9.

 

Program 8.9 ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.