You are on page 1of 19

/* Create class "multi" with 2 data members x and y. Class has multiple constructor and i.e.

one default constructor and another parameterized, which receives value of x and y. show()

display the value accordingly. */

#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()

cout<<"First Value = "<<x;

cout<<"\nSecond Value = "<<y;

}
};

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

float fx, fy;

public:

void input();

void show();

};

void test::input()

cout<<"Enter 2 float values";

ciin>>fx>>fy;

void test::show()

cout<<"\nValues are "<<fx<<" and "<<fy;

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

it. Use pointer technique to access members of class. */

#include<iostream.h>

#include<conio.h>

class cal

int a,b,c,av;

public:

void input()

cout<<"Enter 2 values ";

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;

};

class second:public first

public:

void show()

//cout<<"\nAccessing x"<<x;

cout<<"\nProtected data y "<<y;


cout<<"\nProtected data z "<<z;

};

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()

cout<<"\nThis is Base class constructor";

~base()

cout<<"\nBase class destructor";

};

class derived:public base

public:

derived()

cout<<"\nThis is derived class constructor";

~derived()
{

cout<<"\nDerived class destructor";

};

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()

which prints the message "Ï am show() method of class second".

WAP to call both methods. */

#include<iostream.h>

#include<conio.h>

class first

protected:

void show()

cout<<"I am show() method of class first.";

};

class second:public first

public:

void show()

cout<<"I am show() method of second class";

};
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()

cout<<"\nThis is first class member using base class method";

};

class second:public first

public:

void show()

cout<<"\nThis is first class disp() method";

};

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);

cout<<"Number of words are "<<count;

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

define size of numerical float values. */

#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---------------------------------

You might also like