Advertisement



< Prev
Next >



C++ Class




To write your first C++ program with class, please open the Notepad application(comes preinstalled in Windows Operating System) and type in your first C++ class program, as explained in this article.

You may even use Integrated Development Environment(IDE) application like Eclipse and NetBeans to create your first C++ program with class but for a beginner, we would recommend using Notepad, because you learn so much when you work to find your own mistakes to fix them, as compared to an IDE finding them for you.




Important points about a class in C++







C++ naming conventions -







Note :







Creating our first C++ program


We are creating a C++ program which is saved in a file named A.cpp.




Why cpp extension for C++ program?


It is because every cpp extension is an acronym for c plus cplus.

A.cpp
#include<iostream>

using namespace std;

class A
{
public:
int a = 10;		//declaring a data member of class named 'a' of type int
void message(); 	//declaration of the function named 'message()'
};


//Definition of the function 'message' of class A, which returns void
void A :: message()
{
cout<< "Hello from the class A";
}



int main()
{
A ob;		//Creating an object of class A

//Accessing the data member 'a' of the object of class A, with the dot operator
cout<< "The value of a : " << ob.a << "\n";

//Calling the function 'message' of the object of class A, with the dot operator.
ob.message();
}





Understanding your first C++ program


Let's see what we actually did in our first C++ program by dividing it into a few segments -



  1. class A
    {
    public:
    int a = 10;		//declaring a data member of class named 'a' of type int
    void message(); 	//declaration of the function named 'message()'
    };
    

    • This program contains a class named, A.

    • According to the C++ convention, every class name in C++ should start with a capital alphabet, hence, we have named our class A.

    • In this class, we have declared two class members such as - a data member named a and a function named message()(with a return type void) under the visibility label public, this access modifier the class member accessible from the outside of this class(using the object of this class).





  2. //Definition of the function 'message' of class A, which returns void
    void A :: message()
    {
    cout<< "Hello from the class A";
    }

  3. Next, we have defined the function message() outside its class A. This function has a void return type.



  4. int main()
    {
    A ob;		//Creating an object of class A
    
    //Accessing the data member 'a' of the object of class A, with the dot operator
    cout<< "The value of a : " << ob.a << "\n";
    
    //Calling the function 'message' of the object of class A, with the dot operator.
    ob.message();
    }

    • A C++ program begins its execution with a call to the main() function of the class, because it is an entry point to our program and that's why the main() function contains the multiple lines of codes which calls the rest of the sections in the program.

    • This main() function first creates an object of class A and then use it with a dot operator to access the value of the data member named a of class A.

    • Using the same object of class A with a dot operator, this main() function also calls the function named message() of class A.



    Advertisement




    How to compile a C++ program?


    In order to compile our program, we need to call C++ compiler with a command g++ with the name of our file that contains the C++ class. We have saved our C++ program file A.C++ in the D: Drive location. Hence, in order to compile our A.C++ file from command prompt, first we need to reach the location where this C++ file is saved and type in the command -

    D:\>g++ A.cpp -o A.exe

    This has created a file, A.exe. This .exe file is an executable compiled form of our C++ program, which is going to be required and read in order to run our C++ program.




    Note :


    There is a space between the command g++ and name of the file.




    How to run a C++ program?



    D:\>A.exe
    The value of a : 10
    Hello from the class A

    • In order to run our program, we need to execute the .exe file, with its name, which contains the main() function,
    • And, at the end of this command, we press Enter to get the output of our program. This concludes a detailed description the concepts involved in a simple C++ program with a class.




    Please share this article -







    Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement