0814,类和对象和构造函数喵(哈哈哈没有作业)


是好久没有躺过小沙发的我喵

目录

computer.cc

com_outside.cc

point.cc

com_01.hh/cc

com_02.hh/cc

computer.cc

#include <iostream>
#include <string.h>
using std::cout;
using std::endl;

//类  命名  大驼峰规则
class Computer{
public:
    //成员函数 命名 小驼峰规则
    void setBrand(const char* brand){
        strcpy(_brand,brand);
    }
    void setPrice(double price){
        _price =  price;
    }
    void printBrandPrice(){
        cout<<_brand<<"\t\t";
        cout<<_price<<endl;
    }
private:
    char _brand[20];
    double _price;
};

void test(){
    Computer pc;
    //不能用普通函数的调用形式
    /* setBrand("Lonove"); */
    
    //需要通过对象调用
    pc.setBrand("dale");
    pc.setPrice(7000);
    /* cout<<pc._brand<<"\t\t"; */
    /* cout<<pc._price<<endl; */

    pc.printBrandPrice();
}

int main(void)
{
    test();
    return 0;
}

com_outside.cc

#include <iostream>
#include <string.h>
using std::cout;
using std::endl;

class Computer{
public:
    void setBrand(const char* brand);
    void setPrice(double price);
    void printBrandPrice();
private:
    char _brand[20];
    double _price;
};

    void Computer::setBrand(const char* brand){
        strcpy(_brand,brand);
    }
    void Computer::setPrice(double price){
        _price =  price;
    }
    void Computer::printBrandPrice(){
        cout<<_brand<<"\t\t";
        cout<<_price<<endl;
    }
 
void test(){
    Computer pc;
    
    //需要通过对象调用
    pc.setBrand("dale");
    pc.setPrice(7000);

    pc.printBrandPrice();
}

int main(void)
{
    test();
    return 0;
}

point.cc

#include <iostream>
using std::cout;
using std::endl;

class Point{
public:
    /* void setX(int x){ _ix=x; } */
    /* void setY(int y){ _iy=y; } */
    Point(){
        _ix=0;
        _iy=0;
        cout<<"Point()"<<endl;}
    Point(int x,int y){
        _ix=x;
        _iy=y;
        cout<<"Point(int ,int )"<<endl;}
    void print(){ cout<<_ix<<"\t\t"<<_iy<<endl; }
private:
    int _ix;
    int _iy;
};
void test(){
    Point pt(1,2);
    Point pt1;
    /* pt.setX(10); */
    /* pt.setY(100); */
    pt.print();
    pt1.print();
}

int main(void)
{
    test();
    return 0;
}

com_01.hh/cc

#include <iostream>
#include <string.h>
class Computer{
public:
    void setBrand(const char* brand);
    void setPrice(double price);
    void printBrandPrice();
private:
    char _brand[20];
    double _price;
};

inline
void Computer::setBrand(const char* brand){
    strcpy(_brand,brand);
}
inline
void Computer::setPrice(double price){
    _price =  price;
}
inline
void Computer::printBrandPrice(){
    std::cout<<_brand<<"\t\t";
    std::cout<<_price<<std::endl;
}
#include "com_01.hh"
using std::endl;
using std::cout;

void test(){
    Computer pc;
    
    //需要通过对象调用
    pc.setBrand("dale");
    pc.setPrice(7000);

    pc.printBrandPrice();
}

int main(void)
{
    test();
    return 0;
}

com_02.hh/cc

#include <iostream>
#include <string.h>
using std::cout;
using std::endl;
class Computer{
public:
    //成员函数 命名 小驼峰规则
    void setBrand(const char* brand){
        strcpy(_brand,brand);
    }
    void setPrice(double price){
        _price =  price;
    }
    void printBrandPrice(){
        cout<<_brand<<"\t\t";
        cout<<_price<<endl;
    }
private:
    char _brand[20];
    double _price;
};
#include "com_02.hh"
using std::cout;
using std::endl;

void test(){
    Computer pc;
    
    //需要通过对象调用
    pc.setBrand("dale");
    pc.setPrice(7000);

    pc.printBrandPrice();
}

int main(void)
{
    test();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值