是好久没有躺过小沙发的我喵
目录
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;
}