Showing posts with label C Plus. Show all posts
Showing posts with label C Plus. Show all posts

Tuesday, 12 February 2013


Symbolic constants and constant qualifiers in C++ programming

Symbolic constant is a quantity which does not change during the execution of the program. Constant qualifiers are keywords used to define a quantity as symbolic constant. The qualifiers are

1) const.
2) enum.

steps and rules:

1) Const must be initialized.
2) Const is local to the function where it is declared.
3) By the qualifier extern along with const, it can be made global.
4) If data type is not given it is treated as integer.

Posted by Unknown

What is Typedef in C++ programming ?

Typedef is a keyword. This is used to define a new name for an existing data type. The general form is

typedef datatype newname;

where,
typedef -keyword
datatype - valid datatypes such as int, float, char, etc.
new name - valid C++ name for the data type

In C++ there is no need to specify the keyword typedef for enum, structure and union data types.
Posted by Unknown

Difference between unions and structures

1) A union can provide storage for one member at a given time. But a structure can provide storage for all members.

2) The amount of space allocated for union is based upon the member which requires the largest memory space. But in structure each member is given memory space.

3) In Union at one time we can refer any one variable. But in structure we can refer all the variables.

Posted by Unknown


Union is like a structure data type in which all the members share the same memory area. The general form is,

union tag-field
{
datatype member1;
datatype member2;
------
------
datatype membern;
}variable1, variable2 ...variablen;

Example:
Consider the following union declaration
union example
{
char name[15];
int number;
}student;

This declares student as a variable to the union named example having two members. The members are accessed as
student.name
student.number
Because of the property that all members share the same memory space, the compiler allocates a memory space that is large enough to store the largest variable type in the union. In our declaration the 15 character string requires more memory than the integet quantity. so the compiler allocates 15 bytes to the union. This area will be shared by the two variables student.name and student.number.

Users of Union:

1) Unions needs less memory space
2) They are useful for applications involving number of variables, where values need not be assigned to all elements at one time

Posted by Unknown


A structure is defined as a data type to represent different types of data with a single name. The data items in a structure are called members of the structure.

Defining a structure:

A structure definition contains a keyword struct and a user defined tag-field followed by the members of the structure within braces. The general form is

struct tag-field
{
datatype member1;
datatype member2;
-------
-------
datatype membern;
}

where,
struct  - keyword to define structure
tag-field - name of the structure - valid C++ name
datatype - valid datatypes such as int, float etc

Steps and Rules:

1. Tag-field is the name given to the structure
2. Each member definition should be terminated with semicolon
3. Since structure definition has compound statements, it should have its own opening and closing braces.
4. The semicolon after the closing brace is a must.

Posted by Unknown


Advantages of Enumeration in c++ Programming

a. With this we can assign more than one constant value to a single user defined name.
b. Internally the values of constant are 0, 1, 2, ... n-1.
c. We can explicitly change the value of constants.
d. Arithmetic operations are possible on enumerated type variables.

Posted by Unknown


User defined data types are data types defined by the user. They are

1) Enumeration
2) Structure
3) Union
4) Class

1) Enumeration

This allows us to define our own data type with predefined values. The general form is,

enum userdefined_name
{
value1,
value2,
---
---
valuen
};

The default values 0,1,2,.... n-1 can be changed by giving value in the declaration as

enum userdefined_name { value1=20, vlau2=3 .... valuen=50 };

Once the enumeration data type is defined , we can declare variables of this data types. The general form is

userdefined_name variable1, variable2, ... variablen;

The variables variable1, variable2, ... variablen cannot take values other than value1=20, vlau2=3 .... valuen=50. The userdefined-name should be the same name as used in the definition.


Posted by Unknown


It is a process of converting one data type into another. There are two types of data type conversion. They are,

1) Explicit conversion
2) Implicit conversion

1) Explicit conversion

It is a process of converting one data type into another explicitly using cast operator. The general form is

newtype (variable or expression);

(or)

(new type) variable or expression;

2) Implicit conversion or automatic conversion

If an expression contains different type of operands, the lower data type operands are automatically converted to higher data types. This conversion is called implicit conversion. The lower data types are data types which occupies less memory space and higher data types are data types which occupies large memory space.

float x;
x=6 + 8.3;

The operands 6 and 8.5 in the expression are of different data types. But during execution the integer data 5 is automatically converted into float type and the operation is performed. So the result is 14.5.

Posted by Unknown


C++ data types can be classified as

1) Basic data types
2) User defined data types
3) Derived data types

Basic data types in C++ Programming Language

These data types are already defined in the language. They are,

1) char
2) int
3) float
4) double

The following keywords are prefixed with the basic data types to produce new data types. These keywords are otherwise called as modifiers.

1) signed
2) unsigned
3) long
4) short

Special basic data type:
Void is a special basic data type. The compile will not allocate any memory space for this data type. This data type is used to

1) define functions.
2) define parameter passing

Example:

1. void fun1(); - This means the function fun1 will not return any value.

2. int fun2(); - This means the function fun2 will not receive any parameters. But returns an integer type value.

Posted by Unknown


Constant is a quantity which does not change during the execution of the program. The different type of constants are,

1) Integer constant
2) Floating point constant
3) Character constant
4) String constant
5) Wide - character constant

Example:

345 - Integer
110.21 - Floating point
'B' - Character constant
"Name" - String constant
A 'CD' - Wide character constant
Posted by Unknown


Initialization of variables during run time is called dynamic initialization of variables.

Example:

int l;
cin>>l;
float avg=l/10;

The variable avg is initialized only during run time, because the value of l is available during run time only.

Posted by Unknown


Variable is a quantity which changes during the execution of the program.

All variables present in the program must be declared before it is used. The declaration can also be done before the first use of the variable. The general for is

datatype variablelist;

Example:

1) int a, b, c;
This declares a, b, c as integer variables and allocate memory space.

2) int n;
for(int j=0; j<=n;j++)

This is valid because j is declared as int j before it is used.

Posted by Unknown

Identifiers are names given to variables, functions, arrays and other user defined structure. These are user defined names.

Steps (how to do):

1) Identifiers are formed with alphabets, digits and a special character underscore (-).
2) The first character must be an alphabet.
3) No special characters are allowed other than underscore.
4) They are case sensitive. That is AA is different from aa.
5) There is no limit for the number of characters.

Example:

1) The following are some valid identifiers
   A0, BASICPAY, basicpay, TOTAL_PAY, B12.
 
2) The following are invalid identifiers


Posted by Unknown


Comment statements are non-executable statements. These statements are used to increase the readability and understanding of the program. A comment statement can be included anywhere in the program. There are two types of comments. they are,

1) Multiline comment
2) Singleline comment

All multiline comments should start with /* and end with */. No  space is allowed between * and /.
All singleline comments should start with //. No space allowed between /and /.

Example:

1) /* program to find the sum of three
numbers */

2) // program by Jhon, Ramu and Rahmon.

Posted by Unknown


Namespace is a keyword used to create a memory area. In this area the programmer can define various program elements such as variables, functions, classes etc. These program elements can be used any where in the programs like global variables.

Defining a namespane
The general form is,

namespace name
{
statement-1;
statement-2;
---------
statement-n;
}

Posted by Unknown


Keywords are words which belongs to C++ language. They have standard predefined meaning. These words should be used only for their intended purpose. The users have no right to change its meaning. Keywords should be written in lowercase.

New keywords in C++

C++ language contains all the keywords in C language. In addition to this some more keywords are added to make C++ language as an object oriented language. These keywords are called new keywords. In other words new keywords are words which belongs to C++ language only.

Examples for Keywords:

1) New keywords

asm   catch   friend    private   public
this    try   new       new       class
inline  protected       throw     virtual

2) Keywords common to C and C++

float   signed    auto    for   sizeof
break   static    case    goto  struct
if      switch    char    int   const
long    continue  default typedef union
do      unsigned  double  else  register
void    enum      return  volatile extern

Posted by Unknown


A token is an individual element in a program. More than one taken can appear in a single line separated by white spaces. White space may be blank, carriage return or tab. The possible C++ tokens are

1) keywords
2) identifiers
3) constants
4) operators
5) strings
Posted by Unknown


Manipulators are functions used with the insertion operator (<<). These are used to format the data to be displayed. They are,

1) endl
2) setw

1) endl - end line

This function is used to transfer the control to a new line while printing.

Example:
cout<<"Name :"<<name<<endl<<"Age"<<age;

2) setw - set width

This function is used to set the width of the output list. The list may be variables or constants. The output is right justified.

The general form is

setw(size).

where
size - number of characters to be displayed. This should be an integer.


Posted by Unknown

cout object

cout is an object. It is predefined in the headerfile iostream.h. It is used to display the information on the VDU. The general form is,

cout << list to be printed;

When this statement is executed, the list on the right hand side of the operator << is directed to the object cout and displayed on the VDU.

Rules for cout objects:
1) This should terminate with semicolon (;)
2) If the list to be printed contains more than one, each should be separated by <<.
3) The list to be printed may be variables or constants.

cin object

cin is an object. It is predefined in the header file iostream.h. It is used to give values to the variables through keyboard. The general form is,

cin >> list of variables;

When this statement is executed, the data entered through the keyboard are assigned to the list of variables on the righthand side of the operator >>.

Rules for cin objects:
1) This should terminate with semicolon.
2) The list of variables should be separated by >> operated.

Posted by Unknown

Include file

In C++ there are a number of files called header files. These files contain number of predefined functions, classes, variables and data. If we want to use the predefined functions, classes, variables and data in our program, the appropriate header file should be included at the beginning of the program. The general form of include file is

#include<headerfilename.h>

Examples : iostream.h, fstream.h, graphic.h, math.h, process.h, iomanip.h etc.

Class definition

It is an user defined data type having defined member functions and member variables. We can have more than one class in a program.

Main function

In all C++ programs, there is a function named main. Through this function only we can access other parts of the program. This function should return a value.

Variable, object declaration

In this section the required variables and objects are declared.

Body of main fuction

In this section valid C++ statements, message passing etc., are given.

Posted by Unknown
Powered by Blogger.