Wednesday, July 31, 2019

Constructor In C++

Constructor In C++ -: In the c++ programming language A constructors is a special member function whose task is to initialize the object of its class. It is a special because its name is the same as the class name. The constructors is involved whenever an object of its associated class is created. It is called constructors because it construct the values of data member of the class.

How the constructor declared and defined

//class with a constructor

class integer

{

   int a, b;

   public:

    integer (void);    // constructor declared
    ...........
    ...........
};

integer :: integer (void)  //constructors defined

{

   a=0;
   b=0;

} 


Characteristic for define the constructors -: There are some special character for defining the constructors function these are ..
  • Constructor should be declared in public section.
  • It can not inherited through a derived class can call the base class constructor.
  • These are invoked automatically when the object are created.
  • we can not refers to their address.
  • constructor do not have any return type not even void and therefore and they cannot return values.
  •  Like any other c++ function they can have default arguments.
  • Constructor can not be used as a member of a union.
  • constructor make 'implicit value' to the operator new and delete when memory allocation is require.
  • constructor can not be virtual.

Types of Constructors in c++ programming language-:  In the  C++ programming language constructor has many types these are
  1. Parameterized  Constructors
  2.  Multiple Constructor in class
  3. Default constructors
  4. Dynamic Initialization constructors
  5.  Copy Constructors
  6. Dynamic Construction
  7. Constructing Two dimension array.

Tuesday, July 30, 2019

classes in c++ programming language

Classes In C++ -:  A class is a way to bind the data and its associated function together. It allows the data to be hidden. If necessary from external use. When defining a class we are creating a new abstract data type that we can be treated like any other built in data type. Normally class has two types.
  1. Class Declaration -:The class declaration describe the type and the scope of its member
  2. Class Function Definition -:The class function definition describe how the class function are implemented.
Class Declaration -:The class declaration describe the type and the scope of its member The example of class declaration is here
Class classname 
{
   private:
          variable declaration ;
          function declaration ;
   Public:
          variable declaration ;
          function declaration ;
}


The class declaration is similar to a  declaration.The keyword class specifies that what follows is an abstract data of type class name.The body of a class is enclosed within braces and terminated by a semicolon. The class body contain the declaration of variable and function. These function and variable are collectively called class member.
They are usually grouped under two specify category these are the
  • Private class -This accessed within the class.
  • Public class-:It accessed outside the class.

The variable declared inside the class are known as data member and the function are known as member function. Only the member function can have access to the private class data member and private function
Example of simple class has been given here...
class item
{

          int number;          //variable declaration
          float cost;          //private by default

        Public:

          void getdata( int x, float y);        //function declaration 
           void putdata (void);                 // using prototyping
};

Creating Object -:  Once a class has been declared  we can create a variable of that type by using the name like any other built variable type as like
item x;   // memory of x is created

Accessing Class member- :  After defining the class the second steps comes how to access class member in c++  programming language  The private data of a class can be accessed only through the member function of that class. The Main() function contain statement that access number and cost directly. Syntax of accessing class member as like here
object name, function name(actual arguments);

 http://www.netnic.org/classes-in-cplusplus/

Defining Member Function -: Member function are defined in two types
  1.  Outside the class definition
  2. Inside the class definition
Both the function has major difference for processing the defining the header function .
Outside the class definition -: Member function that are declared inside a class have to be defined separately outside the class The definition are very much like the normal function.They should have a function header and a function body. Since c++ does not support the old version of function definition.
An important difference between a member function and a normal function is that a member function incorporates a membership identify label in the header. This label tells the compiler which class the function belong to for like this
return type class name:: function name (arguments declaration )
{
   function body
}

Sunday, July 28, 2019

data types in c++ programming language

Data Types in C++ -: Data types define the type of data a variable can store for an example an integer variable store integer data and  a character type variable store character data.
In other words we can say that the data type store the variable value. In  the C++ programming has 3 type of data types. These are
  1.  Basic data types or Built in data types
  2. User defined data types
  3. Derived data types



Basic Data Type in C++ programming language-:  Both the C languageand C++ compiler support all the built in data type. This is know the basic data type or fundamental data type. With the exception of void the basic data type may has several modifiers preceding them to serve the needs of various situation.
The Modifiers signed, Unsigned Long and short may be applied to character and integer basic data types. List of basic data type has been given here in this image
data type in c++




2. User Defined Data Type in C++ programming language- C++ also permit user define data types  these are the
  • Structure and classes :  We have used user define data such as struct and union in C  while these data types are Legal in C++ programming language.  C++ also permit user define data type this is known as the class which can be used just like any other basic data types to declare a variable. The class variable are known as object.
  • Enumerated Data Type -: An enumerated data type is another user defined data type which provide a way for attaching a name to number thereby increasing comprehensibility of the code  for example
  •  enum shape { circle,square,triangle };
     enum colour { red,yellow,green};
     enum position { off,on};

Derived Data Types In C++ programming Language -: In the Derived data type are in  C++ programming language has ..
  1. Array
  2. Function
  3. Pointer
Array-: This is the same in C language. Only exception is the way character array are initialized. When Initialing a character array in C, the compiler will allow us to declare the array as the exact length of the string constant. For example
char string [3]=" ABC"    // It is valid in c

While in C++ 

char string [4]="ABC"   // it is valid in c++

Function derived data type in c++-: Function have undergone major change in c++. While some of these changes are simple other require a new way of thinking when organizing our program.
Pointer derived data types -: Pointers are extensively used in C++ for memory management and achieving polymorphism. C++ adds the concept of constants pointer and pointer to constant.
char * constant ptrl ="good";


we can not modified the address the ptrl is initialized



tokens in c++ programming language


Tokens in C++-:  The token is the smallest individual units in a  program. In other words we can say that the smallest unit in the program called tokens. In the C++ programming language  token has the following types
  1. Keywords
  2. Identifiers
  3. Constant
  4. String 
  5. Operator 
Keywords in C++ -: The keywords implemented specific C++ language features. They are explicitly reserved identifiers and can not be used as names for the program variable or other user define program elements.
In other words we can say that the Keywords are pre-defined or reserved words in the programming language. Every  keyword has a  meant to perform a specific function in a program. Since keywords are referred names for a compiler, they can not  used as variable names. here are list has been given for ..


Identifiers  in c++- : It refer to the names of variable,function, array, classed etc. created by the programmer. They are fundamental requirement of any language.Each language has its own rules for naming these identifiers. These are the following rules in c language and c++ language..
  • Only alphabetic characters, digits and underscores are permitted.
  • The name can not started with a digit.
  • First letter must be an alphabet or underscore (_).
  • Uppercase and lowercase letters are  distinct.
  • Reserved keywords can not be used as an identifier’s name.
Major difference between c and c++ language is the limit on the length of a name. While ANSI C recognize only the first 32 character in a name And ANSI C++ place no limit on its length and therefore all the character in a name are significant.
Constant in c++-:  Constant refer the fix values that do not change during the execution of a program. Like c language C++ support several kinds of literal constant . C++ include integer, character, floating point number and string.  literal constant do not have memory allocation as like
1  2 3                         // decimal intger
1 2 .6 5                     // floating point decimal

Operator in C++ -:  C++ support a rich set of built in operator we have already used several of them such as equal to add(+)Minus(-)  multiplication(*) and division(/) operator is a symbol that tells the computer to perform certain mathematics or logical manipulation. operators are used in program to manipulate data and variables. they usually form a part of mathematical or logical expression.
All the c programming language operator are valid in c++ language. C ++ support some extra these are the
: :             Scope Resolution Operator
:  : *          pointer to member declarator
->*            Point to member operator
.*               Point to member operator
Delete     memory release operator
End1       line feed operator
New         Memory allocation operator
Setw         Field with operator

Structure in c++ programming language


Structure in C++ -:  In the c++ programming language structure has four section . These section may be placed in separate code file and then complied independently or jointly. As like..
c++ structure


It is a common practice to organize a program into three separate files. The class declaration are placed in a header file and the definition of member function go into another file. This approach enable the programmer to separate the abstract specification of the interface from the implementation details.
Finally the main program that uses the class is placed in a third file which ” Includes” the previous two files as well as any other files required.
This approach is based on the concept of client server model. The class definition including the member function constitute the server that provides services to the main program known as client. the client use the server through the public interface of the class

introduction of c++ programming language


Introduction of C ++-: C + + is an object oriented programming language it was developed by Bjarne Stroustrup at AT & T Bell Laboratories in  Murray Hill ,New Jersey ,USA ,in the early 1980s . Stroustrup,an admiral of simula67 and strong supporter of C,  Wanted to combine the best of both the language and create a more powerful language that could support object oriented programming features and still retain the power and Elegance of C.  The result was C + +.  Therefore C + + is an extension of C with a major edition of the class construct  feature of Simula 67 since the class was a major addition to original C language. Stroustrup initially called the new new language C with classes . However, letter in 1983, the name was changed to C + +. The idea of C++ comes from the C increment operator + +, thereby suggesting that C + + is an augmented (incremented) version of c.
        During the early 1990’s the language underwent a number of improvements and changes . In November 1997, the ANSI/ISO standards committee standardised these changes and added several new features to the language specifications.
.           C++ is a superset C programming. Most of what we already know about C applies to C++  also. Therefor, almost all C programs are also C ++ programs. However there are a few minor differences that will prevent a C program to run under C++ compiler. We shell see these defenses later as and when they are encountered.
     The most important facilities C++ adds on the C are classes, inheritance, functions overloading, and operator overloading. These features enable creating of abstract data type, inherit properties from existing data type and support polymorphism, thereby making C++ a truly object oriented language.
The object oriented features in C + + allow programmers to build large programs with clarity, extensibility and ease of maintenance, incorporating the spirit and efficiency of C. The addition of new features has transformed C from a language that currently facilitates top-down, structured design, to one that provides bottom-up, object oriented design.

APPLICATIONS OF C + +

C + + is a versatile  language for handling very large programs. It is suitable for Virtually any programming task including development of editors, compilers, databases, communication systems and any Complex real life application systems.
  •  Since C + + allows us to create hierarchy-related objects, we can build special object oriented libraries which can be used later by many programmers.
  •  While C++ is able to map the real world problem properly, the C  part of C++  gives the language the ability to get close to the machine level details.
  •  C++ programs are easily mentionable and Expendables. When  a new feature needs to be implemented, it is very easy to add to the existing structure of an object.
  •  it is expected that C++ will replace C as a general purpose language in the near future.

A Simple C ++ program example 
Let us begin with a simple example of a c ++ program that prints a string on the screen .
PRINTING A STRING
#include <iostream> // include header file
Using namespace std;
Int main()
{
Cout << “C++ is better than C.\n”; // C++ statement
Return 0;
} // End of example
This simple program demonstrates several C++ features.
Program features of c++
Like C , the C++ program is a collection of functions. The above example contains only one function, main(). As usual, execution begins at main(). Every C++ program must have a main().
C++ is a free form language. With a few exceptions, the compiler ignores carriage returns and white spaces. Like C the C++ statements terminate with semicolons.
Comments
C++ introduces a new comment symbol // (double slash). Comments start with a double slash symbol and terminate at the end of the line. A comment may start anywhere in the line and whatever follows till the end of the line is ignored. Note that there is no closing symbol.
The double slash comment is basically a single line comment. Multiline comments can be written as follows:
// This is an example of
// C++ program to illustrate
// Some of its features
The C comment symbols/*, */ are still valid and are more suitable for multiline comments. The
following comment is allowed:
/* This is an example of
C++ program to illustrate
Some of its features
*/
We can use either or both styles in our programs. However, remember that we can not insert a // style comment within the text of a program line. For example, the double slash comment cannot be used in the manner as shown
below:
for(j=0; j<n; /* loops n times */ j++
Output Operator
Only statement in program is an output statement the statement
cout << “C++ is better than C.”;
causes the string in quotation marks to be displayed on the screen . This statement introduces to new features, cout and << . The identifier cout (, pronounced as ‘C out’) is a predefined  object that represents the standard output stream in C + +. Hear ,the standard output stream represents the screen. It is also possible to redirect the output to other output devices. We shall later discuss streams in detail.
The object cout has a simple interface. In string represents a  string variable, then the following statement will display its contents.
cout  <<  string;
We may recall that the operator << is the bitwise left shift operator and it can still be used for this purpose. This is an example of how one operator can be used for different purpose, depending on the context. This concept is known as operator overloading, an important aspect of polymorphism. It is important to note that we can still use printf() for displaying an output. C++ accepts this notation. However, we will use cout << to maintain the spirit of C + +.
The iostream File
We have used the following #include directive in the program:
#include <iostream>
This directive causes the preprocessor to add the contents of the iostream file to the program. It contains declaration for the identifier cout and the operator << . Some old versions of C + + use a header file called iostream.h. This is one of the changes introduced by ANSI C++. (We should use iostream.h if the compiler does not support ANSI C++ features.
The header file iostream should be included at the beginning of all programs that use input/output statements. Note that the naming conventions for header file may vary. Some implementations use iostream.h pp;  yet others iostream.hxx. We must include appropriate header files depending on the contents of the program and implementation.