|
|
| Object-Oriented Programming in C++ (4th Edition) (ISBN: 0672323087) |
 |
List Price: $44.99
Our Price: $31.49
Used Price: $22.00
Release Date: 19 December, 2001
Manufacturer: Sams (Paperback)
Sales Rank: 40,497
Author: Robert Lafore, Waite Group
|
More Info
|
|
|
Book Class
|
2002-05-16 21:40:18
|
| |
cpp
|
|
|
|
|
Category: source:cpp:classes
|
|
Description: A basic example of a class.
|
|
Platform: all
|
|
Author: detour
|
|
Viewed: 2661
|
|
Rating: 3/5 (5 votes)
|
|
|
| If you have any questions about
this piece of code or still need help, try posting your question on the forum. |
/* bkClass.cpp by detour@metalshell.com
*
* Very basic example of a class
*
* http://www.metalshell.com/
*
*/
#include <string>
#include <iostream>
class Book {
/* Private members of a class can never be altered
by anything but functions of the class. The data
is considered hidden. */
private:
int pages;
char ISBN[14];
/* All the functions needed to be called from outside
the class are declared as public. */
public:
/* Becuase only functions in the class can modify the
data you need to have set functions. Always perform
your checks for valid data in these functions. */
void setISBN(char *nISBN) {
if(strlen(nISBN) < 14)
strcpy(ISBN, nISBN);
}
void setPages(int npages) {
pages = npages;
}
/* A print out of the data */
void displayData() {
cout << "******* Book *******\n";
cout << "* ISBN: " << ISBN << endl;
cout << "* Pages: " << pages << endl;
cout << "**********************\n";
}
};
int main() {
Book myBook;
myBook.setISBN("0-671-04191-6");
myBook.setPages(296);
myBook.displayData();
/* These would not work becuase you can not read or
modified private data */
// cout << myBook.ISBN;
// pages = 500;
return 0;
}
|
|
|