#include <iostream> using namespace std; class BankMember { private: static const int len = 30; char member_name[len]; char member_account[len]; double member_within; public: BankMember(); //default BankMember(const char *cn, const char *ca, double within = 0.0); ~BankMember(); void Show() const; void Within(double within); void CatchOut(double catchout); }; BankMember::BankMember() //default { strcpy(member_name, "no name"); strcpy(member_account, "no account"); member_within = 0.0; } BankMember::BankMember(const char *cn, const char *ca, double within) //只在声明处赋值 { strncpy(member_name, cn, 29); member_name[29] = '\0'; strncpy(member_account, ca, 29); member_account[29] = '\0'; member_within = within; } BankMember::~BankMember() { } void BankMember::Show() const { cout << "the name of the member is: "<< member_name << endl; cout << "the account of the member is: " << member_account << endl; cout << "the within of now is: " << member_within << endl; } void BankMember::Within(double within) { member_within = member_within + within; } void BankMember::CatchOut(double catchout) { //不允许透支 if(catchout > member_within) { cout << "the account is not enough"; } else member_within = member_within - catchout; } int main() { BankMember mem("xiaoke", "keke", 4344.3); mem.Within(43.5); mem.Show(); mem.CatchOut(43.5); mem.Show(); cin.get(); cin.get(); return 0; }
2.
/*2014 01 24 by arby*/ /* 类的初学初用*/ /*构造函数的应用举例:多个构造模式的构造函数的建立*/ #include <iostream> #include <string> using namespace std; class Person { private: static const int LIMIT = 25; string lname; char fname[LIMIT]; public: Person() {lname == " "; fname[0] = '\0';} Person(const string &ln, const char *fn = "Heyyou"); void Show() const; void FormalShow() const; }; Person::Person(const string &ln, const char *fn) { lname = ln; strncpy(fname, fn, 24); fname[25] = '\0'; } void Person::Show() const { cout << fname << " " << lname << endl; } void Person::FormalShow() const { cout << lname << ", " << fname << endl; } int main() { Person one; Person two("Smythecraft"); Person three("Dimwinddy", "Sam"); one.Show(); one.FormalShow(); two.Show(); two.FormalShow(); three.Show(); three.FormalShow(); cin.get(); cin.get(); return 0; }
4.
#ifndef GOLF_H #define GOLF_H namespace SALES { class SalesClass { private: static const int QUARTERS = 4; struct Sales { double sales[QUARTERS]; double average; double max; double min; }; Sales s; public: SalesClass(const double ar[], int n); SalesClass(); void showSales() const; }; }; #endif
#include <iostream> /*在系统中查找*/ using namespace std; #include "golf.h" /*在文件中查找*/ namespace SALES { SalesClass::SalesClass(const double ar[], int n) { int i = 0; int totle = 0; s.max = 0; if(n > 4) //n > 4 { for(i = 0; i < 4; i++) { s.sales[i]= ar[i]; totle = totle + s.sales[i]; if(s.sales[i] > s.max) s.max = s.sales[i]; } s.average = totle / 4; s.min = s.sales[0]; for(i = 0; i < 4; i++) { if(s.min > s.sales[i]) s.min = s.sales[i]; } } //end if else //n < 4 { for(i = 0; i < n; i++) { s.sales[i]= ar[i]; totle = totle + s.sales[i]; if(s.sales[i] > s.max) s.max = s.sales[i]; } s.average = totle / n; s.min = s.sales[0]; for(i = 0; i < n; i++) { if(s.min > s.sales[i]) s.min = s.sales[i]; } } // end else } SalesClass::SalesClass() { int i = 0; int totle = 0; s.max = 0; cout << "enter the 4 sales: " << endl; for(i = 0; i < 4; i++) { cin >> s.sales[i]; totle = totle + s.sales[i]; if(s.sales[i] > s.max) s.max = s.sales[i]; } s.min = s.sales[0]; for(i = 0; i < 4; i++) { if(s.min > s.sales[i]) s.min = s.sales[i]; } } void SalesClass::showSales() const { for(int i = 0; i < 4; i++) { cout << "the " << i << " is: "<< s.sales[i] << endl; } cout << "the average is: "<< s.average<< endl; cout << "the max is: "<< s.max << endl; cout << "the min is: " << s.min <<endl; } }
/*2014 01 24 by arby*/ /* 类的初学初用*/ /*名称空间以及类的构建*/ #include <iostream> #include "golf.h" using namespace std; using namespace SALES; int main() { SalesClass mem_a; double array_b[4] = {3, 43.34, 5,4}; SalesClass mem_b(array_b, 4); mem_a.showSales(); mem_b.showSales(); cin.get(); cin.get(); return 0; }
5.
// stack.h -- class definition for the stack ADT #ifndef STACK_H_ #define STACK_H_ struct customer { char fullname[35]; double payment; }; typedef customer Item; class Stack { private: enum {MAX = 10}; // constant specific to class Item items[MAX]; // holds stack items int top; // index for top stack item public: Stack(); bool isempty() const; bool isfull() const; // push() returns false if stack already is full, true otherwise bool push(const Item & item); // add item to stack // pop() returns false if stack already is empty, true otherwise bool pop(Item & item); // pop top into item }; #endif
// stack.cpp -- Stack member functions #include "stack.h" Stack::Stack() // create an empty stack { top = 0; } bool Stack::isempty() const { return top == 0; } bool Stack::isfull() const { return top == MAX; } bool Stack::push(const Item & item) { if (top < MAX) { items[top++] = item; return true; } else return false; } bool Stack::pop(Item & item) { if (top > 0) { item = items[--top]; return true; } else return false; }
/*2014 01 24 by arby*/ /* 类的初学初用*/ /*名称空间以及类的构建*/ #include <iostream> #include "stack.h" using namespace std; double sum_stack(Stack &sta, customer &cus, double sum); int main() { customer cus_a = {"xiaoke", 3000}; customer cus_b = {"xiaohe", 4000}; Stack stack; double count = 0; if(stack.isempty()) { stack.push(cus_a); } stack.push(cus_b); count = sum_stack(stack, cus_a, count); count = sum_stack(stack, cus_b, count); cout << "the sum of the payment is: " << count << endl; cin.get(); cin.get(); return 0; } double sum_stack(Stack &sta, customer &cus, double sum) { sta.pop(cus); sum = sum + cus.payment; return sum; }
6.
/*2014 01 2 4 by arby*/ /* 类的初学初用*/ /*名称空间以及类的构建*/ #include <iostream> #include "stack.h" using namespace std; class Move { private: double x; double y; public: Move(double a, double b); void showmove() const; Move add(const Move &m) const; void reset(double a = 0, double b = 0); }; Move::Move(double a, double b) { x = a; y = b; } void Move::showmove() const { cout << "the value is seperated: " << x << " " << y << endl; } Move Move::add(const Move&m) const { double new_x, new_y; new_x = x + m.x; new_y = y + m.y; Move new_move(new_x, new_y); return new_move; } void Move::reset(double a, double b) { x = a; y = b; } int main() { Move move_a(0, 4); Move move_b(43, 43.4); Move move_c = move_a.add(move_b); move_a.showmove(); move_b.showmove(); move_c.showmove(); move_b.reset(43.53, 43); move_b.showmove(); cin.get(); cin.get(); return 0; }
7.
/*2014 01 2 4 by arby*/ /* 类的初学初用*/ /*类的创建*/ #include <iostream> #include "stack.h" using namespace std; class plorg { private: static const int LIMIT = 19; char plorg_name[LIMIT]; int plorgCI; public: plorg(char *cn = "plorga", int CI = 50); void changeCI(int value); void showPlorg() const; }; plorg::plorg(char *cn, int CI) { strncpy(plorg_name, cn, 18); plorg_name[19] = '\0'; plorgCI = CI; } void plorg::changeCI(int value) { plorgCI = value; } void plorg::showPlorg() const { cout << "the name is: " << plorg_name << endl; cout << "the value is: " << plorgCI << endl; } int main() { plorg plorg_a("xiaohe", 43); plorg plorg_b; plorg_a.showPlorg(); plorg_b.showPlorg(); plorg_a.changeCI(2); plorg_a.showPlorg(); cin.get(); cin.get(); return 0; }
8.
// stack.h -- class definition for the stack ADT #ifndef LIST_H_ #define LIST_H_ typedef int Item; void AddOne(Item &item); class List { private: enum {MAX = 10}; // constant specific to class Item items[MAX]; int num; public: List(); bool isempty() const; bool isfull() const; bool push(const Item &item); void visit(void (*pf) (Item&)); }; #endif
// stack.cpp -- Stack member functions #include <iostream> #include "stack.h" using namespace std; List::List() // create an empty stack { num = 0; } bool List::isempty() const { return num == 0; } bool List::isfull() const { return num == MAX; } bool List::push(const Item &item) { if(num == MAX) return false; else { items[num] = item; num++; return true; } } void List::visit(void (*pf) (Item&)) { for(int i = 0; i < num; i++) { (*pf)(items[i]); cout << items[i] << endl; } } void AddOne(Item &item) { item = item + 1; }
/*2014 01 24 by arby*/ /* 类的初学初用*/ /*类的创建*/ #include <iostream> #include "stack.h" using namespace std; int main() { List list; int test_a = 3; int test_b = 4; list.push(test_a); list.push(test_b); list.visit(AddOne); cin.get(); cin.get(); return 0; }