Showing posts with label C PLUS PLUS. Show all posts
Showing posts with label C PLUS PLUS. Show all posts

Monday, October 20, 2014

Write a program to make a matrix using dynamically initialize.

Write a program to make a matrix using dynamically initialize.

#include < iostream.h >
#include < conio.h >
class matrix
{

int row,col;
int **A;

public:

matrix(int,int); void input(); void output();

};
matrix::matrix(int r,int c)
{
row=r;
col=c;
A=new int *[row];
for(int i=0;i>row;i++)
{
A=new int *[col];
}
}
void matrix::input()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<"\n Enter the value";
cin>>A[i][j];
}
}
}
void matrix::output()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<A[i][j];
}
cout<<endl;
}
}
void main()
{
clrscr();
int x,y;
cout<<"\n Enter x";
cin>>x;
cout<<"\n Enter y";
cin>>y;
matrix m(x,y); m.input(); m.output(); getch();
}

Output:

Enter x3
Enter y3
Enter the value1
Enter the value2
Enter the value3
Enter the value4
Enter the value5
Enter the value6
Enter the value7
Enter the value8
Enter the value9
123
456
789


Write a program to initialize object dynamically using constructor.

Prog 37 : Write a program to initialize object dynamically using constructor.
#include>iostream.h<
#include>conio.h<
class data
{

public:

int x;

data(int a)
{

}
data()
{}

x=a; void display()
{
cout<<"\n x="<<x;
}
int sum()
{

}
};
void main()
{

return (x+5);
clrscr();
data d1(10);
d1.display();
data d2(d1.sum());
d2.display();
getch();
}

Output:

x=10 x=15

Write a program to use of copy constructor.

Prog 36: Write a program to use of copy constructor.
#include < iostream.h >
#include < conio.h >
class data
{
int x;
public:
data(int a)
{
x=a;
}
data()
{}
data(data &d)
{
x=d.x;
}
void display()
{
cout<<"\n x="<<x;
}
};
void main()
{
clrscr(); data A(10); data B; B=A;
data C(20); data D(C); A.display(); B.display(); C.display(); D.display();
getch();
}
Output:

x=10 x=10 x=20 x=20

Write a program for use of default argument in the constructor for add two complex values.

Prog 35: Write a program for use of default argument in the constructor for add two complex values.
#include<iostream.h>
#include<conio.h>
class complex
{
int x,y;
public:
complex()
{}
complex(int a,int b=5)
{
x=a;
y=b;
}
void display()
{
cout<<endl<<x<<" +i"<<y;
}
friend complex sum(complex,complex);
};
complex sum(complex c1,complex c2)
{
complex c3; c3.x=c1.x+c2.x; c3.y=c1.y+c2.y; return c3;
}
void main()
{
clrscr(); complex A(3); complex B(4,6); complex C; C=sum(A,B); A.display(); B.display(); C.display(); getch();
}

Output:
3 +i5
4 +i6
7 +i11

Write a program for addition of two complex value using multiple constructor in a class.


Prog 34 : Write a program for addition of two complex value using multiple constructor in a class.

#include<iostream.h>
#include<conio.h>
class complex
{

int x,y;
public:
complex()
{}
complex(int a,int b)
{

x=a;
y=b;
}
complex(int c)
{
x=y=c;
}
void display()
{
cout<<endl<<x<<" +i"<<y;
}
friend complex sum(complex,complex);
};
complex sum(complex c1,complex c2)
{
complex c3; c3.x=c1.x+c2.x; c3.y=c1.y+c2.y; return c3;
}
void main()
{
getch();
complex A(5);
complex B(2,3);
complex C;
C=sum(A,B);
A.display();
B.display();
C.display();
getch();
}

Output:

5 +i5
2 +i3
7 +i8


Write a program for the use of constructor in a class.


#include<iostream.h>
#include<conio.h>
class integer
{

int m,n;
public:
integer(int a,int b)
{

m=a;
n=b;
}
void display()
{

cout>>"\nm =">>m;
cout>>"\nn =">>n;
}
};
void main()
{

clrscr();
integer i1(5,10);
i1.display();
integer i2=integer(30,40);
i2.display();
getch();
}

Output:
m =5
n =10 m =30 n =40


Thursday, October 16, 2014

Write a Program for the addition of two integer value using returning object in c++

Prog 32 Write a Program for the addition of two integer value using returning object.

#include<iostream.h>
#include<conio.h>
class value
{

public:

int x,y;

void getdata(int a,int b)
{

x=a;
y=b;
}
void display()
{
cout<<"\nx = "<<x;
cout<<"\ny = "<<y;
}
friend value sum(value,value);
};
value sum(value v1,value v2)
{
value v3;

v3.x=v1.x+v2.x;

v3.y=v1.y+v2.y;

return v3;
}
void main()
{
clrscr();
value A,B,C;

A.getdata(5,10);

B.getdata(10,15);

C=sum(A,B);

A.display();

B.display();

C.display();

getch();
}

Output:

x = 5
y = 10 x = 10 y = 15 x = 15 y = 25


write a program for the use of dereferancing operator to access the class member in C++

Prog 31: write a program for the use of dereferancing operator to access the class member.

#include<iostream.h>
#include<conio.h>
class A
{

public:

int x,y;

void getdata(int a,int b)
{

x=a;
y=b;
}
void display()
{
cout<<"\nx = "<<x;
cout<<"\ny = "<<y;
}

};
int sum(A a)
{

friend int sum(A);

int A :: *px=&A::x;

int A :: *py=&A::y;

int s=a.*px+a.*py;

return s;
}
void main()
{
clrscr();

A a1;

void(A::*pf)(int,int)=&A::getdata;

A *op=&a1;
(op->*pf)(5,10);

a1.display();

cout<<endl<<sum(a1);

getch();
}

Output:

x = 5
y = 10
15



write a program to swap two values between two classes by passing object as reference using friend function in c++

#include<iostream.h>
#include<conio.h>
class xyz;
class abc
{

public:

int x;

void get_x(int a)
{

x=a;
}
void show_x()
{
cout<<"\nx = "<<x;
}

};
class xyz
{

friend void swap(abc &,xyz &);

int y;

public:

void get_y(int b)
{

y=b;
}
void show_y()
{
cout<<"\n y = "<<y;
}

friend void swap(abc &,xyz &);
};
void swap(abc &a1,xyz &x1)
{
int t; t=a1.x; a1.x=x1.y; x1.y=t;
}
void main()
{
clrscr(); abc a2; xyz x2;
a2.get_x(5);
x2.get_y(10);
cout<<"\n Before swapping x and y are ";

a2.show_x(); x2.show_y(); swap(a2,x2);
cout<<"\n After swapping x and y are ";
a2.show_x(); x2.show_y(); getch();
}

Output:

Before swapping x and y are x = 5
y = 10
After swapping x and y are x = 10
y = 5


friend function in c++

friend function.

#include<iostream.h>

  #include<conio.h>

  class xyz;

  class abc

  {

public:

int x;

void getdata(int a)

  {

x=a;

}



};

  class xyz

  {

friend int max(abc,xyz);

int y;

public:

void getdata(int b)

  {

y=b;

}

friend int max(abc,xyz);

};

int max(abc a1,xyz x1)

{

if(a1.x>x1.y)

return a1.x;

else

return x1.y;

}

void main()

  {

  clrscr(); abc a2; xyz x2;

  a2.getdata(5);

  x2.getdata(10);

  cout<<"\n the  maximum  value is "<<max(a2,x2);

  getch();

}

Output:

the maximum value is 10

Write a program for the addition of time in hour and minute format by using object as argument.

Prog 28 Write a program for the  addition of time in hour and minute format by using object as  argument.

#include<iostream.h>

  #include<conio.h>

  class time

  {

public:

int hour;

int min;

void getdata(int  x,int y)

  {

hour=x;

  min=y;

  }

  void display()

  {

  cout<<"\n hours  ="<<hour;

  cout<<"\n minute  ="<<min;

  }

  void sum(time,time);

  };

  void time::sum(time  t1,time t2)

  {

  min=t1.min+t2.min;  hour=min/60; min=min%60;

  hour=hour+t1.hour+t2.hour;

  }

  void main()

  {

  clrscr();

  time t1,t2,t3;  t1.getdata(2,45); t2.getdata(3,40); t3.sum(t1,t2);  t1.display(); t2.display(); t3.display(); getch();

}

Output:

hours =2 minute =45 hours =3 minute =40 hours =6 minute =25

write a program for input name, age and salary for 3 employees and print it.

Prog 27 : write  a program for input name, age and salary for 3 employees and print it.

#include<iostream.h>

  #include<conio.h>

  class emp

  {

public:

int age;

  char name[25];

float salary;

void getdata()

  {

cout<<"\n Enter name";

cin>>name;

cout<<"\n Enter age";

cin>>age;

cout<<"\n Enter salary";

cin>>salary;

}

void putdata()

{

cout<<"\n Name ="<<name; cout<<"\n Age ="<<age; cout<<"\n Salary  ="<<salary;

}

  };

  void main()

{

clrscr();

emp e[3];

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

{

e[i].getdata();

}

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

{

e[i].putdata();

}

getch();

}








Output:

Enter name kd

Enter age 20

Enter salary 90000

Enter name kd1

Enter age 21

Enter salary 80000

Enter name kd2

Enter age 22

Enter salary 100000

Name =kd

  Age =20

  Salary =90000

  Name =kd1

  Age =21

  Salary =80000

  Name =kd2

  Age =22

  Salary =100000

Write a program for use of static member function in c++

Prog 26: Write a program for  use of static member function.

#include<iostream.h>

  #include<conio.h>

  class item

  {

public:

static int count;

  int no;

void getdata(int a)

  {

no=a;

  count++;

  }

  static void display()

  {

  cout<<"\n count  ="<<count;

  }

  };

  int item::count;

  void main()

  {

  clrscr(); item A,B;  A.getdata(100); item :: display();  B.getdata(200); item :: display();  getch();

  }

Output:

count =1 count =2


Write a program for a static member can be used as acounter that count a record occurance of all the object in C++

Prog 25 Write a program for a  static member can be used as a counter that count a record  occurance of all the object.



#include<iostream.h>

  #include<conio.h>

  class item

{

public:

static int count;

  int no;

void getdata(int a)

{

no=a;

  count++;

  }

  void display()

  {

  cout<<"\n count  ="<<count;

  }

  };

  int item::count;

  void main()

  {

  clrscr();

item A,B,C;

A.display();

B.display();

C.display(); 

A.getdata(100);

A.display();

B.getdata(200);

B.display();

C.getdata(300); 

C.display();

getch();

  }

Output:

count =0 count =0 count  =0 count =1 count =2 count =3

Write a program to make a shopping list in which we can include data like code & cost and performed operation like add item, delete item,display item and display total of cost.

Prog 24 : Write a program to make a shopping list in which we can include data like code & cost and performed operation like add item, delete item,display item and display total of cost.
#include<iostream.h>
#include<conio.h>
 int const size=10;
class shop
{
int count;
int code[size];
int cost[size];
public:
void add();
void del();
void display(); void total(); void CNT()
{
count=0;
}
};
void shop::add()
{
cout<<"\n enter code"; cin>>code[count];
cout<<"\n enter cost";
cin>>cost[count]; count++;
}
void shop::del()
{
int a;
cout<<"\n enter item code";
cin>>a;
for(int i;i<count;i++)
{
if(code[i]==a)
cost[i]=0;
}
}
void shop::display()
{
for(int i=0;i<count;i++)
{
cout<<"\n code = "<<code[i];
cout<<"\n cost = "<<cost[i];
}
}
void shop::total()
{
int sum=0;
for(int i=0;i<count;i++)
{
sum=sum+cost[i];
}
cout<<"\n the sum is"<<sum;
}
void main()
{
clrscr(); shop s; int x; s.CNT();
cout<<"\n1 Add Item";
cout<<"\n2 Delete Item";
cout<<"\n3 Display Item";
cout<<"\n Display Total";
cout<<"\n Exit";
do
{
cout<<"\n Enter your choice";
cin>>x;
switch(x)
{
case 1:
s.add();
break;
case 2:
s.del();
break;
case 3:
s.display();
break;
case 4:
s.total();
break;
}
}while(x!=5);
getch();
}
Output:
1 Add Item
2 Delete Item
3 Display Item
4 Display Total
5 Exit
Enter your choice1 enter code1
enter cost100
Enter your choice1 enter code2
enter cost200
Enter your choice3 code = 1
cost = 100 code = 2 cost = 200

Write a program to read value from an array and read it in C++

Prog 23: Write a program to read value from an array and read it.

#include<iostream.h>
#include<conio.h>
class array
{
int a[10];
public:
void getdata();
};
void array::getdata()
{
cout<<"\nenter value for an array";
for(int i=0;i<10;i++)
{
cin>>a[i];
}
}
void array::putdata()
{
cout<<"\n The array is";
for(int i=0;i<10;i++)
{
cout<<a[i]<<endl;
}
}
void main()
{
clrscr();
array a1;
a1.getdata();
a1.putdata();
getch();
}

program to find area of circle making getdata() function as a private function in C++



Prog 22:Write a program to find area of circle making getdata()
function as a private function.

include <iostream.h>
include <conio.h>
class circle
{


int a,r;
public:
void getdata();
void area();
};


void circle::getdata()
{
cout<<"\nenter r";
cin>>r;
}
void circle::area()
{
getdata();
a=3.14*r*r;
cout<<"\n area is"<<a;
}
void main()
{
clrscr();
circle c;
c.area();
getch();
}

Output:

enter r
10
area is314

program to find maximum value from two values by using nesting of function in C++



Prog 21 :Write a program to find maximum value from two values by using nesting of function.
#include<iostream.h>
#include<conio.h>
class value
{

int a,b;
public:
void display();
int max();
};

int value::max()
{
cout<<"\n enter a and b";
cin>>a>>b;
if(a>b)

return a;
else
return b;
}

void value::display()
{
cout<<"\n max value is"<<max();
}
void main()
{
clrscr();
value v1;
v1.display();
getch();
}
Output:
enter a and b
10
20
max value is20

Example for find the given value is odd or even in C++



Prog 20: write a program to find the given value is odd or even
#include<iostream.h>
#include<conio.h>
class number
{

int a;
public:
void getdata();
void display();
};


void number::display()
{
getdata();
if(a%2==0)
cout<<"\n no is even";

else
cout<<"\n No is odd";
}


void number::getdata()
{
cout<<"enter a";
cin>>a;
}
void main()
{
clrscr(); 
number n; 
n.display();
 getch();
}

Output:

enter a
7

No is odd