faqts : Computers : Programming : Languages : C++

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

13 of 17 people (76%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

C++: Class: Create: Simple: How to create a class in C++?

Sep 27th, 2003 06:39
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 26 September 2003 - 10:55 - 11.50 pm ----------

C++: Class: Create: Simple: How to create a class in C++?

A class is zero or more functions, procedures or variables,
grouped together, with a common name (e.g. addressC).

---

Inside you tell about their access (e.g. friend, private, public,
protected).

http://www.faqts.com/knowledge_base/view.phtml/aid/24762/fid/163

---

There are also zero or more special procedures to initialize
(called 'constructors').

---

And zero or more special procedures (but usually exactly one) to 
remove it
(called 'destructors') when finished.

---

This class information will be stored in 1 specific location in memory.

---

To use your class, you first assign it to a variable.

---

Note:

This process of creating you can repeat zero or more times, and each
time a new location in memory with the same memory structure as that of
the given class (in that single memory location) will be assigned to
the variable associated with that class.

So all this variables can be seen as initially having the same
structure (=variables, and a reference to the 1 memory location where
that functions and procedures can be found) as that class, but later
possibly with different data (=variables) inside it, and this variables
are also rather independent of each other (as each variable has its own
separate location in memory).

---

To access the procedures, functions and variables, after
initializing, you can use the classname followed by a dot
followed by the name of that variable, function or procedure.

---
---

Steps: Overview:

 1. -Create a new application in C++

 2. -Add the following code about
     your class:

      class addressC {
       //
       private:
        //
        string nameS;
        string streetS;
        string cityS;
        string countryS;
        string telephoneS;
        string emailS;
        //
       public:
        addressC( string nameS, string streetS, string cityS, string 
countryS, string telephoneS, string emailS ); // constructor
        ~addressC(); // destructor
        void PROCView();
      };

 3. -Then give the code for the procedures or functions used in that 
class:

http://www.faqts.com/knowledge_base/view.phtml/aid/16106

      // constructor for this class
      addressC::addressC( string nameS, string streetS, string cityS, 
string countryS, string telephoneS, string emailS ) {
       this->nameS = nameS;
       this->streetS = streetS;
       this->cityS = cityS;
       this->countryS = countryS;
       this->telephoneS = telephoneS;
       this->emailS = emailS;
       cout << "address object is created and then initialized\n";
      };

      // destructor for this class
      addressC::~addressC() {
       cout << "address object is now destroyed\n";
      };

      // view current values of variables in this class
      void addressC::PROCView() {
       cout << nameS + ' ' + '-' + ' ' + streetS + ' ' + '-' + ' ' + 
cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + telephoneS 
+ ' ' + '-' + ' ' + emailS + "\n";
      };

 4. -Declare the variables which you intend to use with your class:

      addressC knudO( "myname", "mystreet 
1", "mycity", "mycountry", "01-2345678", "myemail@address" );
      addressC vanessaO( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );

 5. -Supply some source code where you use your class:

      main() {
       // initialize a variable representing that class:
       addressC knudO( "myname", "mystreet 
1", "mycity", "mycountry", "01-2345678", "myemail@address" );
       addressC vanessaO( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );
       // do something (e.g. show the current content stored in that 
variable:
       knudO.PROCView();
       // finally release the memory used by that variable, after use, 
when you do not need it anymore
      }

 6. -That will give alltogether the following source code in your C++ 
program:

     #include <iostream.h>
     #include <conio.h>
     //
           class addressC {
            //
            private:
             //
             string nameS;
             string streetS;
             string cityS;
             string countryS;
             string telephoneS;
             string emailS;
             //
            public:
             addressC( string nameS, string streetS, string cityS, 
string countryS, string telephoneS, string emailS ); // constructor
             ~addressC(); // destructor
             void PROCView();
           };

           // constructor for this class
           addressC::addressC( string nameS, string streetS, string 
cityS, string countryS, string telephoneS, string emailS ) {
            this->nameS = nameS;
            this->streetS = streetS;
            this->cityS = cityS;
            this->countryS = countryS;
            this->telephoneS = telephoneS;
            this->emailS = emailS;
            cout << "address object is created, and then 
initialized\n";
           };

           // destructor for this class
           addressC::~addressC() {
            cout << "address object is now destroyed\n";
           };

           // view current values of variables in this class
           void addressC::PROCView() {
            cout << nameS + ' ' + '-' + ' ' + streetS + ' ' + '-' 
+ ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + 
telephoneS + ' ' + '-' + ' ' + emailS + "\n";
           };

           main() {
            // initialize a variable representing that class:
            addressC knudO( "myname", "mystreet 
1", "mycity", "mycountry", "01-2345678", "myemail@address" );
            addressC vanessaO( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );
            // do something (e.g. show the current content stored in 
that variable:
            knudO.PROCView();
            // finally release the memory used by that variable, after 
use, when you do not need it anymore
           }

 7. -When you run this source code, you will see a message with that 
address information:

     c:\wordproc\tse32_v40025\knud>c:\borland\bcc55\bin\bcc32.exe -
Ic:\borland\bcc55\
     include -Lc:\borland\bcc55\lib c:\BBC\TAAL\DDDCPP.CPP
     Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

     c:\BBC\TAAL\DDDCPP.CPP:
     Turbo Incremental Link 5.60 Copyright (c) 1997-2002 Borland

     c:\wordproc\tse32_v40025\knud>DDDCPP.exe

     address object is created, and then initialized

     address object is created, and then initialized

     myname - mystreet 1 - mycity - mycountry - 01-2345678 - 
myemail@address

     address object is now destroyed

     address object is now destroyed

     Press any key to continue . . .

---
---

1. In general your class will look like this:

class <your classname> {
 //
 private:
  //
  int myprivatevariable1;
  bool myprivatevariable2;
  string myprivatevariable3;
  ...
  double myprivatevariablelast;
  //
  void myprivateprocedure1();
  void myprivateprocedure2();
  void myprivateprocedure3();
  ...
  void myprivateprocedurelast();
  //
  int myprivatefunction1();
  bool myprivatefunction2();
  string myprivatefunction3();
  ...
  double myprivatefunctionlast();
 //
 protected:
  //
  int myprotectedvariable1;
  bool myprotectedvariable2;
  string myprotectedvariable3;
  ...
  double myprotectedvariablelast;
  //
  void myprotectedprocedure1();
  void myprotectedprocedure2();
  void myprotectedprocedure3();
  ...
  void myprotectedprocedurelast();
  //
  int myprotectedfunction1();
  bool myprotectedfunction2();
  string myprotectedfunction3();
  ...
  double myprotectedfunctionlast();
 //
 public:
  //
  int mypublicvariable1;
  bool mypublicvariable2;
  string mypublicvariable3;
  ...
  double mypublicvariablelast;
  //
  void mypublicprocedure1();
  void mypublicprocedure2();
  void mypublicprocedure3();
  ...
  void mypublicprocedurelast();
  //
  int mypublicfunction1();
  bool mypublicfunction2();
  string mypublicfunction3();
  ...
  double mypublicfunctionlast();
 //
 myclassnameprocedure1(); // constructor
 myclassnameprocedure2( int i ); // constructor
 myclassnameprocedure3( int i, double d ); // constructor
 myclassnameprocedure4( int i, double d, string s ); // constructor
 ...
 myclassnameprocedurelast( int i, double d, string s, ... ); // 
constructor
 //
 ~myclassnameprocedure1(); // destructor
};

---

2. with below of this class definition, your work out of this function
   and procedure headers in that class, like this:

    // constructor for your class
    <myclassname>::<myclassname>() {
      // do something
    };

    // destructor for your class
    <myclassname>::~<myclassname>() {
      // do something
    };

    void <myclassname>::mypublicprocedure1() {
      // do something
    };

3. and further your using of this class, e.g. at main():

    main() {
     // do something
    }

   or similar by using e.g. a button event:

---
---

Internet: see also:

What is a Class ?
http://www.faqts.com/knowledge_base/view.phtml/aid/2025/fid/163

C#: Class: Create: Simple: How to create a class in C#?
http://www.faqts.com/knowledge_base/view.phtml/aid/24304

Delphi: Class: Create: Simple: How to create a class in Delphi?
http://www.faqts.com/knowledge_base/view.phtml/aid/24789/fid/175

Java: Class: Create: Simple: How to create a class in Java?
http://www.faqts.com/knowledge_base/view.phtml/aid/24842/fid/165

----------------------------------------------------------------------