C++练习笔记

  1. 类中的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;//永远只有一个
};
//double Account::interestRate=0.015; 初始化

int main()
{

    Account a("sam",1002);
    Account b("holiday",2000);

    return 0;
}

  1. 友元
#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;
        friend class Dog::foo(Screen &);
    private:
        string contents;
        int height,width;

};
class Window_Mgr  //窗口管理类,对Screen进行管理
{
    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;
}

  1. 构造函数
#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;
        }
    /* data */
};

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值