- 类中的Static
#include<iostream>
#include<string.h>
using namespace std;
class Account
{
public:
Account(string name,double money):owner(name),amount(money){};
double getAccount()const
{
return this->amount;
}
void deposit(double money)
{
this->amount+=money;
}
void applyint(){amount+=amount*interestRate;}
static double rate(){return interestRate;}
void rate(double newRate){this->interestRate=newRate;}
private:
string owner;
double amount;
static double interestRate;
};
int main()
{
Account a("sam",1002);
Account b("holiday",2000);
return 0;
}
- 友元
#include <iostream>
#include<string.h>
using namespace std;
class Screen;
class Dog
{
public:
int foo(Screen& Screen);
int koo(Screen& Screen);
};
int Dog::foo(Screen& Screen)
{
return Screen.height*Screen.width;
}
class Screen
{
public:
typedef string::size_type index;
Screen(int ht,int wd)
{
height=ht;width=wd;
area();
}
int area() const
{
return height*width;
}
friend int calArea(Screen &);
friend class Window_Mgr;
friend class Dog::foo(Screen &);
private:
string contents;
int height,width;
};
class Window_Mgr
{
public:
void relocate(int r,int c,Screen &s)
{
s.height+=r;
s.width+=c;
}
};
int calcArea(Screen & screen)
{
screen.area();
}
int main()
{
Screen a(60,100);
cout<<a.area()<<endl;
system("pause");
return 0;
}
- 构造函数
#include <stdio.h>
#include <string>
#include<iostream>
using namespace std;
struct Person
{
private:
string name;
string address;
public:
Person(const string &num,const string &addr)
{
this->name=num;
this->address=addr;
}
string getName() const
{
return this->name;
}
string getAddress() const
{
return address;
}
};
class Screen
{
public:
typedef string ::size_type index;
Screen(index ht=0,index wd=0):contents(ht*wd," "),cursor(0),height(ht),width(wd){}
char get() const{return contents[cursor];}
char get(index r,index c) const
{
index row=r*width;
return contents[row+c];
}
void move(index r,index c);
void set(char);
private:
string contents;
index cursor;
index height,width;
};
void Screen::move(index r,index c)
{
index row=r*width;
cursor=row+c;
}
void Screen::set(index r,index c,char ch)
{
index row=r*width;
contents[row+c]=ch;
}
void Screen::set(char c)
{
contents[cursor]=c;
}
int main()
{
Person p("Tom","呵呵呵哒");
cout<<p.getName()<<endl;
system("pause");
return 0;
}