In a C++ program, objects communicate by invoking each other's methods, forming a cohesive system. Let's delve into the meanings of classes, objects, methods, and instance variables.
Object:
- Objects have
states and behaviours. For example, a dog can have states like colour,
name, and breed, and behaviours like wagging, barking, and eating.
Essentially, an object is an instance of a class.
Class:
- A class serves
as a blueprint or template that defines the states and behaviours that its
objects will have.
Methods:
- Methods define behaviours
in a class. A class can contain multiple methods, where the logic is
implemented, data is manipulated, and actions are carried out.
Instance Variables:
- Each object has
a unique set of instance variables. The state of an object is determined
by the values assigned to these variables.
Structure of a C++ Program
Let's look at a simple example that prints "Hello World":
#include <iostream>
using namespace std;
// Program execution begins here.
int main()
{
cout << "Hello World"; // Prints Hello World
return 0;
}
Components of the Program
- Header Files: C++ defines
several headers that provide essential functionalities. For this program,
we include the <iostream> header.
- Namespace: using namespace
std; tells the compiler to use the standard namespace, which helps avoid
naming conflicts.
- Comments: The line // main() is
where program execution begins. is a single-line comment.
Single-line comments in C++ start with //.
- Main Function: int main() defines the
main function where execution starts.
- Output
Statement: cout << "Hello World"; prints "Hello World"
to the screen.
- Return
Statement: return 0; ends the main function and returns 0, signaling successful
execution.
Compiling and Running a C++ Program in
Dev-C++
Here’s how to save, compile, and run the program using Dev-C++:
- Open Dev-C++
and create a new source file.
- Write the code
shown above.
- Save the file
as hello.cpp.
- Click on the
"Execute" menu and select "Compile & Run" (or
press F11).
You should see Hello World printed in the output window.
Semicolons and Blocks in C++
In C++, statements end with a semicolon (;), marking the conclusion of one
logical statement. For instance:
a = b;
b = b + 1;
sum(a, b);
A block is a set of statements enclosed in braces ({}). For example:
{
cout << "Hello World"; // Prints Hello World
return 0;
}
C++ does not consider the end of a line as a statement terminator, so
statements can be written on the same line:
a = b; b = b + 1; sum(a, b);
Identifiers in C++
Identifiers are names for variables, functions, classes, modules, and
other user-defined items. They must begin with a letter (A-Z or a-z) or an
underscore (_) and can be followed by letters, digits (0-9), or underscores.
Identifiers are case-sensitive.
Examples of valid identifiers:
ram |
mohan |
john |
variable_name |
x_123 |
Example15 |
_temp |
y |
z9value |
returnValue |
Keywords in C++
The following are reserved words in C++, meaning they cannot be used as
identifiers:
asm |
else |
new |
this |
auto |
enum |
operator |
throw |
bool |
explicit |
private |
TRUE |
break |
export |
protected |
try |
case |
extern |
public |
typedef |
catch |
FALSE |
register |
typeid |
char |
float |
reinterpret_cast |
typename |
class |
for |
return |
union |
const |
friend |
short |
unsigned |
const_cast |
goto |
signed |
using |
continue |
if |
sizeof |
virtual |
default |
inline |
static |
void |
delete |
int |
static_cast |
volatile |
do |
long |
struct |
wchar_t |
double |
mutable |
switch |
while |
dynamic_cast |
namespace |
template |
|
Whitespace in C++
Whitespace includes blanks, tabs, newline characters, and comments, and
it separates parts of a statement to help the compiler distinguish between
elements like int and value:
int value;
In the following example, whitespace is not necessary but can improve
readability:
total = apples + oranges; // Calculate the total fruit
This deeper understanding of C++ fundamentals will aid in writing,
compiling, and executing C++ programs effectively.
Comments
Post a Comment