Академический Документы
Профессиональный Документы
Культура Документы
C++ Programs
main()
{
enum day{Sun, Mon, Tue, Wed, Thu, Fri, Sat};
int dy;
cout<<"\nEnter Day:- ";
cin>>dy;
switch(dy)
{
case Sun: cout<<"\nSUNDAY"; break;
case Mon: cout<<"\nMONDAY"; break;
case Tue: cout<<"\nTUESDAY"; break;
case Wed: cout<<"\nWEDNUSDAY"; break;
case Thu: cout<<"\nTHURSDAY"; break;
case Fri: cout<<"\nFRIDAY"; break;
case Sat: cout<<"\nSATURDAY"; break;
default: cout<<"Invalid Day";
}
getch();
}
main()
{
clrscr();
int a=10;
int b=20;
int c=30;
// Constant Pointer
int* const p=&a;
cout<<"\n*p= "<<*p;
*p=100;
cout<<"\na= "<<a;
// p=&b; This can not be done as pointer p is a const pointer.
//Pointer to Constant
const int *q=&b;
q=&a;
cout<<"\n*q= "<<*q;
q=&b;
// *q=200; This can not be done as pointer q is a pointer to constant
// so using q we can not change the value of b;
cout<<"\n*q= "<<*q;
//Constant Pointer to Constant
const int* const z=&c;
// z=&b; This can not be done as its const pointer
// *z=100; This can not be done as its pointer to constant
cout<<"\n*z= "<<*z;
getch();
}
main()
{
clrscr();
int a=10;
int &r=a;
cout<<"\nr= "<<r;
cout<<"\na= "<<a;
a=100;
cout<<"\nr= "<<r;
cout<<"\na= "<<a;
r=200;
cout<<"\nr= "<<r;
cout<<"\na= "<<a;
getch();
}
void fun()
{
extern int a;
cout<<"\nFrom fun Function a= "<<a;
}
int a=100;
main()
{
clrscr();
extern int a;
cout<<"\nFrom main Function a= "<<a;
fun();
getch();
}
main()
{
clrscr();
int *p,*q,*r;
int val,max;
p=new int;
cout<<"\nEnter a Value:- ";
cin>>*p;
cout<<"\n *p= "<<*p;
delete p;
cout<<"\nEnter a Value:- ";
cin>>val;
q=new int(val);
cout<<"\n *q= "<<*q;
delete q;
cout<<"\nEnter Max Items:- ";
cin>>max;
r=new int[max];
for(int i=0;i<max;i++)
{
cout<<"\nEnter value no "<<i+1<<" ";
cin>>r[i];
}
cout<<"\nEntered values are ";
{
cout<<" "<<r[i];
}
delete []p;
/* This is also possible
for(i=0;i<max;i++)
delete (int*) r[i];
*/
delete r;
getch();
}
// Program to show the use of Call by Value, Call by Address & Call by Reference
void CallByVal(float a)
{
a=100; // The local variable "a" 's value is changed locally
}
void CallByAdd(int *p)
{
*p=200; // The pointer "p" changes the value of the variable whose address
// it has
}
void CallByRef(int &r)
{
r=300; // The reference variable "r" changes the value of the
// original variable, whose reference it was created
}
main()
{
clrscr();
int a=10;
cout<<a;
CallByVal((float)a);
cout<<a;
CallByAdd(&a);
cout<<a;
CallByRef(a);
cout<<a;
getch();
}
Experiment No. 10
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int a,b;
cout<<"\nEnter data for a= ";
cin>>a;
cout<<" Enter data for b= ";
cin>>b;
Greater(a,b)=100; // Here the greater value containing variable will (to show the property of reference some
value should be assigned to it)
//be returned and so its value
//would be replaced by 100
9to
Developed by Partha Roy Page 13
if(a==100)
{
cout<<"\na>b";
}
else
{
cout<<"\nb>a";
}
getch();
}
Experiment No. 11
#include<iostream.h>
#include<conio.h>
class MyClass
{
int data;// Private data member.
//They can not be accessed from outside the class definition.
public:
void fill() // Public function member
{
cout<<"\nEnter value for data= ";
cin>>data; // "data" accessible inside the class
}
void show(); // The Member functions can be defined outside the class
};
void MyClass::show()
{
cout<<"\ndata= "<<data; //"data" is accessible because the declaration of show()
is
// inside the class
}
Experiment No. 13
#include<iostream.h>
#include<conio.h>
class MyClass
{
static int cnt;
public:
static void count() // static member functions can only access
// static data members
{
cnt+=1;
}
void show()
{
cout<<"\nOutput from show() function :- count() called "<<cnt<<" times";
// non static functions can access static members
}
};
int MyClass::cnt; (should be declared just ouytside class)
// This creates the static member and initilizes it to 0
// This initilization is invoked only when the first object of
// the class is created
main()
{
clrscr();
MyClass ob1;
ob1.show();
MyClass::count();// This is the only way to call/use static members
ob1.show();
MyClass::count();
ob1.show();
MyClass::count();
ob1.show();
getch();
}
class MyClass2;
class MyClass1
{
int a;
public:
void fill()
{
cout<<"\nEnter data for MyClass1:- ";
cin>>a;
}
friend void show(MyClass1 ob1, MyClass2 ob2);
};
class MyClass2
{
int b;
public:
void fill()
{
cout<<"\nEnter data for MyClass2:- ";
cin>>b;
}
friend void show(MyClass1 ob1, MyClass2 ob2);
};
#include<iostream.h>
#include<conio.h>
{
cout<<"\nEnter data for MyClass1:- ";
cin>>ob.a;
//since class2 is sa friend of class1
} so it can access its private memberd directly through object
and no need of public functions
void show(MyClass1 ob)
{
cout<<"\nData of MyClass1 from MyClass2:- "<<ob.a;
}
};
main()
{
clrscr();
MyClass1 ob1;
MyClass2 ob2;
ob2.fill(ob1);
ob2.show(ob1);
getch();
}
Experiment No. 16
#include<iostream.h>
#include<conio.h>
class MyClass
{
int a;
public:
void fill()
{
cout<<"\nEnter data for MyClass:- ";
cin>>a;
}
void show() const
{
// a=100; This cannot be done as show() is a const function
cout<<"\nData of MyClass:- "<<a;
}
};
main()
{
clrscr();
MyClass ob;
ob.fill();
ob.show();
getch();
}
Experiment No. 17
#include<iostream.h>
#include<conio.h>
class MyClass1
{
int a;
class MyClass2
{ mainly used to secure data of one class from another &
int b; when it is needed that any operation on ne data will automati-
cally do the same on other,
public:
void fill()
{
cout<<"\nEnter Data for MyClass2:- ";
cin>>b;
}
void show()
{
cout<<"\nData of MyClass2:- "<<b;
}
}ob1;
public:
void fill()
{
cout<<"\nEnter Data for MyClass1:- ";
cin>>a;
ob1.fill();
}
void show()
{
cout<<"\nData of MyClass1:- "<<a;
ob1.show();
}
};
main()
{
clrscr();
MyClass1 ob;
MyClass1::MyClass2 ob2; we can make object of inner class of private part like this
ob.fill();
ob.show();
ob2.fill();
ob2.show();
getch();
}
Experiment No. 18
#include<iostream.h>
#include<conio.h>
class MyClass
{
int a;
public:
MyClass()
{
cout<<"\nDefault Constructor Invoked";
a=10;
}
void show()
{
cout<<"\nThe data in MyClass:- "<<a;
}
};
main()
{
clrscr();
MyClass ob1;
MyClass ob2=MyClass();
MyClass *op1;// This will Not invoke the Constructor function
// But "MyClass *op1=new MyClass();" this will invoke the constructor
ob1.show();
ob2.show();
getch();
}
class MyClass
{
int a;
public:
MyClass()
{
cout<<"\nDefault Constructor Invoked";
a=10;
}
MyClass(int z)
{
cout<<"\nParameterised Constructor Invoked";
a=z;
}
void show()
{
cout<<"\nData in MyClass:- "<<a;
}
};
main()
{
clrscr();
MyClass ob1,ob2(100); //This is called Implicit call to the constructor
MyClass ob3;// This way also objects can be created
//This called Explicit call to the constructor
ob3=MyClass(200);
ob1.show();
ob2.show();
ob3.show();
getch();
}
Experiment No. 20
#include<iostream.h>
#include<conio.h>
class MyClass
{
int a;
// The accessibility of a constructor depends upon the
// access mode under which it had been declared
public:
MyClass(){}// This default constructor is needed for creating simple objects
MyClass(MyClass &ob)
{
cout<<"\nCopy Constructor Called";
a=ob.a;
}
void fill()
{
cout<<"\nEnter data for MyClass:- ";
cin>>a;
}
void show()
{
cout<<"\nThe data of MyClass:- "<<a;
}
};
main()
{
clrscr();
MyClass ob1,ob2;
ob1.fill();
ob2.fill();
ob1.show();
ob2.show();
MyClass ob3(ob1);//Here we can also use = to invoke the COPY
CONSTRUCTOR
//i.e during INITIALIZATION not during ASSIGNMETNT
ob3.show();
ob3.show();
getch();
}
class MyClass
{
char obname[20];
public:
MyClass(char *onm)
{
cout<<"\nDefault Constructor Invoked for object name:"<<onm;
strcpy(obname,onm);
}
~MyClass()
{
cout<<"\nDestructor Invoked for Object:- "<<obname;
}
};
main()
{
clrscr();
MyClass ob1("First"),ob2("Second"),ob3("Third");
getch();
}
class MyClass
{
int a;
public:
void fill()
{
cout<<"\nEnter the data for MyClass ";
cin>>a;
}
void show()
{
cout<<"\nThe data of MyClass "<<a;
}
//The calling object is the operand in the L.H.S of the operator +=
//and the input argument is the R.H.S operand of the operator +=
void operator +=(MyClass ob)
{
a=a+ob.a;
}
};
class MyClass
{
int a;
public:
void fill()
{
cout<<"\nEnter the data for MyClass ";
cin>>a;
}
void show()
{
cout<<"\nThe data of MyClass "<<a;
}
// Here the object in the LHS is the operand calling this function
// The object in the RHS is the operand as the input argument of this function
// The returned object is the resulting object due to the operation "+"
class MyClass
{
int a;
public:
void fill()
{
cout<<"\nEnter the data for MyClass ";
cin>>a;
}
void show()
{
cout<<"\nThe data of MyClass "<<a;
}
void operator ++() //Here the calling object itself is the Operand
{
cout<<"\nOverloaded '++' operator called ";
a+=1;
}
};
class A
{
int a;
public:
A()
{
a=10;
}
//LHS + //RHS
friend void operator +(A &ob1, A&ob2)
{
cout<<"\n"<<ob1.a+ob2.a;
}
//operand
friend void operator ++(A &ob)
{
ob.a+=1;
cout<<"\n"<<ob.a;
}
};
main()
{
clrscr();
A ob1,ob2;
//LHS + //RHS
ob1 + ob2;
//operand
ob1++;
getch();
}
class MyClass
{
int a;
public:
void fill()
{
cout<<"\nEnter data for MyClass ";
cin>>a;
}
// Here we need to use the Friend keyword for two input parameters
// This is because the original cout<< syntax will only allow single
// input parameter.
// For allowing parameters of two different classes i.e ostream and MyClass
// we need to make it Friend.
friend ostream& operator << (ostream &out,MyClass ob)
{
out<<"\nData in MyClass "<<ob.a;
return out;
}
};
main()
{
clrscr();
MyClass ob1;
ob1.fill();
cout<<ob1;
getch();
}
class MyClass
{
int a;
public:
operator int()// Overloading "int()" operation
{
return a;
}
MyClass(int z)
{
a=z;
}
};
main()
{
clrscr();
MyClass ob1(100);
int in=int(ob1); //Overloaded "int()" Operator called
cout<<in;
getch();
}
class MyClass
{
private:
int a;
void fill()
{
cout<<"\nEnter data for MyClass:- ";
cin>>a;
}
protected:
void show()
{
cout<<"\nData of MyClass:- "<<a;
}
public:
void callfill()
{
fill(); // Calling "fill()" which is defined under Private mode
}
void callshow()
{
show(); // Calling "show()" which is defined under Protected mode
}
};
class Parent
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent Class:- ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};
main()
{
clrscr();
Child c1;
//Here the Child class functions are used
c1.fill();
c1.show();
cout<<"\n";
//Here Parent class functions are used through Child class Onjects
c1.Parent::fill();
c1.Parent::show();
getch();
}
class Parent
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent Class:- ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};
main()
{
clrscr();
Child c1;
//Here the Child class functions are used
c1.fill();
c1.show();
cout<<"\n";
c1.ParentFill();
c1.ParentShow();
/* Here Parent class functions can not be called as they all are private
in Child class.
c1.Parent::fill();
c1.Parent::show();
*/
getch();
}
class Parent
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent Class:- ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};
main()
{
clrscr();
Child c1;
//Here the Child class functions are used
c1.fill();
c1.show();
cout<<"\n";
c1.ParentFill();
c1.ParentShow();
/* Here Parent class functions can not be called as they all are
under Protected mode in the Child class.
c1.Parent::fill();
c1.Parent::show();
*/
getch();
}
class Parent
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent Class:- ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};
class Parent1
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent1 Class:- ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent1 Class:- "<<a;
}
};
class Parent2
{
int b;
public:
void fill()
{
cout<<"\nEnter data for Parent2 Class:- ";
cin>>b;
}
class Parent
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent Class:- ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};
main()
{
clrscr();
Child1 c1;
Child2 c2;
c1.fill();
c1.show();
cout<<"\n";
c2.fill();
c2.show();
cout<<"\n";
c1.fillParent();
c1.showParent();
cout<<"\n";
c2.fillParent();
c2.showParent();
getch();
}
class Parent
{
int a;
public:
void fill()
{
cout<<"\nEnter data for Parent Class: ";
cin>>a;
}
void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};
class Parent
{
int a;
public:
Parent(int z)
{
a=z;
}
void show()
{
cout<<"\nData of Parent Class "<<a;
}
};
class Parent
{
int a;
public:
Parent(int z)
{
a=z;
}
void show()
{
cout<<"\nData of Parent Class "<<a;
}
};
main()
{
clrscr();
Child2 c(10,20,30);
c.show();
getch();
}
class Parent1
{
int a;
public:
Parent1(int z)
{
a=z;
}
void show()
{
cout<<"\nData of Parent1 Class "<<a;
}
};
class Parent2
{
int b;
public:
Parent2(int x)
{
b=x;
}
void show()
{
cout<<"\nData of Parent2 Class "<<b;
}
};
main()
{
clrscr();
Child c(10,20,30);
c.show();
getch();
}
class Parent
{
int a;
public:
Parent(int z)
{
a=z;
}
void show()
{
cout<<"\nData of Parent Class "<<a;
}
};
main()
{
clrscr();
Child1 c1(10,20);
c1.show();
Child2 c2(30,40);
c2.show();
getch();
}
class A
{
int a;
public:
A()
{
cout<<"Class A constructor Called ";
}
void fill()
{
cout<<"\nEnter data for Class A :- ";
cin>>a;
}
void show()
{
cout<<"\nData of Class A:- "<<a;
}
};
class B
{
int b;
public:
getch();
}
class Parent
{
int a;
public:
virtual void fill()
{
cout<<"\nEnter data for Parent Class:- ";
cin>>a;
}
virtual void show()
{
cout<<"\nData of Parent Class:- "<<a;
}
};
//Program to show the use of Pure Virtual Functions and Abstract class
class Parent
{
public:
// Functions fill() & show() are Pure Virtual Functions
// This causes Class Parent to become Abstract.
// Any class inheriting this class would automatically become Abstract
// To avoid this the child class should define the functions.
virtual void fill()=0;
virtual void show()=0;
};
main()
{
clrscr();
float a,b,c;
cout<<"\nEnter a Float value :- ";
cin>>a;
cout<<"\nEnter a Float value :- ";
cin>>b;
cout<<"\nEnter a Float value :- ";
cin>>c;
cout<<"\nThe Entered Values Are:-\n";
cout.width(20);
cout.fill('!');
cout.precision(3);
cout<<a;
cout.width(20);
cout.fill('$');
cout.precision(2);
cout<<b;
cout.width(20);
cout.fill('#');
cout.precision(1);
cout<<c;
getch();
}
main()
{
clrscr();
fstream f;
char c1;
f.open("c:\\Test.txt",ios::out);//Opening file "c:\Test.txt" in write only mode
for(int i=1;i<=5;i++)
{
cout<<"\nEnter a Character:- ";
cin>>c1;
f.put(c1);//putting one character at a time into the file
}
f.close();
f.open("c:\\Test.txt",ios::in);//Opening file "c:\Test.txt" in read only mode
char c2;
while(!f.eof()) //eof() returns zero value when eof is not encountered
//otherwise it returns non zero value when eof is encountered.
{
f.get(c2);//getting one character at a time from the file
cout<<c2;
}
f.close();
getch();
}
main()
{
clrscr();
fstream f;
char c1;
f.open("c:\\Test2.txt",ios::out);//Opening file "c:\Test2.txt" in write mode
for(int i=1;i<=6;i++)
{
cout<<"\nEnter a Character:- ";
cin>>c1;
f.put(c1);//putting one character at a time into the file
}
cout<<"\n";
char c2;
cout<<"\nUsing tellg() function:- "<<f.tellg();
cout<<"\nUsing tellp() function:- "<<f.tellp();
//f.seekp(0,ios::beg);
f.seekg(0,ios::beg);
class Student
{
char name[40];
int roll;
public:
void fill()
{
cout<<"\nEnter The Name of the Student:- ";
gets(name);
cout<<"\nEnter Roll Number:- ";
cin>>roll;
}
void show()
{
cout<<"\nThe Name of the Student:- ";
puts(name);
cout<<"\nRoll Number:- "<<roll;
}
};
Student Temp;
f.open("c:\\Test3.txt",ios::in);//Opening the file in Read Mode
cout<<"\nReading Data From File....\n";
while(!f.eof())
{
f.read((char*)&Temp, sizeof(Temp));
Temp.show();
}
f.close();
getch();
}
#include<iostream.h>
main()
{
try
{
throw(10);
/*Here the exception argument is an integer value 10, So it can be
caught by that case which has int in the parameter list, here implicit
type conversion is possible */
}
catch(int a)
{
cout<<"\nException Caught:- "<<a;
}
cout<<"\n";
}
Best of Luck