类的头文件:
#pragma once class bookstore { public: bookstore(void); ~bookstore(void); char book_name[10]; int book_price; static int book_num; int get_book_sum_price(); void save_info(char con_name[],int con_price); void dis_info(); private: static int totle_price; };
cpp:
#include "stdafx.h" #include "bookstore.h" #include <iostream> #include <string> using namespace std; int bookstore::book_num=0; int bookstore::totle_price=0; bookstore::bookstore(void) { } bookstore::~bookstore(void) { } void bookstore::save_info(char con_name[10],int con_price) { strcpy_s(book_name,10,con_name); book_price=con_price; book_num++; totle_price=totle_price+con_price; } int bookstore::get_book_sum_price() { return totle_price; } void bookstore::dis_info() { cout<<"书名:"<<book_name<<endl; cout<<"书价:"<<book_price<<endl; cout<<"总数:"<<book_num<<endl; cout<<"总价:"<<bookstore::get_book_sum_price()<<endl; }
主文件:
#include "stdafx.h" #include<iostream> #include"bookstore.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { bookstore a; int key=0; for(int i=0;i<100;i++) { cout<<"请输入书名和书价"<<endl; cin>>a.book_name>>a.book_price; a.save_info(a.book_name,a.book_price); a.dis_info(); cout<<"按“1”继续录入"<<endl; while (1) { cin>>key; if(key==1) break; } } return 0; }