Introduction to C++

Introduction to C++

☯ What is C++?

➤ C++ is a cross-platform language that can be used to create high-performance applications.

➤ C++ was developed by Bjarne Stroustrup, as an extension to the C language.

➤ C++ gives programmers a high level of control over system resources and memory**.**

Sample C++ Program:

Code:

#include<iostream>
using namespace std;
int main()
{
cout<< "hello world"<<endl;

}

Output:

hello world

➤ Explanation:

✚ #include<iostream>

The #include is a preprocessor directive used to include files in our program. The above code is including the contents of the iostream file. This allows us to use cout in our program to print output on the screen. For now, just remember that we need to use #include to use cout which allows us to print output on the screen.

✚ int main() {...}

A valid C++ program must have the main() function. The curly braces indicate the start and the end of the function. The execution of code beings from this function.

✚ std::cout << "Hello World!";

std::cout prints the content inside the quotation marks. It must be followed by << followed by the format string. In our example, "Hello World!" is the format string. Note: ; is used to indicate the end of a statement.

✚ return 0;

The return 0; statement is the "Exit status" of the program. In simple terms, the program ends with this statement.

▶ Applications of C++ :

☑Games.

☑ GUI-Based Applications. Adobe Systems. ...

☑ Database Software. MYSQL Server.

☑ Operating Systems. Apple OS. ...

☑ Browsers. Mozilla Firefox. ...

☑ Advanced Computation And Graphics. Alias System.

☑ Banking Applications. ...

☑ Cloud/Distributed System.

Introduction to class and object:

A class is a template or a blueprint that binds the properties and functions of an entity. You can put all the entities or objects having similar attributes under a single roof, known as a class.

Consider an example of a railway station having several trains. A train has some characteristics like train_no, destination, train_type, arrival_time, and departure_time. And its associated operations are arrival and departure. You can define a class can for a train as follows:

class train
{
 // characteristics
 int train_no;
 char destination;
 char train_type;
 int arrival_time;
 int departure_time; 
 // functions
 int arrival(delayed_time)
 class train
{
 // characteristics
 int train_no;
 char destination;
 char train_type;
 int arrival_time;
 int departure_time; 
 // functions
 int arrival(delayed_time)

The above class declaration contains properties of the class, train_no, destination, train_type, arrival_time, and departure_time as the data members.

• Description of the Syntax:

☉ Class: This is the keyword used to declare a class that is followed by the name of the class.

☉ Class_name: This is the name of the class which is specified along with the keyword class.

☉ access_specifier: It provides the access specifier before declaring the members of the class. These specifiers control the access of the class members within the class. The specifiers can be public, protected, and private.

☉ data_member: These are the variables of the class to store the data values.

☉ member_function: These are the functions declared inside the class.

☯Objects in C++

Objects in C++ are analogous to real-world entities. There are objects everywhere around you, like trees, birds, chairs, and tables.

Consider the example of a railway station discussed in the previous section. After defining the class train, you can create similar objects for this class. For example, train_A and train_B. You can create the objects for the class defined above in the following way:

train train_A, train_B;

The syntax to create objects in C++:

class_name object_name;

The object object_name once created, can be used to access the data members and member functions of the class class_name using the dot operator in the following way:

obj.data_member = 10; // accessing data member

obj.func(); // accessing member function

Member Functions in Classes

The member functions are like the conventional functions.

When you define a member function, it only creates and shares one instance of that function with all the instances of that class.

The following syntax can be used to declare a member function inside a class:

class class_name 

{ access_specifier: return_type member_function_name(data_type arg); 

};

It specifies the access specifier before declaring a member function. It also specifies the return type and data type in the same way in which it declares a usual function.

Method Definition Outside and Inside of a Class:

The following two ways can define a method or member functions of a class:

❁ Inside class definition

❁ Outside class definition

The function body remains the same in both approaches to define a member function. The difference lies only in the function's header. Now, have a deeper understanding of these approaches.

Inside Class Definition

It defines a member function inside a class in the same familiar way as it defines a conventional function. It specifies the return type of the function, followed by the function name, and it provides arguments in the function header. Then it provides the function body to define the complete function. The member functions that are defined inside a class are automatically inline. The following example illustrates defining a member function inside a class.

#include <iostream>
using namespace std;
// define a class
class my_class
{
public:
 // inside class definition of the function
 void sum(int num1, int num2) // function header
 { 
 cout << "The sum of the numbers is: "; // function body
 cout << (num1 + num2) << "\n\n"; 
 }
};
int main()
{
 // create an object of the class
 my_class obj;
 // call the member function
 obj.sum(5, 10);
 return 0;
}

Output:

Outside Class Definition

It can be done by scope resolution operator(::).

The following syntax is used to define a member function outside the class:

void class_name :: function_name(arguments) 

{ 

// function body 

}

The class_name is the name of the class in which the function has been declared. The function_name is the name of the member function. It uses the scope resolution operator here to restrict the scope of the function to its class. The following example illustrates how to define a member function outside a class.

#include <iostream>
using namespace std;
// define a class
class my_class
{
public:
 // declare the member function
 void sum(int num1, int num2);
};
// outside class definition of the function
void my_class::sum(int num1, int num2) // function header
{
 cout << "The sum of the numbers is: "; // function body
 cout << (num1 + num2) << "\n\n";
}
int main()
{
 // create an object of the class
 my_class obj;
 // call the member function
 obj.sum(5, 10);
 return 0;
}

Output:

Array of Objects

The definition of an array of objects is similar to the usual array definition with the data type replaced with the class name. The following syntax is used to define an array of objects

class_name obj[20];

The following example illustrates the use of an array of objects.

#include <iostream>
using namespace std;
// define a class
class my_class
{
public:
 // declare the member function
 void printValue(int value)
 {
 cout << "The value is: " << value << "\n";
 }
};
int main()
{
 // create an object of the class
 my_class obj[5]; 
 // call printValue() function for all objects
 for (int i = 0; i < 5; i++)
 {
 obj[i].printValue(2 * i); // printValue() is called
 // for particular object
 } 
 cout << "\n\n"; 
 return 0;
}

🔯🔯🔯HAPPY LEARNING 🔯🔯🔯