"Learning C + +" C + + Primer Plus (Sixth edition) Chapter tenth programming Exercise 1-8

Source: Internet
Author: User

1.
#include <iostream> #include <string>class account{private:std::string name;std::string number;double Deposit;public:account (const std::string & na= "No Name", const std::string & nu= "0", int de=0); void show (); void Add (double dep); void remove (double dep);}; Account::account (const std::string & na, const std::string & nu, int de) {name = Na;number = Nu;deposit =de;} void Account::show () {std::cout << "name:" << name << "Number:" << number << "Deposi T: "<< deposit << Std::endl;} void Account::add (double dep) {if (DEP <= 0) std::cout << "deposit added can not be negative.\n"; Elsedeposit + = DEP ;} void Account::remove (double dep) {if (DEP < 0) std::cout << "deposit removed can not is negative.\n"; else if (DEP & Gt Deposit) Std::cout << "You can not remove more than you have!\n"; elsedeposit-= dep;} int main () {account a;a.show (); a = account ("Xiaoming", "000111", 0); A.show (); A.add (a.show); A.remove (4); A.show (); Account B ("Xiaohua", "111000", b.show); B.show (); B.add (+); B.remove (); B.show (+); Std::cin.get (); (); return 0;}
2.
#include <iostream> #include <string>class person{private:static const int LIMIT = 25;std::string Lname;char Fname[limit];p Ublic:person () {lname = ""; Fname[0] = ' + '; }person (const std::string & LN, const char * fn = "heyyou"), void Show () const;void formalshow () const;}; Person::P Erson (const std::string & LN, const char * fn) {lname = ln;strcpy (fname, FN);} void Person::show () const{std::cout << fname << "<< lname;} void Person::formalshow () const{std::cout << lname << "," << fname << Std::endl;} int main () {person one; Person ("Smythecraft"); Person three ("Dimwiddy", "Sam"); Three.show (); Std::cout << Std::endl;three. Formalshow (); Std::cin.get (); return 0;}

3.

Golf.h#ifndef golf_h_#define golf_h_class golf{private:static const int LEN = 40;char Fullname[len];int Handicap; Public:golf (const char * name= "No name", int hc=0); void Setgolf (); void Handi (int hc); void Showgolf ();}; #endif
Golf.cpp#include <iostream> #include "golf.h" Golf::golf (const char * name,int HC) {strcpy (fullname, name); Handicap = HC;} void Golf::setgolf () {char name[len];int handi;std::cout << "Please input name hand handicap:\n"; Std::cin.getline ( Name, LEN); Std::cin >> handi;std::cin.get (); *this = Golf (name, Handi);} void Golf::handi (int hc) {handicap = HC;} void Golf::showgolf () {std::cout << fullname << ":" << Handicap<<std::endl;}
Main.cpp#include <iostream> #include "golf.h" int main () {golf a[4];for (int i = 0; i < 4; i++) {A[i].showgolf ();} for (int i = 0; i < 4; i++) {A[i].setgolf ();} for (int i = 0; i < 4; i++) {A[i].showgolf ();} Std::cin.get (); return 0;}
4.

Sales.h#ifndef sales_h_#define sales_h_namespace sales{class sales{private:static const int QUARTERS = 4;double sales[ Quarters];d ouble average;double max;double min;public:sales (const double AR [], int n); void Setsales (); void Showsales ();} ;} #endif
<pre name= "code" class= "CPP" >//sales.cpp#include <iostream> #include "sales.h" namespace Sales{sales:: Sales (const double AR [], int n) {int l;l = n > 4? 4:n;for (int i =0; i < L; i++) {sales[i] = ar[i];}  if (L < 4) for (int i = l; i < 4; i++) sales[i] = 0;double sum = sales[0];d ouble ma = sales[0], MI = sales[0];for (int i = 1; I < 4; i++) {sum + = Sales[i];ma = Ma>sales[i]? ma:sales[i];mi = Mi < sales[i]? Mi:sales[i];} Average = Sum/4;max = Ma;min = mi;}  void Sales::setsales () {double ar[4];std::cout << "Please enter 4 numbers: \ n"; for (int i = 0; i < 4; i++) std::cin >> ar[i];*this = Sales (AR, 4);} void Sales::showsales () {for (int i = 0; i < 4; i++) std::cout << sales[i] << ""; Std::cout << Std::end L;std::cout << "average:" << average << std::endl;std::cout << "max number:" <<max <&lt ; Std::endl;std::cout << "min number:" << min << std::endl;}}

Main.cpp#include <iostream> #include "sales.h" int main () {double ar[4] = {0.0, 1.0, 2.0, 3.0}; Sales::sales s (AR, 4); S.showsales (); S.setsales (); S.showsales (); Std::cin.get (); Std::cin.get (); return 0;}
5.

Stack.h#ifndef stack_h_#define stack_h_struct Customer{char fullname[35];d ouble payment;}; typedef customer Item;class stack{private:enum{MAX =};item items[max];int top;public:stack (); bool IsEmpty () Const;bo Ol isfull () Const;bool push (const item & Item); bool Pop (item & item);}; #endif
Stack.cpp#include <iostream> #include "stack.h" Stack::stack () {top = 0;} BOOL Stack::isempty () const{return top = = 0;} BOOL Stack::isfull () const{return top = = MAX;} BOOL Stack::p ush (const item & Item) {if (Top < MAX) {items[top++] = Item;return true;} Elsereturn false;} BOOL Stack::p OP (item & Item) {if (Top > 0) {Item = Items[--top];return true;} Elsereturn false;}
Main.cpp#include <iostream> #include "stack.h" int main () {stack s;double sum = 0;customer C[3] = {{"Liujiayu", 35 }, {"Wagnrunze", Max}, {"Wanghaojian", Ten}};for (int i = 0; i < 3; i++) S.push (C[i]); for (int i = 0; i < 3; i++) {S.pop (c[i]); sum + = C[i].payment;std::cout << "sum:" << sum << Std::endl;} Std::cin.get (); return 0;}
6.

#include <iostream>class move{private:double x;double y;public:move (Double A = 0, double b = 0); void Showmove () cons T 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{std::cout << "x:" << x << "   y:" << Y<<std::endl;} Move move::add (const Move & M) const{move m; m.x = x + m.x; M.y = y + m.y;return m;} void Move::reset (Double A, double b) {x = A;y = b;} int main () {Move A (2,3); A.showmove (); Move B (4, 5); B.showmove (); Move C;c.showmove (); c= A.add (b); C.showmove (); A.reset (3, 7); A.showmove (); A.reset (); A.showmove (); Std::cin.get (); return 0;}

7.

#include <iostream>class plorg{private:char fullname[20];int ci;public:plorg (const char * name = "Plorga"); void SETCI (int c); void showplorg ();}; plorg::P lorg (const char *name) {strcpy (fullname, name); CI = 50;} void Plorg::setci (int c) {CI = C;} void plorg::showplorg () {std::cout << "name:" << fullname << "   ci:" << ci << std::endl; }int Main () {plorg p;p.showplorg ();p = plorg ("Liujiayu");p. showplorg ();p. SETCI (;p). showplorg (), Std::cin.get (); return 0;}
8.

List.h#ifndef list_h_#define list_h_typedef unsigned int item;class list{private:enum{MAX = ten};item items[MAX];int nu M;public:list (); void Add (const Item &), bool IsEmpty () Const;bool isfull () const;void visit (void (*PF) (Item &));}; #endif
List.cpp#include <iostream> #include "list.h" list::list () {num = 0;} void List::add (const Item & A) {if (num < MAX) {items[num++] = A;} Elsestd::cout << "The list is full\n";} BOOL List::isempty () const{return num = = 0;} BOOL List::isfull () const{return num = = MAX;} void List::visit (void (*PF) (Item &)) {for (int i = 0; i < num; i++) PF (items[i]);}
Main.cpp#include <iostream> #include "list.h" void Show (Item &); int main () {list L;int i = 0;while (!l.isfull () {L.add (i*10); i++;} L.visit (show); Std::cin.get (); return 0;} void Show (Item &a) {std::cout << a <<std::endl;}














"Learning C + +" C + + Primer Plus (Sixth edition) Chapter tenth programming Exercise 1-8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.