Saturday, 1 November 2014

Introduction to OOP

19:07

LAB REPORT #01
Object Oriented Programming
Introduction to C++:
A C++ program is a collection of commands, which tell the computer to do "something". This collection of commands is usually called C++ source codesource code or just code. Commands are either "functions" or "keywords". Keywords are a basic building block of the language, while functions are, in fact, usually written in terms of simpler functions--you'll see this in our very first program, below. (Confused? Think of it a bit like an outline for a book; the outline might show every chapter in the book; each chapter might have its own outline, composed of sections. Each section might have its own outline, or it might have all of the details written up.) Thankfully, C++ provides a great many common functions and keywords that you can use. 

But how does a program actually start? Every program in C++ has one function, always named main, that is always called when your program first executes. From main, you can also call other functions whether they are written by us or, as mentioned earlier, provided by the compiler. 

So how do you get access to those prewritten functions? To access those standard functions that comes with the compiler, you include a header with the #include directive. What this does is effectively take everything in the header and paste it into your program. Let's look at a working program:
1
2
3
4
5
6
7
8
#include <iostream>

using namespace std;

int main()
{
  cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
}
Let's look at the elements of the program. The #include is a "preprocessor" directive that tells the compiler to put code from the header called iostream into our program before actually creating the executable. By including header files, you gain access to many different functions. For example, the cout function requires iostream. Following the include is the statement, "using namespace std;". This line tells the compiler to use a group of functions that are part of the standard library (std). By including this line at the top of a file, you allow the program to use functions such as cout. The semicolon is part of the syntax of C++. It tells the compiler that you're at the end of a command. You will see later that the semicolon is used to end most commands in C++. 

Variables and Constants:
Programs need a way to store the data they use. Variables and constants offer various ways to represent and manipulate data. Constants, as the name suggests, have fixed values. Variables, on the other hand, hold values which can be assigned and changed as the program executes.

Variable types:

Every variable and constant has an associated type which defines the set of values that can be legally stored in it. Variables can be conveniently divided into integer, floating point, character and boolean types for representing integer (whole) numbers, floating point numbers (real numbers with a decimal point), the ASCII character set (for example 'a', 'b', 'A') and the boolean set (true or false) respectively.
More complicated types of variable can be defined by a programmer, but for the moment, we will deal with just the simple C++ types. These are listed below:

int

to store a positive or negative integer (whole) number

float

to store a real (floating point) number

bool

to store the logical values true or false

char

to store one of 256 character (text) values

Declaration of a Variable:

A variable is introduced into a program by a declaration which states its type (i.e.int, float , bool or char) and its name, which you are free to choose. A declaration must take the form: 
   type          variable-name; 

   int           count; 
   float         length; 
   char          firstInitial; 
   bool          switched_on; 
or:
   type             variable1, variable2, ... variableN; 

    float           base, height, areaCircle; 
    int             myAge, number_throws; 

The variable name can be any sequence of characters consisting of letters, digits and underscores that do not begin with a digit. It must not be a special keyword of the C++ language and cannot contain spaces. C++ is case-sensitive: uppercase and lowercase letters are considered to be different. Good variable names tell you how the variable is used and help you understand the flow of the program. Some names require two words and this can be indicated by using the underscore symbol (_) or using an uppercase letter for the beginning of words.

Inputting in C++:
Object of class istream that represents the standard input stream oriented to narrow characters (of type char). It corresponds to the C stream stdin.

The standard input stream is a source of characters determined by the environment. It is generally assumed to be input from an external source, such as the keyboard or a file.

As an object of class 
istream, characters can be retrieved either as formatted data using the extraction operator (operator>>) or as unformatted data, using member functions such as read.

If- Else Statement:

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax:

The syntax of an if...else statement in C++ is:
if(boolean_expression)
{
   // statement(s) will execute if the boolean expression is true
}
else
 
  // statement(s) will execute if the boolean expression is false
}



If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 All in One. All rights resevered. Designed by Templateism

Back To Top