Posts

Showing posts with the label Cpp Programming

Namespace in C++

Namespace A program includes many identifiers defined in different scopes. Sometimes an identifier of one scope will overlap (i.e. collide) with an identifier of the same name in a different scope, potentially creating a problem. Identifier overlapping also occurs frequently in third-party libraries that happen to use the same names for global identifiers (such as functions).

Objects as Function Arguments in C++ Programming | C++ Programming

Objects as Function Arguments Like any other data type, an object may be used as a function argument in three ways: pass-by-value, pass-by-reference, and pass-by-pointer.

Class and Object and Memory in C++ Programming | C++ Programming

Image
Class and Object and Memory in C++ Programming   When a class is specified, memory space of all the member function is allocated but memory allocation is not done for the data member.   When an object is created, memory allocation for its data members is done. The logic behind separate memory allocation for member functions is quite obvious. All instances of a particular class would be using the same member functions but they may be storing different data in their data members.   Memory allocation for objects is illustrated in fig below Class and Object and Memory in C++ Programming It can be observed that “n” objects of the same class are created and data members of those objects are stored in distinct memory location, whereas the member functions of object 1 to object n are stored in the same memory area. Therefore, each object has a separate copy of data members and the different objects share the member functions among them.

C++ Program To Find Area Rectangle Using Class Function | C++ Programming

Source Code #include<iostream> using namespace std; #include<iomanip> class rectangle { float  length; float  breadth; public: void setdata (int l, int b) { length=l; breadth=b; } void showdata() { cout<<"length="<<length<<endl; cout<<"Breadth="<<breadth<<endl; } float  area() { return length*breadth; } void getdata() { cout<<"Enter length:\n"; cin>>length; cout<<"Enter breadth:\n"; cin>>breadth; setdata(length,breadth); } }; int main() { rectangle r; r.getdata(); r.showdata(); cout<<"Area="<<r.area(); } OUTPUT Enter length: 25 Enter breadth: 5 length=25 Breadth=5 Area=125

C++ Program using Manipulators endl and setw | C++ Programming

C++ Program using Manipulators endl and setw Before starting the program let us know about the use of endl and setw which are called manipulators. Manipulators: It is one of the code used in C++ Programming Language to give nice appearance to the output of the code. The effect of these manipulators are only known when we execute the program and look for output. setw : It is used to give white space in the C++ program like setw(15): which means 15 white spaces. Syntax for setw: setw ( x ) endl: It is used for terminating the line. For Ex, we use "\n" in C program for new line like wise we use endl for new line in C++ Programming Language. //Create a model of mark sheet with equal gaps(using setw() function) and line change (using endl). Source Code #include<iostream> using namespace std; #include<iomanip> int main() { int a,b,c,d,e,total; float percent; cout<<"Enter marks of Maths:\n"; cin>>a; cout<<"E

Function overloading in C++ | C++ Programming

Function overloading in C++ Programming Overloading refers to the use of the same thing for a different purpose. When the same  function name is used for different tasks this is known as function overloading. Function overloading is one of the important features of C++ and any other OO languages. When an overloaded function is called the function with matching arguments and return type is invoked. e.g. void border(); //function with no arguments void border(int ); // function with one int argument void border(float); //function with one float argument void border(int, float);// function with one int and one float arguments  For overloading a function prototype for each and the definition for each function that share same name is compulsory. //function overloading //multiple function with same name #include&lt;iostream.h&gt; #include&lt;conio.h&gt; int max(int ,int); long max(long, long); float max(float,float); char max(char,char); void main() {   re

Passing and Returning by reference | C++ Programming

Passing by references in C++ Programming          We can pass parameters in function in C++ by reference. When we pass arguments by reference, the   formal arguments in the called function become aliases to the   actual arguments in the calling   function i.e. when the function is working with its own arguments, it is actually working on the original data.

Default Arguments and Constant Arguments

Default Arguments In C++ a function can be called without specifying all its arguments. In such cases, the function assigns a default value to the parameter which does not have a matching argument in the function call. The default value is specified when function is declared.  

Reference Variables | C++ Programming

Reference Variables:               C++ Programming introduces a new kind of variable known as reference variable. A reference variable provides an alias (Alternative name) of the variable that is previously defined. For example, if we make the variable sum a reference to the variable total, the sum and total can be used interchangeably to represent that variable.   Syntax for defining reference variable Data_type & reference_name = variable_nane   Example: int total=100 ; int &sum=total;                Here total is int variable already declared. Sum is the alias for variable total. Both the variable refer to the same data 100 in the memory.   cout< <total;    and  cout<<sum; gives the same output 100.   And total= total+100;   Cout< <sum;     //gives output 200                   A reference variable must be initialized at the time of declaration. This establishes the correspondence between the reference and the data object which it means. The initi

Scope Resolution Operator( :: ) | C++ Programming

Scope Resolution Operator( :: ) | C++ Programming C++ programming supports a mechanism to access a global variable from a function in which a local variable is defined with the same name as a global variable. It is achieved using the scope resolution operator ::  

Advantages and Disadvantages of OOP | C++ Programming

Advantages and Disadvantages of OOP Advantages of OOPs Object oriented programming contributes greater programmer productivity, better quality of software and lesser maintenance cost.  The main advantages are: Making the use of inheritance, redundant code is eliminated, and the existing class is extended. Through data hiding, programmer can build secure programs that cannot be invaded by code in other parts of the program. It is possible to have multiple instances of an object to co-exist without any interference. System can be easily upgraded from small to large systems. Software complexity can be easily managed. Message passing technique for communication between objects makes the interface description with external system much simpler. Aids trapping in an existing pattern of human thought into programming. Code reusability is much easier than conventional programming languages. Disadvantages of OOPs Compiler and runtime overhead. Object oriented progr

Object Oriented Approach | C++ Programming

Image
Object Oriented Approach | C++ Programming                 The fundamental idea behind object-oriented language is to combine both data & function that separate data into a single unit such a unit is called object. A objects function called member function provides the way to access objects data.

Procedural Oriented Approach | C++ Programming

Image
Procedural Oriented Approach | C++ Programming In procedural oriented approach programs are organized in form of subroutines. The subroutines do not let the code duplication. P.O. is suitable for medium sized software C, Pascal, FORTRAN etc. Are procedural oriented language.                

Function Templates and Class Templates in C++ Programming | C++ Programming

Templates in C++ Programming  Function templates in C++ Programming  Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type. The format for declaring function templates with type parameters is: template  function_declaration; template  function_declaration; The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its use is indistinct, since both expressions have

Inheritance in C++ Programming | C++ Programming

Inheritance in C++ Programming  Introduction Inheritance (or derivation) is the process of creating new classes, called derived classes, from existing classes, called base classes. The derived class inherits all the properties from the base class and can add its own properties as well. The inherited properties may be hidden (if private in the base class) or visible (if public or protected in the base class) in the derived class.

Inline function in C++ Programming | C++ Programming

Inline function in C++ Programming A inline function is a short-code function   written and placed before main function   and compiled as inline code. The prototyping is not required for inline function. It starts with keyword inline . In ordinary functions, when function is invoked the control is passed to the calling function and after executing the function the control is passed back to the calling program.           But , when inline function is called, the inline code of the function is inserted at the place of call and compiled with the other source code together. That is the main   feature of inline function and different from the ordinary function.   So using inline function executing time is reduced because there is no transfer and return back to control. But if function has long code inline function is not suitable because it increases the length of source code due to inline compilation. // Inline Function //saves memory, the call to function cause the same co

Function in C++ Programming | C++ Programming

Default Arguments When declaring a function, we can specify a default value for each of the last parameters which are called default arguments. This value will be used if the corresponding argument is left blank when calling the function. To do that, we simply have to use the assignment operator and a value for the arguments in the function declaration. If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is ignored and the passed value is used instead.  For example: // default values in functions using namespace std ; int divide ( int a , int b = 2 ) {     int r ;     r = a / b ;     return ( r ); } int main () {     cout << divide ( 12 );     cout << endl;     cout << divide ( 20 , 4 );     return 0 ; } As we can see in the body of the program there are two calls to function divide. In the first one: divide (12) We have only specified one argument, but the

Structured Programming in C++ Programming | C++ Programming

Image
Structured Programming in C++ Programming Structured programming (sometimes known as modular programming) is a subset of procedural programming that enforces a top-down design model, in which developers map out the overall program structure into separate subsections to make programs more efficient and easier to understand and modify similar functions is coded in a separate module or sub-module, which means that code can be loaded into memory more efficiently and that modules can be reused in other programs. In this technique, program flow follows a simple hierarchical model that employs three types of control flows: sequential, selection, and iteration.