C++/Class/Class Member
Содержание
Assign values using the member initialization syntax
<source lang="cpp">
- include <iostream>
using namespace std; class MyClass {
const int numA; // const member const int numB; // const member
public:
// Initialize numA and numB using initialization syntax. MyClass(int x, int y) : numA(x), numB(y) { } int getNumA() { return numA; } int getNumB() { return numB; }
}; int main() {
MyClass object1(7, 9), object2(5, 2); cout << "Values in object1 are " << object1.getNumB() << " and " << object1.getNumA() << endl; cout << "Values in object2 are " << object2.getNumB() << " and " << object2.getNumA() << endl; return 0;
}
</source>
enum field
<source lang="cpp">
- include <iostream>
using namespace std; enum Days { Monday, Tuesday, Wednesday, Thursday, Friday }; class MyClass {
public: int *lucky_number; enum Days lucky_day;
};
int main(void) {
MyClass wow; int lucky = 1500; wow.lucky_day = Monday; wow.lucky_number = &lucky; cout << *(wow.lucky_number) << endl; switch (wow.lucky_day) { case Monday: cout << "Monday\n"; break; default: cout << "My lucky day ain"t any day but Monday\n"; };
}
</source>
getName member function reads from the name member variable and the setName member function writes to it
<source lang="cpp">
- include <iostream>
- include <string>
using namespace std; class Person {
private: string name; int height; public: string getName() const; void setName(string); int getHeight() const; void setHeight(int);
};
string Person::getName() const { return name; } void Person::setName(string s) { if (s.length() == 0) name = "No name assigned"; else name = s; } int Person::getHeight() const { return height; } void Person::setHeight(int h) { if (h < 0) height = 0; else height = h; }
void setValues(Person&); void getValues(const Person&); int main () {
Person p1; setValues(p1); cout << "Outputting person data\n"; getValues(p1); return 0;
} void setValues(Person& pers) {
string str; int h; cout << "Enter person"s name: "; getline(cin,str); pers.setName(str); cout << "Enter height in inches: "; cin >> h; cin.ignore(); pers.setHeight(h);
} void getValues(const Person& pers) {
cout << "Person"s name: " << pers.getName() << endl; cout << "Person"s height in inches is: " << pers.getHeight() << endl;
}
</source>
The Member Initialization Syntax
<source lang="cpp">
- include <iostream>
using namespace std; class MyClass {
int numA; int numB;
public:
MyClass(int x, int y) { numA = x; numB = y; } int getNumA() { return numA; } int getNumB() { return numB; }
}; int main() {
MyClass object1(7, 9), object2(5, 2); cout << "Values in object1 are " << object1.getNumB() << " and " << object1.getNumA() << endl; cout << "Values in object2 are " << object2.getNumB() << " and " << object2.getNumA() << endl; return 0;
}
</source>
Use class name to reference field name
<source lang="cpp">
- include <iostream>
- include <iomanip>
- include <string.h>
- include <stdlib.h>
using namespace std; class Book {
public: Book(char *title, char *author, char *publisher, float price); Book(void); void show_title(void) { cout << title << "\n"; }; float get_price(void) { return(price); }; void show_book(void){ show_title(); show_publisher(); }; 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; }
Book::Book(void)
{ cerr << "You must specify initial values for Book instance\n"; exit(1); }
int main(void)
{ Book tips("A", "B","C", 49.95); Book diary; tips.show_book(); diary.show_book(); } </source>