C++/Class/Class Basics
Содержание
Address class: class definition and implementation
<source lang="cpp">
- include <iostream>
- include <cstring>
using namespace std; class Address {
char name[40]; char street[40]; char city[30]; char state[3]; char zip[10];
public:
void store(char *n, char *s, char *c, char *t, char *z); void display();
}; void Address::store(char *n, char *s, char *c, char *t, char *z) {
strcpy(name, n); strcpy(street, s); strcpy(city, c); strcpy(state, t); strcpy(zip, z);
} void Address::display() {
cout << name << endl; cout << street << endl; cout << city << endl; cout << state << endl; cout << zip << endl;
} int main() {
Address a; a.store("C", "11 Lane", "W", "In", "4"); a.display(); return 0;
}
</source>
A simple class with member variable, constructor, destructor
<source lang="cpp">
- include <iostream>
using namespace std; class who {
char name;
public:
who(char c) { name = c; cout << "Constructing who"; cout << name << endl; } ~who() { cout << "Destructing who: " << name << endl; }
}; who makewho() {
who temp("B"); return temp;
} int main() {
who ob("A"); makewho(); return 0;
}
</source>
Assign object1 to object2
<source lang="cpp">
- include <iostream>
using namespace std; class BaseClass {
int a;
public:
void load_a(int n) { a = n; } int get_a() { return a; }
}; class DerivedClass : public BaseClass {
int b;
public:
void load_b(int n) { b = n; } int get_b() { return b; }
}; int main() {
DerivedClass object1, object2; object1.load_a(5); object1.load_b(10); object2 = object1; cout << "Here is object1"s a and b: "; cout << object1.get_a() << " " << object1.get_b() << endl; cout << "Here is object2"s a and b: "; cout << object2.get_a() << " " << object2.get_b() << endl; return 0;
}
</source>
Class forward declaration
<source lang="cpp">
- include <iostream>
using namespace std; class pr2; // class pr1 {
int printing;
public:
pr1() { printing = 0; } void set_print(int status) { printing = status; } friend int inuse(pr1 object1, pr2 object2);
}; class pr2 {
int printing;
public:
pr2() { printing = 0; } void set_print(int status) { printing = status; } friend int inuse(pr1 object1, pr2 object2);
}; int inuse(pr1 object1, pr2 object2) {
if(object1.printing || object2.printing) return 1; else return 0;
} int main() {
pr1 p1; pr2 p2; if(!inuse(p1, p2)) cout << "Printer idle\n"; cout << "Setting p1 to printing...\n"; p1.set_print(1); if(inuse(p1, p2)) cout << "Now, printer in use.\n"; cout << "Turn off p1...\n"; p1.set_print(0); if(!inuse(p1, p2)) cout << "Printer idle\n"; cout << "Turn on p2...\n"; p2.set_print(1); if(inuse(p1, p2)) cout << "Now, printer in use.\n"; return 0;
}
</source>
Declare class instance
<source lang="cpp">
- include <iostream>
using namespace std; class myclass {
int a;
public:
myclass(int x) { a = x; } int geta() { return a; }
};
int main() {
myclass ob(4); cout << ob.geta(); return 0;
}
</source>
Declare Class instance and use them
<source lang="cpp">
- include <iostream>
- include <cstring>
using namespace std; class Book {
char title[80]; // book title char author[40]; // author int number; // number in library
public:
void store(char *t, char *name, int num); void show();
}; void Book::store(char *t, char *name, int num) {
strcpy(title, t); strcpy(author, name); number = num;
} void Book::show() {
cout << "Title: " << title << endl; cout << "Author: " << author << endl; cout << "Number on hand: " << number << endl;
} int main() {
Book book1, book2, book3; book1.store("D", "t", 2); book2.store("T", "v", 2); book3.store("T", "e", 1); book1.show(); book2.show(); book3.show(); return 0;
}
</source>
Init Object array
<source lang="cpp">
- include <iostream>
using namespace std; class CharClass {
char ch;
public:
CharClass(char c) { ch = c; } char get_ch() { return ch; }
}; int main() {
CharClass ob[10] = { CharClass("a"), CharClass("b"), CharClass("c"), CharClass("d"), CharClass("e"), CharClass("f"), CharClass("g"), CharClass("h"), CharClass("i"), CharClass("j") }; int i; for(i = 0; i <10; i++) cout << ob[ i ].get_ch() << " "; cout << endl; return 0;
}
</source>
Simplest class definition
<source lang="cpp">
- include <iostream>
using namespace std; class SimpleClass {
int len;
public:
SimpleClass(int l);
}; SimpleClass::SimpleClass(int l) {
len = l; int i; for(i = 0; i <len; i++) cout << "*";
} int main() {
SimpleClass l(10); return 0;
}
</source>