Professional Documents
Culture Documents
one default constructor and another parameterized, which receives value of x and y. show()
#include<iostream.h>
#include<conio.h>
class multi
int x, y;
public:
multi()
x=0;
y=0;
multi(int a, int b)
x=a;
y=b;
void show()
}
};
void main()
clrscr();
multi m1;
multi m2(1,2);
m1.show();
m2.show();
getch();
-----------------------------x------------------------------x-------------------------------------
/* Create a class test with 2 float values fx and fy. Write 2 function input() and show() to
accept and point value response, access the function in main using pointer technique. */
#include<iostream.h>
#include<conio.h>
class test
public:
void input();
void show();
};
void test::input()
ciin>>fx>>fy;
void test::show()
void main()
clrscr();
test obj;
int *ptr=&obj;
ptr->input();
ptr->show();
------------------------------x--------------------------------x--------------------------------
/* Create a class "cal" with 3 integer variables a,b and c. Write two functions intput(), cal().
input() accepts the value while the function cal() finds the total and average of number and displays
#include<iostream.h>
#include<conio.h>
class cal
int a,b,c,av;
public:
void input()
cin>>a>>b;
void cal()
c=a+b;
av=c/2;
cout<<"Total is "<<c;
cout<<"Average is "<<av;
};
void main()
{
clrscr();
cal obj;
int *ptr=&obj;
ptr->input();
ptr->cal();
getch();
-----------------------------x-----------------------------x---------------------------------
/* Accessing different data types with public access specifier. */
#include<iostream.h>
#include<conio.h>
class first
int x;
protected:
int y;
public:
int z;
first()
x=1;
y=2;
z=3;
};
public:
void show()
//cout<<"\nAccessing x"<<x;
};
void main()
clrscr();
second obj;
obj.show();
getch();
-------------------------------x--------------------------------x---------------------------
/* Single inheritance constructor & destructor execution. */
#include<iostream.h>
#include<conio.h>
class base
public:
base()
~base()
};
public:
derived()
~derived()
{
};
void main()
clrscr();
derived d;
getch();
------------------------------x---------------------------------x-------------------------------
/* Take a class first in which there is a method in protected section namely show(). This
method prints the message "I am show() method of class first". Take another class second
which publicly inherits the class first. The class second also contains a method namely show()
#include<iostream.h>
#include<conio.h>
class first
protected:
void show()
};
public:
void show()
};
void main()
clrscr();
second obj;
obj.show();
getch();
------------------------------x-------------------------------x---------------------------------
/* Accessing derived class member using base class pointer. */
#include<iostream.h>
#include<conio.h>
class first
public:
void show()
};
public:
void show()
};
void main()
clrscr();
first obj1;
second obj2;
first *ptr;
ptr=&obj1;
ptr->show();
ptr=&obj2;
ptr->show();
getch()
-----------------------------x------------------------------x--------------------------------
/* WAP to enter name using single character variable. */
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
char ch;
ch=cin.get();
while(ch!='\n')
cout.put(ch);
cin.get(ch);
getch();
---------------------------x--------------------------------x-----------------------------------
/* Enter a line and count number of words in it. */
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int count=1;
char a;
cin.get(a);
while(a!='\n')
if(a==32)
count++;
cin.get(a);
getch();
------------------------------x------------------------------------x----------------------------
/* Various format flags to format numeric data in fixed field width for alligning the data left
or right in coloumn, displaying the positive sign and display the real number, scientific and fixed notation
*/
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
cout.fill('*');
cout.setf(ios::left, ios::adjustfield);
cout.width(10);
cout<<sqrt(2);
cout.setf(ios::right, ios::adjustfield);
cout.width(20);
cout.fill('*');
cout.precision(4);
cout.setf(ios::showpos);
cout.setf(ios::scientific, ios::floatfield);
cout<<'\n'<<sqrt(2);
cout.setf(ios::fixed, ios::floatfield);
cout<<'\n'<<sqrt(2);
getch();
-------------------------------x------------------------------------x------------------------
/* WAP to print user-defined informations using formatting functions. Input or assign strings
and numbers and define field width for them. Fill the empty spaces with character and also
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
clrscr();
float f=100;
cout.width(20);
cout<<"Address \n";
cout.width(10);
cout<<"Riya";
cout<<"\n";
cout.width(20);
cout<<"Gwalior";
cout<<"\n";
cout.fill('*');
cout.width(15);
cout<<"Riya";
cout<<"\n";
cout.precision(4);
cout<<f/3<<endl;
cout<<"\n";
cout<<sqrt(2)<<endl;
cout<<3.45678;
getch();
---------------------------x--------------------------------x---------------------------------