In this, I will explain basic syntax. we going to take input from the user and display it. C++ is object-oriented so we have to class and object. In this, I am going to explain everything in details. lets started
Structure of C++ program is as follow.
Include Files
Class Declaration or Definition
Member functions definitions
Main function
In any program first write header files like as iostream.h, conio.h, etc..as per requirement Of program.
After header file writes class declaration or definition as per your planning.
After class, define all member functions which are not define but declare the class.
In last write the main function without main function program execution is not possible.
int main() { }
It defines a function called main, which takes no arguments and does nothing.
int return type of the main function it means the min function is returning some data.
Every C++ program must have a function named main(). The program starts by executing that function. The int value returned by main(), if any, is the program’s return value to ‘‘the system.’’ If no value is returned, the system will receive a value indicating successful completion. A nonzero value from main() indicates failure.
main() function is the main part of c and c++ program because compiler starts reading from main() that's why main() is compulsory for every program. through this, we can say main is the body of the program.
Program : #include <iostream.h> // this is predefine library like stdio.h And it contains input-output function(cin and cout)And it #include<conio.h>// this is also predefine library use for console input output class program// class is the keyword for class declaration and program is a name of the class { // class starting block public: // public is access modifier . its means everyone can access char c[10]; // char is data type ,c is variable and [10] is length of string }; // class closeing block make sure terminating symbol( ; ) should be there void main() //this is main part of program which gonna execute { clrscr(); // clear screen function program p; // creating class object . program is class and p is its object cout<<"enter the name"<<"\n"; // cout use display message . remember this like cout is less then(<<) cin>>p.c; //cin is use to take input frome user . remember this like cin is greater then(>>) . here we are accessing c variable by p object cout<<"name"<<p.c; // here again we are accessing c variable by p object and displaying it but in c++ you no need ( %c,%d,%f etc) getch(); // its is same in both c & c++ . it is use to hold display }
C++ is object-oriented programming language but we have not used in this program.
C program extension is .c
And C++ program extension is .cpp . If you save in .c extention program will not run and it will show error.
The line #include instructs the compiler to include the declarations of the standard stream I/O facilities as found in< iostream.h> Without these declarations, the expression
would make no sense. The operator << (‘‘put to’’) writes its second argument onto its first. In this case, the string literal "Hello, world!\n" is written onto the standard output stream cout. A string literal is a sequence of characters surrounded by double quotes. In a string literal, the backslash character \followed by another character denotes a single special character. In this case, \nis the newline character, so that the characters are written are Hello, world! followed by a newline.
The iostream library defines output for every built-in type. Further, it is easy to define the output of a user-defined type. By default, values output to couture converted to a sequence of characters.
Cout and Cin function:
Cout is a predefined function. It is used to display output on the screen.
<iostream.h> predefine library contain cout function. If we want use cout function then we have to include that library other wish we cant use that function
The << operator (‘‘put to’’) is used as an output operator; cout is the standard output stream.
Cin is also predefined function. It is input function used to take input from the function.
Cin is also in <iostream.h> library
>> operator (‘‘get from’’) is used as an input operator; cinis the standard input stream. The type of the right-hand operand of >> determines what input is accepted and is the target of the input operation. The \ncharacter at the end of the output string represents a newline.