Sample easy c++ class


This sample have 3 files.

Computer.h : This is header file define for member variable, function and set access right.
Computer.cpp : This file is detail function for Computer.h, But Computer.cpp must insert line #include “Computer.h” on header of file.
TestClass.cpp: This is the main file for run code. It’s contain main function

Computer.h

   1: #pragma once
   2: class Computer
   3: {
   4: private:
   5:     int ram;
   6:     double cpu;
   7:     int hdd;
   8: public:
   9:     Computer(void);
  10:     Computer(int ram,double cpu,int hdd);
  11:     ~Computer(void);
  12:     int GetRam();
  13:     void SetRam(int ram);
  14:     double GetCpu();
  15:     void SetCpu(double cpu);
  16:     int GetHdd();
  17:     void SetHdd(int hdd);
  18: };
  19:
Computer.cpp
   1: #include "StdAfx.h"
   2: #include "Computer.h"
   3:
   4:
   5: Computer::Computer(void)
   6: {
   7:     this->cpu=2.2;
   8:     this->ram=2;
   9:     this->hdd=360;
  10: }
  11: Computer::Computer(int ram,double cpu,int hdd)
  12: {
  13:     this->ram = ram;
  14:     this->cpu = cpu;
  15:     this->hdd = hdd;
  16: }
  17: Computer::~Computer(void)
  18: {
  19: }
  20: double Computer::GetCpu(void)
  21: {
  22:     return this->cpu;
  23: }
  24: int Computer::GetRam(void)
  25: {
  26:     return this->ram;
  27: }
  28: int Computer::GetHdd(void)
  29: {
  30:     return this->hdd;
  31: }
  32: void Computer::SetRam(int ram)
  33: {
  34:     this->ram = ram;
  35: }
  36: void Computer::SetCpu(double cpu)
  37: {
  38:     this->cpu = cpu;
  39: }
  40: void Computer::SetHdd(int hdd)
  41: {
  42:     this->hdd = hdd;
  43: }

TestClass.cpp

   1: #include "stdafx.h"
   2: #include "TestClass.h"
   3: #include "Computer.h"
   4:
   5: using namespace std;
   6:
   7: int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
   8: {
   9:     int nRetCode = 0;
  10:
  11:     Computer PC1;
  12:     Computer PC2(4,3.2,500);
  13:     std::cout<<"PC1: CPU="<<PC1.GetCpu()<<"GHz Ram="<<PC1.GetRam()<<"GB HDD="<<PC1.GetHdd()<<"GB"<<endl;
  14:     std::cout<<"PC2: CPU="<<PC2.GetCpu()<<"GHz Ram="<<PC2.GetRam()<<"GB HDD="<<PC2.GetHdd()<<"GB"<<endl;
  15:     PC1.SetCpu(3.2);
  16:     std::cout<<"PC1: CPU="<<PC1.GetCpu()<<"GHz Ram="<<PC1.GetRam()<<"GB HDD="<<PC1.GetHdd()<<"GB"<<endl;
  17:     system("pause");
  18:
  19:     return nRetCode;
  20: }

OUTPUT

   1: PC1: CPU=2.2GHz Ram=2GB HDD=360GB
   2: PC2: CPU=3.2GHz Ram=4GB HDD=500GB
   3: PC1: CPU=3.2GHz Ram=2GB HDD=360GB
   4: Press any key to continue . . .

ติดป้ายกำกับ:

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s

%d bloggers like this: