C++/Class/Instance
Содержание
class array and object method call
<source lang="cpp">
- include <iostream>
- include <iomanip>
- include <string.h>
using namespace std; class Book {
public: void show_title(void) { cout << title << "\n"; }; void show_book(void) { show_title(); show_publisher(); }; void assign_members(char *, char *, char *, float); private: char title[256]; char author[64]; float price; char publisher[256]; void show_publisher(void) { cout << publisher << "\n"; };
};
void Book::assign_members(char *title, char *author, char *publisher,float price) {
strcpy(Book::title, title); strcpy(Book::author, author); strcpy(Book::publisher, publisher); Book::price = price;
}
int main(void) {
Book Library[4]; Library[0].assign_members("A", "B","C", 49.95); Library[1].assign_members("D", "E", "F", 54.95); Library[2].assign_members("G", "H","I", 49.95); Library[3].assign_members("J", "K","L", 24.95); for (int i = 0; i < 4; i++) Library[i].show_book();
}
</source>
Construct object in an object array one by one
<source lang="cpp">
- include <iostream>
- include <iomanip>
- include <string.h>
using namespace std; class Book {
public: void show_title(void) { cout << title << "\n"; }; void show_book(void) { show_title(); show_publisher(); }; Book(char *title, char *author, char *publisher, float price); private: char title[256]; char author[64]; float price; char publisher[256]; void show_publisher(void) { cout << publisher << "\n"; };
}; Book::Book(char *title, char *author, char *publisher, float price)
{ strcpy(Book::title, title); strcpy(Book::author, author); strcpy(Book::publisher, publisher); Book::price = price; cout << "In constructor." << endl; }
int main(void)
{ Book *Library[4]; int i; Library[0] = new Book("A", "B","C", 49.95); Library[1] = new Book("D", "E", "F", 54.95); Library[2] = new Book("G", "H","I", 49.95); Library[3] = new Book("K", "J","P", 29.95); for (i = 0; i < 4; i++) Library[i]->show_book(); } </source>
Declare the instance variable for a class
<source lang="cpp">
- include <iostream>
using namespace std; class Power {
double b; int e; double val;
public:
Power(double base, int exp); double getPower() { return val; }
}; Power::Power(double base, int exp) {
b = base; e = exp; val = 1; if(exp == 0) return; for( ; exp > 0; exp--) val = val * b;
} int main() {
Power x(4.0, 2), y(2.5, 1), z(5.7, 0); cout << x.getPower() << " "; cout << y.getPower() << " "; cout << z.getPower() << endl; return 0;
}
</source>
Loop through an object array and call its method
<source lang="cpp">
- include <iostream>
- include <iomanip>
- include <string.h>
using namespace std; class Book {
public: void show_title(void) { cout << title << "\n"; }; void show_book(void) { show_title(); show_publisher(); }; Book(char *title, char *author, char *publisher, float price); ~Book(void) { cout << "Destroying the entry for " << title << endl; }; private: char title[256]; char author[64]; float price; char publisher[256]; void show_publisher(void) { cout << publisher << "\n"; };
}; Book::Book(char *title, char *author, char *publisher, float price) {
strcpy(Book::title, title); strcpy(Book::author, author); strcpy(Book::publisher, publisher); Book::price = price;
} int main(void) {
Book *Library[4]; int i = 0; Library[0] = new Book("A", "B","C", 49.95); Library[1] = new Book("D", "E", "F", 54.95); Library[2] = new Book("G", "H","I", 49.95); Library[3] = new Book("J", "K","P", 24.95); for (i = 0; i < 4; i++) Library[i]->show_book(); for (i = 0; i < 4; i++) delete Library[i];
}
</source>