Friday, 27 January 2017

program to add two complex number using constructor overloading in C++

Tags

Q.6 Write a program to perform addition of two complex numbers using constructor overloading. The first constructor which takes no arguments is used to create objects which are not initialized. Second, which takes one argument is used to initialize real and imaginary parts to equal values and third which takes two arguments is used to initialize real and imaginary parts to two different values.

 //Program to perform addition of two complex numbers using constructor overloading.

# include<conio.h>

# include<iostream.h>

class Complex

{

int real,imag;

public:

   Complex()                         //Default Constructor.

   {

   }

   Complex(int r)                // Parameterised constructor for equal values.

   {

   real=r;

   imag=r;

   }

   Complex(int r,int i)       // Parameterised constructor for different values.

   {

   real=r;

   imag=i;

   }

   Complex(Complex &c)       //Copy Constructor.

   {

    real=c.real;

    imag=c.imag;

   }

   void print()

   {

   cout<<"\n The sum of two complex nos. is "<<real<<"+"<<imag<<"i.";

   }

   friend Complex sum(Complex,Complex);     //Declaration of friend function.

};

Complex sum(Complex obj1,Complex obj2)

{

 Complex obj3;

 obj3.real=obj1.real+obj2.real;

 obj3.imag=obj1.imag+obj2.imag;

 return obj3;

}

void main()

{

 clrscr();

 int a,b,c;

 Complex c1;                      //Calling default constructor.



 cout<<"\n\t\tPROGAM TO PERFORM ADDITION OF TWO COMPLEX NUMBERS USING CONSTRUCTOR OVERLOADING\t\t\n";



 cout<<"\n\t\tFor equal values :\t";

 cout<<"\n\tEnter the equal value of real and imaginary part of number 1:\n\t";

 cin>>a;

 Complex c2(a);                //Calling parameterised constructor for equal values.



 cout<<"\n\t\tFor different values :\t";

 cout<<"\n\tEnter the real and imaginary part of number 2:\n\t";

 cin>>b>>c;

 Complex c3(b,c);                         //Calling parameterised constructor for different values.



 Complex c4=sum(c2,c3);          //Calling copy constructor.

 c4.print();

 getch();

 }


Output:



















Saturday, 14 January 2017

program to explain class in C++ and print marks of student

Tags

Program to make a class student to print result of student.



#include<iostream.h>

#include<conio.h>



class student

{

int rollno;

char name[6];

int classstud;

int marks[5];

float per;

float calculate();

public:void readmarks();

       void displaymarks();

};

float student::calculate()

{

per=0;

for(int i=0;i<=5;i++)

per+=marks[i];

per=(per/5);

return per;

}

void student::readmarks()

{

cout<<"enter rollno no.:";

cin>>rollno;

cout<<"enter the name:";

cin>>name;

cout<<"enter the class standing in:";

cin>>classstud;

cout<<"enter the marks:"<<endl;

for(int j=0;j<5;j++){

cout<<"\t enter marks"<<j+1<<":";

cin>>marks[j];

}

}

void student::displaymarks()

{

cout<<"Rollno:"<<rollno<<endl;

cout<<"name:"<<name<<endl;

cout<<"class:"<<classstud<<endl;

cout<<"percentage:"<<calculate()<<endl;

}

int main()

{

student s1;

s1.readmarks();

s1.displaymarks();

getch();

}









OUTPUT


Monday, 9 January 2017

Comparison between C and C++.

Tags
  1. C++ is super set of C language.
  2. C++ program can use existing C software libraries.
  3. C follows top down approach and C++ follows bottom up approach.
  4. C adopt procedure oriented language and C++ follows object oriented language as well as procedure oriented language.
  5. Both are middle level language.
  6. In C++ data is secured because of class uses different access specifier like private and protected and C only have public access specifier.
  7.  C does not have friend and virtual function but C++ supports friend and virtual function.

Saturday, 7 January 2017

Introduction and history of C++

Tags
  • C++ was invented by Bjarne stroustrup.
  • It was invented in 1979.
  • In 1979 ,it was called C with classes . But its name was changed in 1983 and currently its called C++.
  • C++ is middle level language because its a combination of low level language and high level language.
  • low level language means machine dependent language and we can only built  system software with the help of low level language like device drivers.
  • High level language is used to build application softwares.
  • so, middle level language is includes low level as well  as high level language. so with use of C++ and C we can build system software as well as application software.
  • C++ joins three separate programming traditions .
                                                  - procedural language.
                                                   -object oriented language.
                                                   -generic programming with use of templates.
  •    oops was invented by Alan Kay and simula 16 was the first object oriented language.

multiplication of matrix in c

Tags

#include<stdio.h>

#include<conio.h>

void main()

{
int a[10][10],b[10][10],multi,r1,c1,r2,c2,i,j,k;

clrscr();

printf(“enter row and column for first matrix:”);

scanf(“%d%d”,&r1,&c1);

printf(“enter row and column for second matrix:”);

while(c1!=r2)

{
printf(“error! column of first matrix not equal to row of second:”);

printf(“enter row and column for first matrix:”

scanf(“%d%d”,&r1,&c1);

printf (“enter row and column for second matrix:”);

scanf(“%d%d”,&r1,&c2);

}

printf(“\n enter elements of matix 1:\n”);

for(i=0;i<r2;++i)

for(j=0;j<c2;j++)

{

printf(“enter elements  a%d%d:”,i+1,J+1);

scanf(“%d”,&a[i][j]);

}



printf(“\n enter elements of matix 2:\n”);

for(i=0;i<r2;++i)

for(j=0;j<c2;j++)

{

printf(“enter elements  b%d%d:”,i+1,J+1);

scanf(“%d”,&b[i][j]);

}

for(i=0;i<r1;++i)

for(j=0;j<c2;++j)

{

mult[i][j]=0;

}

for (i=0;i<r1;++i)

for(j=0;j<c2;++j)

for(k=0;k<c1;++k)

{

mult[i][j]+=a[i][k]*b[k][j];

}

printf(“\n output matrix:\n”);

for(i=0;i<r1;++i)

for(j=0;j<c2;++j)

{

printf(“%d   ”,mult[i][j]);

if(j==c2-1)

printf(“\n\n”);

}

getch();

}