Posts

Showing posts from September, 2016

C++: Pointers

Image
A Pointer is a special type of variable which holds the address of a value.

C++: Inheritance

Image
div { width: 100%; } .success { background-color: #ccffcc; } Reusability is an  important feature of OOP. It saves our time and increase reliability. C++ strongly supports the concept of reusability. Types of Inheritance: 1) Single Inheritance
2) Multiple Inheritance
3) Hierarchical  Inheritance
4) Multilevel Inheritance
5) Hybrid Inheritance
__________________________________________ Defining Derived Class:   class derived-class-name: visibility-mode base-class-name
  {
       ..........//members of derived class
   }; The colon indicates that the derived-class-name is derived from the base-class-name. The default visibility mode is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived. When a base class is privately inherited by a derived class, 'public members' of the base class become 'private member' of the derived class and therefore the public members of the base class can only be accessed by the member…

C++:Program Swapping variables

Image
How to swap three nos. without another variable.
let input be a=2,b=3 and c=4
and output=a=4,b=2and c=3

#include<iostream>
using namespace std;

int main(){
int a,b,c;
cout<<"Enter three nos."<<endl;
cin>>a>>b>>c;
a=a+b+c; //now a=9
b=a-(b+c); //b=2
c=a-(b+c); //c=3
a=a-(b+c); //a=4
cout<<"a="<<a<<" b="<<b<<" c="<<c;
return 0;
} This method will not work for large value of integers.            < Happy Coding >

C++: Operator Overloading

Defining Operator Overloading:We can specify what operator means in relation to the class to which the operator is applied, with the help of operator function.     return type classname::operator(op-arglist)
     {
         function body //task assigned
     }op is the operator being overloaded.Operator functions must be either member function(non-static) or friend function.A friend function will have only one argument for unary operators and two for binary operators.A member function has no arguments for unary operator and only one for binary operators.   vector operator+(vector) //vector addition
   vector operator-();            //unary minus
   friend vector operator+(vector,vector); //vector addition
   friend vector opertaor-(vector);  //unary minusFor friend function overloaded operator function can be invoked by expression such as
   op x or x op    for unary operator
  x op y              for binary operatorin case of member function
   operator op (x,y)_____________________…

C++: Program to enter integers in 2D Array

Program to enter numbers in 2-D 5*5 array. #include<iostream>
usingnamespacestd; int main()
{
int a[5][5];
cout<<"Enter numbers in array"<<endl;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cin>>a[i][j];
  }
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cout<<a[i][j]<<" ";
  }
cout<<endl;
}
return0;
}

C++: Program to enter integers in 1D Arrray

Image
Program to enter numbers in an 1-D array of length 5. #include<iostream>
usingnamespacestd; int main()
{
int a[5];
cout<<"Enter numbers in array"<<endl;
for(int i=0;i<5;i++){
cin>>a[i];
}
for(int i=0;i<5;i++){
cout<<a[i]<<" ";
}
return0;
}

C++: Program to print pattern

Image
Q. Write a program to print the pattern

1
22
333
4444
55555 Ans. #include<iostream>
usingnamespacestd; int main()
{
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
cout<<i;
  }
cout<<endl;
}
return0;
} Any better solution or suggention is welcomed. < Happy Coding >

C++: Constructors and Destructors

Constructors:It enables an object to initialize itself when it is created and knows as automatic initialization.
Its name is same as the class and it is invoked whenever an object of its associated class is created.   class integer {
              int m,n;
      public:
              integer(void);  //constructor declared
             ...........
        };
    integer::integer(void){ //constructor defined
             m=0;
             n=0;
         }Now object is created
        integer int1;
Here data members m and n are initialized to zero.A constructor that accepts no parameters is called the default constructor.
eg
   for class A is A::A()If no constructor is defined, then the compiler supplies a default constructor.
Thus
         A a;
invokes default constructor.Special characteristics of constructor function:They should be declared in the public section.They do not have any return type, even void.They cannot be inherited, though a derived class can call the base class constru…

C++: Constructors and Destructors-1

Image
Constructors: It enables an object to initialize itself when it is created and knows as automatic initialization.
Its name is same as the class and it is invoked whenever an object of its associated class is created.    class integer {
              int m,n;
      public:
              integer(void);  //constructor declared
             ...........
        };
    integer::integer(void){ //constructor defined
             m=0;
             n=0;
         } Now object is created
        integer int1;
Here data members m and n are initialized to zero. A constructor that accepts no parameters is called the default constructor.
eg
   for class A is A::A() If no constructor is defined, then the compiler supplies a default constructor.
Thus
         A a;
invokes default constructor. Special characteristics of constructor function: They should be declared in the public section. They do not have any return type, even void. They cannot be inherited, though a derived class can call the base class…

C++: Classes & Objects-2

Image
Private Member Functions:A private member function can only be called by another function that is a member of its class.An object cannot invoke a private function using the dot operator.eg:
     class demo{
                   void read(); 
            public:
                   void update();
     };
if s1 is object
   s1.read(); //won't work   void sample::update(){
              read();
       }
__________________________________________Memory allocation for Objects:The member functions are created and placed in the memory space only once when they are defined as a part of a class specification.No seperate space is allocated for member function when objects are created.Only space for member variables is alocated seperately for each object, because the member variables will hold different data values for different objects.
__________________________________________Static Data Member:It is initialized to zero when the first object of its class is created. no other initialization …

C++: Classes & Objects- 1

Image
Structures in C++:it provides a facility to hide the data which is one of the main percepts of OOP.It also supports Inheritance, a mechanism  by which one type can inherit characteristics from other types.It can have both variables and functions as members.
__________________________________________
Specifying a Class:A class is a way to bind the data and its associated functions together.When defining a class, we are creating a new abstract data type that can be treated like any other built-in data type.The general form of class declaration is:class class_name{private:
variable declaration;
                    function declaration;
public:
variable declaration;
                    function declaration;
     };The data hiding(using private declaration) is the key feature of Object Oriented Programming.By default, the members of a class are private.The variables declared inside the class are known as data members and the functions are known as member functions.Only the member functions can…

C++: Program to find prime no.

Image
#include<iostream>
using namespace std;

int main(){ 
  int i,m=0,flag=0;   
  int n=17;//it is the number to be checked 
  m=n/2;   
  for(i=2;i<=m;i++){   
   if(n%i==0){   
   cout<<"Number is not prime"<<endl;   
   flag=1;   
   break;   
   }   
  }   
  if(flag==0)   
  cout<<"Number is prime"<<endl;   
  return0;
        < Happy Coding >

C++: Functions-2

Image
Call 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.  void swap(int a,int b)//a and b are reference variables
  {
        int t=a;
        a=b;
        b=t;
  }the function  will be

    swap(m,n);
__________________________________________Inline Function:An  inline function is a function that is expanded in line when it is invoked. That is, the compiler replaces the function call
with the corresponding functionn code.inline function-header
     {
           function body
      }
eg.
     inline double cube(double a){
      return(a*a*a);
       }All inline function be defined before they are called. The inline keyword merely sends a request, not a command, to the compiler. The compiler may ignore this request if the function definition is too long or too complicated and treat it as a normal function.Some of the situations where inline expansion may not wor…

C++: Functions -1

The main advantage of using function is that it is possible to reduce the size of a program by calling and using them at different places in the program.When the function is called,control is transferred to the first statement in the function body. The other statements in the function body are then executed and control returns to the main program when the closing brace is encountered.The Main Function:In C++,the main() returns a value of type int to the operating system.     int main();
     int main(int argc, char * argv[]);The function that have a return value should use the return statement for termination.The normal convention is that a return value of zero means the program ran successfully, while a nonzero value means there was a problem.The use of return(0) statement will indicate that the program was successfully executed.
__________________________________________Function Prototyping:The prototype describes the function interface to the compiler by diving details such as the …

C++: Small Topics

Image
Operators in C++

     <<      insertion operator
     >>      extraction operator
     ::         Scope resolution operator
     ::*      Pointer to member declarator
     ->*     Pointer to member operator
     .*       Pointer to member operator
  delete  Memory release operator
   endl     line feed operator
   new      Memory allocation operator
   setw     Field width operator
__________________________________________Memory Management Operatorspointer-variable = new data-type;The new operator allocates sufficient memory to hold a data object of type data-typeand returns the address of the object.pointer-variable = new data-type(value);pointer-variable = new data-type[size];delete pointer-variable;The pointer-variableis the pointer that points to a data object created with new.delete[size] pointer-variable;_______________________________________
Manipulators Manipulators are operators that are used to format the data display.endland setware manipulators.____________…