思维导图:
代码示例:
# include <iostream>
using namespace std;
int i;
namespace Ns {
int j;
}
int main ( ) {
i = 5 ;
Ns:: j = 6 ;
{
using namespace Ns;
int i;
i = 7 ;
cout << "i = " << i << endl;
cout << "j = " << j << endl;
}
cout << "i = " << i << endl;
return 0 ;
}
# include <iostream>
using namespace std;
int i = 1 ;
void other ( ) {
static int a = 2 ;
static int b;
int c = 10 ;
a += 2 ;
i += 32 ;
c += 5 ;
cout << "---OTHER---" << endl;
cout << " i: " << i << " a: " << a << " b: " << b << " c: " << c << endl;
b = a;
}
int main ( ) {
static int a;
int b = - 10 ;
int c = 0 ;
cout << "---MAIN---" << endl;
cout << " i: " << i << " a: " << a << " b: " << b << " c: " << c << endl;
c += 8 ;
other ( ) ;
cout << "---MAIN---" << endl;
cout << " i: " << i << " a: " << a << " b: " << b << " c: " << c << endl;
i += 10 ;
other ( ) ;
return 0 ;
}
# include <iostream>
using namespace std;
class Clock {
public :
Clock ( ) ;
void setTime ( int newH, int newM, int newS) ;
void showTime ( ) ;
private :
int hour, minute, second;
} ;
Clock :: Clock ( ) : hour ( 0 ) , minute ( 0 ) , second ( 0 ) { }
void Clock :: setTime ( int newH, int newM, int newS) {
hour = newH;
minute = newM;
second = newS;
}
void Clock :: showTime ( ) {
cout << hour << ":" << minute << ":" << second << endl;
}
Clock globClock;
int main ( ) {
cout << "First time output:" << endl;
globClock. showTime ( ) ;
globClock. setTime ( 8 , 30 , 30 ) ;
Clock myClock ( globClock) ;
cout<< "Second time output:" << endl;
myClock. showTime ( ) ;
return 0 ;
}
# include <iostream>
using namespace std;
class Point {
public :
Point ( int x = 0 , int y = 0 ) : x ( x) , y ( y) {
count++ ;
}
Point ( Point & p) {
x = p. x;
y = p. y;
count++ ;
}
~ Point ( ) { count-- ; }
int getX ( ) { return x; }
int getY ( ) { return y; }
void showCount ( ) {
cout << " Object count = " << count << endl;
}
private :
int x, y;
static int count;
} ;
int Point:: count = 0 ;
int main ( ) {
Point a ( 4 , 5 ) ;
cout << "Point A: " << a. getX ( ) << ", " << a. getY ( ) ;
a. showCount ( ) ;
Point b ( a) ;
cout << "Point B: " << b. getX ( ) << ", " << b. getY ( ) ;
b. showCount ( ) ;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
using namespace std;
class Point {
public :
Point ( int x = 0 , int y = 0 ) : x ( x) , y ( y) {
count++ ;
}
Point ( Point & p) {
x = p. x;
y = p. y;
count++ ;
}
~ Point ( ) { count-- ; }
int getX ( ) { return x; }
int getY ( ) { return y; }
static void showCount ( ) {
cout << " Object count = " << count << endl;
}
private :
int x, y;
static int count;
} ;
int Point:: count = 0 ;
int main ( ) {
Point :: showCount ( ) ;
Point a ( 4 , 5 ) ;
cout << "Point A: " << a. getX ( ) << ", " << a. getY ( ) ;
Point :: showCount ( ) ;
a. showCount ( ) ;
Point b ( a) ;
cout << "Point B: " << b. getX ( ) << ", " << b. getY ( ) ;
b. showCount ( ) ;
Point :: showCount ( ) ;
system ( "pause" ) ;
return 0 ;
}
# include <iostream>
# include <cmath>
using namespace std;
class Point {
public :
Point ( int x = 0 , int y = 0 ) : x ( x) , y ( y) { }
int getX ( ) { return x; }
int getY ( ) { return y; }
friend float dist ( Point & p1, Point & p2) ;
private :
int x, y;
} ;
float dist ( Point & p1, Point & p2) {
double x = p1. x - p2. x;
double y = p1. y - p2. y;
return static_cast < float > ( sqrt ( x * x + y * y) ) ;
}
int main ( ) {
Point myp1 ( 1 , 3 ) , myp2 ( 5 , 5 ) ;
cout << "The distance is: " ;
cout << dist ( myp1, myp2) << endl;
return 0 ;
}
# include <iostream>
using namespace std;
class R {
public :
R ( int r1, int r2) : r1 ( r1) , r2 ( r2) { }
void print ( ) ;
void print ( ) const ;
private :
int r1, r2;
} ;
void R :: print ( ) {
cout << r1 << ":" << r2 << endl;
}
void R :: print ( ) const {
cout << r1 << ";" << r2 << endl;
}
int main ( ) {
R a ( 5 , 4 ) ;
a. print ( ) ;
const R b ( 8 , 24 ) ;
b. print ( ) ;
return 0 ;
}
# include <iostream>
using namespace std;
class A {
public :
A ( int i) ;
void print ( ) ;
private :
const int a;
static const int b;
} ;
const int A:: b = 10 ;
A :: A ( int i) : a ( i) { }
void A :: print ( ) {
cout << a << ":" << b << endl;
}
int main ( ) {
A a1 ( 55 ) , a2 ( 0 ) ;
a1. print ( ) ;
a2. print ( ) ;
return 0 ;
}
# include <iostream>
# include <cmath>
using namespace std;
class Point {
public :
Point ( int x = 0 , int y = 0 ) : x ( x) , y ( y) { }
int getX ( ) { return x; }
int getY ( ) { return y; }
friend float dist ( const Point & p1, const Point & p2) ;
private :
int x, y;
} ;
float dist ( const Point & p1, const Point & p2) {
double x = p1. x - p2. x;
double y = p1. y - p2. y;
return static_cast < float > ( sqrt ( x * x + y * y) ) ;
}
int main ( ) {
const Point myp1 ( 1 , 1 ) , myp2 ( 8 , 24 ) ;
cout << "The distance is: " ;
cout << dist ( myp1, myp2) << endl;
return 0 ;
}
# ifndef CLIENT_H_
# define CLIENT_H_
# include <string>
using namespace std;
class Dog {
private :
string name;
string weight;
string color;
public :
void Info ( string name, string weight, string color) ;
void show_info ( ) ;
void sit ( ) ;
void roll ( ) ;
} ;
# endif
# include "Dog.h"
# include <iostream>
using namespace std;
void Dog :: Info ( string new_name, string new_weight, string new_color) {
Dog:: name = new_name;
Dog:: weight = new_weight;
Dog:: color = new_color;
}
void Dog :: show_info ( ) {
cout<< " This dog's name is " << name << "and its " << weight << " with " << color << endl;
}
void Dog :: sit ( ) {
cout<< " This dog has sitted down. " << endl;
}
void Dog :: roll ( ) {
cout<< " This dog's is rolling now!" << endl;
}
# include <iostream>
# include "Dog.h"
# include "Dog.cpp"
using namespace std;
int main ( ) {
Dog myDog;
myDog. Info ( "Timi" , "30" , "write" ) ;
myDog. show_info ( ) ;
myDog. sit ( ) ;
myDog. roll ( ) ;
return 0 ;
}
# ifndef __ACCOUNT_H__
# define __ACCOUNT_H__
class SavingsAccount {
private :
int id;
double balance;
double rate;
int lastDate;
double accumulation;
static double total;
void record ( int date, double amount) ;
double accumulate ( int date) const {
return accumulation + balance * ( date - lastDate) ;
}
public :
SavingsAccount ( int date, int id, double rate) ;
int getId ( ) const { return id; }
double getBalance ( ) const { return balance; }
double getRate ( ) const { return rate; }
static double getTotal ( ) { return total; }
void deposit ( int date, double amount) ;
void withdraw ( int date, double amount) ;
void settle ( int date) ;
void show ( ) const ;
} ;
# endif
# include "account.h"
# include <cmath>
# include <iostream>
using namespace std;
double SavingsAccount:: total = 0 ;
SavingsAccount :: SavingsAccount ( int date, int id, double rate)
: id ( id) , balance ( 0 ) , rate ( rate) , lastDate ( date) , accumulation ( 0 ) {
cout << date << "\t#" << id << " is created" << endl;
}
void SavingsAccount :: record ( int date, double amount) {
accumulation = accumulate ( date) ;
lastDate = date;
amount = floor ( amount * 100 + 0.5 ) / 100 ;
balance += amount;
total += amount;
cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
}
void SavingsAccount :: deposit ( int date, double amount) {
record ( date, amount) ;
}
void SavingsAccount :: withdraw ( int date, double amount) {
if ( amount > getBalance ( ) )
cout << "Error: not enough money" << endl;
else
record ( date, - amount) ;
}
void SavingsAccount :: settle ( int date) {
double interest = accumulate ( date) * rate / 365 ;
if ( interest != 0 )
record ( date, interest) ;
accumulation = 0 ;
}
void SavingsAccount :: show ( ) const {
cout << "#" << id << "\tBalance: " << balance;
}
# include "account.h"
# include <iostream>
using namespace std;
int main ( ) {
SavingsAccount sa0 ( 1 , 21325302 , 0.015 ) ;
SavingsAccount sa1 ( 1 , 58320212 , 0.015 ) ;
sa0. deposit ( 5 , 5000 ) ;
sa1. deposit ( 25 , 10000 ) ;
sa0. deposit ( 45 , 5500 ) ;
sa1. withdraw ( 60 , 4000 ) ;
sa0. settle ( 90 ) ;
sa1. settle ( 90 ) ;
sa0. show ( ) ; cout << endl;
sa1. show ( ) ; cout << endl;
cout << "Total: " << SavingsAccount :: getTotal ( ) << endl;
return 0 ;
}