Posts

Showing posts from 2016

For Loop

For Loop: A for loop is a iterative control structure that allows you to write a loop that needs to execute a specific number of times.(OR ’n’ number of times taken as a input). Syntax: for(initialisation ; condition; change in value of the variable) {      Body of the loop; } Example:  for(int i=1;i<=5;i++) //Starting from 1 to 5  {    cout<<“Hello\n”; //prints Hello  } output: Hello Hello Hello Hello Hello Explanation: In the above example the loop runs 5 times , ‘i’ value starting from 1 and end value 5 incrementing at the rate of 1. Example Designs: 1)   1   12   123   1234 2)            1          21        321      4321 3)     1    212 32123 4321234 543212345 4)     1    212  32123 4321234 543212345 4321234  32123    212     1 Practicing to do designs like this in for loops can help you i

Hybrid Inheritance

Image
There could be cases where we need to apply more than two types of inheritances to design a program. when we apply two or more types of i inheritances n a single program then it is called a hybrid inheritance. For Example : We wanted to write a simple program that could perform processing of students results. Program: #include<iostream> using namespace std; class student {  protected:    int rollno;    char name[30];  public:    void getdata()    {     cout<<"\nEnter student's Name:";     cin>>name;     cout<<"enter the rollno:";     cin>>rollno;    }   void display_details()   {    cout<<"\nName:"<<name;    cout<<“\nRoll no:"<<rollno;   } }; class test: public student {  protected:   float sub1,sub2;  public:  void get_marks()  {   cout<<endl<<"student su

Dynamic Allocation & Copy Constructor

Program in C++ to Create a Matrix using Dynamic allocation and copying it to another object using copy constructor and deallocate the memory using ‘delete’ key word. Program: #include<iostream> #include<new>                                    //Header file for new using namespace std; class Matrix { int m,n; int**p;                                   //Pointer to pointer(which stores the address of another pointers)  public:  Matrix()           //dynamic constructor {  cout<<"Enter no of rows and columns:";  cin>>m>>n;                                      // Dynamic Allocation p=new int*[m];                                   //rows for(int i=0;i<m;i++) p[i]=new int[n];                                 //in each row no of columns int i,j; cout<<"Enter values for the matrix"; for(i=0;i<m;i++)  {  for(j=0;j<n;j++) {  cin>>p[i][j]; } }   } Matrix(Matrix &