0% found this document useful (0 votes)
56 views

Formatted Console I/O Operation Coding

This document contains code snippets demonstrating various C++ concepts including: 1. Formatted console I/O operations like setting width, precision, and justification of output. 2. File handling operations like opening, writing, reading from text and binary files. 3. Class definitions for single and multiple inheritance, copy constructor, dynamic memory allocation, friend functions, operator overloading, and virtual functions.

Uploaded by

aloneface
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Formatted Console I/O Operation Coding

This document contains code snippets demonstrating various C++ concepts including: 1. Formatted console I/O operations like setting width, precision, and justification of output. 2. File handling operations like opening, writing, reading from text and binary files. 3. Class definitions for single and multiple inheritance, copy constructor, dynamic memory allocation, friend functions, operator overloading, and virtual functions.

Uploaded by

aloneface
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 29

FORMATTED CONSOLE I/O OPERATION

CODING:

# include<iostream.h>
# include<conio.h>
# include<math.h>
void main()
{
clrscr();
cout.fill('*');
cout.setf(ios::left,ios::adjustfield);
cout.width(10);
cout<<"VALUE";
cout.setf(ios::right,ios::adjustfield);
cout.width(15);
cout<<"SQRT OF VALUE"<<"\n";
cout.fill('*');
cout.precision(4);
cout.setf(ios::showpoint);
cout.setf(ios::showpos);
cout.setf(ios::fixed,ios::floatfield);
for(int n=1;n<=10;n++)
{
cout.setf(ios::internal,ios::adjustfield);
cout.width(5);
cout<<n;
cout.setf(ios::right,ios::adjustfield);
cout.width(20);
cout<<sqrt(n)<<"\n";
}
cout.setf(ios::scientific,ios::floatfield);
cout<<"\nsqrt(100)="<<sqrt(100)<<"\n";
getch();
}
OUTPUT:

VALUE*******SQRT OF VALUE
+***1*************+1.0000
+***2*************+1.4142
+***3*************+1.7321
+***4*************+2.0000
+***5*************+2.2361
+***6*************+2.4495
+***7*************+2.6458
+***8*************+2.8284
+***9*************+3.0000
+**10*************+3.1623

sqrt(100)=+1.0000e+01

FILE HANDLING

CODING:
# include<iostream.h>
# include<fstream.h>
int main()
{
ofstream outf(“ITEM”);
cout<<”Enter item name:”;
char name[30];
cin>>name;
outf<<name<<”\n”;
cout<<”Enter item cost:”;
float cost;
cin>>cost;
outf<<cost<<”\n”;
outf.close();
ifstream inf(“ITEM”);
inf>>name;
inf>>cost;
cout<<”\n”;
cout<<”Item name:”<<name<<”\n”;
cout<<”Item cost:”<<cost<<”\n”;
inf.close();
return 0;
getch();
}

OUTPUT:
Enter item name:cd-rom
Enter item cost:250

Item name:cd-rom
Item cost:250

//this pointer

# include<iostream.h>
# include<conio.h>
# include<string.h>
class person
{
char name[20];
float age;
public:
person(char*s,float a)
{
strcpy(name,s);
age=a;
}
person &person::greater(person&x)
{
if(x.age>=age)
return x;
else
return*this;
}
void display(void)
{
cout<<name<<"\n";
cout<<age<<"\n";
}};
void main()
{
person p1("john",40),
p2("ahmed",30),
p3("madhan",20);
person p=p1.greater(p2);
clrscr();
cout<<"elder person is:\n";
p.display();
p=p1.greater(p3);
cout<<"the elder person is:\n";
p.display();
getch();
}

COPY CONSTRUCTOR

PROGRAM
# include<iostream.h>
# include<conio.h>
class code
{
int id;
public:
code(){}
code(int a)
{ id=a;
}
code(code&x)
{ id=x.id;
}
void display(void)
{ cout<<id<<"\n";
}};
int main()
{
clrscr();
code a(100);
code b(a);
code c=a;
code d;
d=a;
cout<<"\nid of A:";a.display();
cout<<"\nid of B:";b.display();
cout<<"\nid of C:";c.display();
cout<<"\nid of D:";d.display();
getch();
return 0;
}

OUTPUT:
id of A:100

id of B:100

id of C:100

id of D:100

DYNAMIC CONSTRUCTOR
PROGRAM
# include<iostream.h>
# include<conio.h>
class vector
{
int *v;
int sz;
public:
vector(int size)
{
sz=size;
v=new int[size];
}
void read();
void show();
};
void vector::read()
{
for(int i=0;i<sz;i++)
{
cout<<"\tEnter vector:";
cin>>v[i];
}}
void vector::show()
{
int sum=0;
for(int i=0;i<sz;i++)
sum+=v[i];
cout<<"\tVector sum"<<sum;
}
void main()
{
int count=0;
cout<<"How many elements";
cin>>count;
vector v1(count);
v1.read();
v1.show();
getch();
}
OUTPUT:
How many elements in vector:3
Enter vector:1
Enter vector:2
Enter vector:3
Vector sum:6

CLASSES AND OBJECTS


PROGRAM
# include<iostream.h>
# include<conio.h>
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void)
{
cout<<"\n\tNUMBER"<<number;
cout<<"\n\tCOST"<<cost;
}};
void item::getdata(int a,float b)
{
number=a;
cost=b;
}
void main()
{
item x;
item y;
clrscr();
cout<<"\tobject x" <<"\n";
x.getdata(100,200.5);
x.putdata();
cout<<"\nObject y"<<"\n";
y.getdata(200,75.75);
y.putdata();
getch();
}
OUTPUT:
Object x:
NUMBER:100
COST:200.5

Object y:
NUMBER:200
COST:75.75

SINGLE INHERITANCE
PROGRAM
# include<iostream,.h>
# include<conio.h>
class b
{
int a;
public:
int b;
void get_ab();
int get_a(void);
void show_a(void);
};
class d:public b
{
int c;
public:
void mul(void);
void display(void);
};
void b::get_ab(void)
{
a=5;b=10;
}
int b::get_a()
{
return a;
}
void b::show_a()
{
cout<<"a="<<a<<"\n";
}
void d::mul()
{
c=b*get_a();
}
void d::display()
{
cout<<"a="<<get_a()<<"\n";
cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";
}
void main()
{
d z;

clrscr();
z.get_ab();
z.mul();
z.show_a();
z.display();
z.b=20;
z.mul();
z.display();
getch();
}

OUTPUT:
a=5
a=5
b=10
c=50

a=5
b=20
c=100

OVERLOADING UNARY OPERATOR USING FRIEND FUNTION

PROGRAM
# include<iostream.h>
# include<conio.h>
class space
{
int x,y,z;
public:
void getdata(int a,int b,int c);
void display(void);
friend void operator-(space&s);
};
void space::getdata(int a,int b,int c)
{
x=a; y=b; z=c;
}
void space::display(void)
{
cout<<x<<"\t"<<y<<"\t"<<z<<"\n";
}
void operator-(space&s)
{
s.x=-s.x;
s.y=-s.y;
s.z=-s.z;
}
void main()
{
space s;
clrscr();
s.getdata(10,-20,30);
cout<<"\ts";
s.display();
-s;
s.display();
getch();
}

OUTPUT:
S 10 -20 30
-10 20 -30

OVERLOADING BINARY OPERATOR USING MEMBER


FUNCTION

PROGRAM
# include<iostream.h>
# include<conio.h>
class complex
{
float x,y;
public:
complex(){}
complex(float real,float imag)
{
x=real; y=imag;
}
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<"\t"x<<"+j"<<y<<"\n\t";
}
void main()
{
complex c1,c2,c3;
clrscr();
c1=complex(2.5,3.5);
c2=complex(1.6,2.6);
c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c3=";c3.display();
getch();
}

OUTPUT:
C1=2.5+J3.5
C2=1.6+J2.6
C3=4.1+J6.1

VIRTUAL BASE CLASS

PROGRAM
# include<iostream.h>
# include<conio.h>
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<"Roll No:"<<roll_number<<"\n";
}
};
class test:virtual public student
{
protected:
float mark1,mark2;
public:
void get_marks(float x,float y)
{
mark1=x; mark2=y;
}
void put_marks(void)
{
cout<<"Marks obtained:"<<"\n"
<<"Mark1="<<mark1<<"\n"
<<"Mark2="<<mark2<<"\n";
}
};
class sports:public virtual student
{
protected:
float score;
public:
void get_score(float s)
{ score=s;}
void put_score(void)
{
cout<<"Sports wt"<<score<<"\n\n";
}
};
class result:public test,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=mark1+mark2+score;
put_number();
put_marks();
put_score();
cout<<"Total score:"<<total<<"\n";
}
void main()
{
result student1;
student1.get_number(123);
student1.get_marks(30.5,25.5);
student1.get_score(7.0);
student1.display();
getch();
}

OUTPUT:
Roll no:123
Marks obtained:
Mark1=30.5
Mark2=25.5
Sports wt:7

Total score:63

//virtual base class

# include<iostream.h>
# include<conio.h>
class student
{
protected:
int r_n;
public:
void get_n(int a)
{
r_n=a;
}
void put_n(void)
{
cout<<"roll number"<<r_n<<"\n";
}
};
class test:virtual public student
{
protected:
float p1,p2;
public:
void get_m(float x,float y)
{
p1=x;
p2=y;
}
void put_m(void)
{
cout<<"part1="<<p1<<"\n"<<"part2="<<p2<<"\n";
}
};
class sports:public virtual student
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"sports wt"<<score<<"\n";
}
};
class result:public test,public sports
{
float total;
public :
void display(void);
};
void result::display(void)
{
total=p1+p2+score;
put_n();
put_m();
put_score();

cout<<"total score="<<total<<"\n";
}
void main()
{
result student1;
clrscr();
student1.get_n(123);
student1.get_m(70.0,50.0);
student1.get_score(6.0);
student1.display();
getch();

//sequential i/o operations


# include<iostream.h>
# include<conio.h>
# include<string.h>
# include<fstream.h>
void main()
{
char string[80];
clrscr();
cout<<"Enter a string\n";
cin>>string;
int len=strlen(string);
fstream file;
file.open("TEXT.txt",ios::in|ios::out);
for(int i=0;i<len;i++)
file.put(string[i]);
file.seekg(0);
char ch;
while(file)
{
file.get(ch);
cout<<ch;
}
getch();
}

//i/o operation on binary file


# include<iostream.h>
# include<conio.h>
# include<fstream.h>
# include<iomanip.h>
const char*filename="binary.txt";
void main()
{
float height[4]={175.5,153.0,167.25,160.70};
clrscr();
ofstream outfile;
outfile.open(filename);
outfile.write((char *)&height,sizeof(height));
outfile.close();
for(int i=0;i<4;i++)
height[i]=0;
ifstream infile;
infile.open(filename);
infile.read((char*)&height,sizeof(height));
for(i=0;i<4;i++)
{
cout.setf(ios::showpoint);
cout<<setw(10)<<setprecision(2)<<height[i];
}
infile.close();
getch();
}

# include<iostream.h>
# include<conio.h>
# include<fstream.h>
# include<stdlib.h>
int main(int argc,char*argv[])
{
int number[9]={11,22,33,44,55,66,77,88,99};
clrscr();
if(argc!=2)
{
cout<<"argc="<<argc<<"\n";
cout<<"error in arguments";
}
ofstream fout1,fout2;
fout1.open(argv[0]);
if(fout1.fail())
{
cout<<"{could open file"<<argv[0]<<"\n";
exit(1);
}
fout2.open(argv[1]);
if(fout2.fail())
{
cout<<"could open file"<<argv[0]<<"\n";
exit(1);
}
for(int i=0;i<9;i++)
{
if(number[i]%2==0)
fout2<<number[i]<<" ";
else
fout1<<number[i]<<" ";
}
fout1.close();
fout2.close();
ifstream fin;
char ch;
for(i=0;i<argc;i++)
{
fin.open(argv[i]);
cout<<"contents of"<<argv[i]<<"\n";
do
{
fin.get(ch);
cout<<ch;
}
while(fin);
cout<<"\n\n";
fin.close();
getch();
}
return(0);
}

VIRTUAL FUNCTIONS
CODING:
# include<iostream.h>
class base
{
public:
void display()
{
cout<<"\nDisplay base";
}
virtual void show()
{
cout<<"\nShow base";
};
class derived :: public base
{
public:
void display()
{
cout<<"\nDisplay derived";
}
void show()
{
cout<<"\nShow derived";
}
void main()
base b;
derived d;
base *bptr;
cout<<"\nbptr points to base\n";
bptr=&b;
bptr->display();
bptr->show();
cout<<"\nbptr points to derived\n";
bptr=&d;
bptr->display();
bptr->show();
getch();
}

OUTPUT:
Bptr points to base

Display base
Show base

Bptr points to derived

Display derived
Show derived

ARITHMETIC OPERATION USING SWITCH CASE


CODING:

Class maths
{
public static void main(String args[])
{
int a,b,x,z;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
x=Integer.parseInt(args[2]);
switch(x)
{
case1:
z=a+b;
System.out.println(“sum=”+z);
break;
case2:
z=a-b;
System.out.println(“difference=”+z);
break;
case 3:
z=a*b;
System.out.println(“multiplication=”+z);
break;
case 4:
z=a/b;
System.out.println(“division=”+z);
break;
default:
System.out.println(“enter any number from 1 to 5”);
break;
}
}
}

OUTPUT:
10 5 4
division=2
231
sum=5
532
difference=2
443
multiplication:16

CLASSES AND OBJECTS

CODING:

class rectangle
{
int length,width;
void getdata(int x,int y)
{
length= x;
width= y;
}
int rectarea()
{
int area=length*width;
return(area);
}
}
class ractarea
{
public static void main(String args[])
{
int area1,area2;
rectangle rect1=new rectangle();
rectangle rect2=new rectangle();
rect1.length=15;
rect1.width=10;
area1=rect1.length*rect1.width;
rect2.getdata(20,12);
area2=rect1.rectarea();
System.out.println("area1="+area1);
System.out.println("area2="+area2);
}
}

OUTPUT:
Area1=150
Area2=240

EXCEPTION HANDLING

CODING:

class error1
{
public static void main(String args[])
{

int c[]={5,10};
int b=5;

try
{
int x=c[2]/(b-c[1]);
}

catch(ArithmeticException e)
{
System.out.println("division by 0");
}

catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("arrray index error");
}

catch(ArrayStoreException e)
{
System.out.println("wrong data type");
}

int y=c[1]/c[0];
System.out.println("y="+y);
}
}
OUTPUT:

arrray index error


y=2

FACTORIAL

CODING:

Class factor
{
public static void main(String args[])
{
int f,i,n;
f=1;
n=Integer.parseInt(args[0]);
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.println(“factorial=”+i);
}
}

OUTPUT:
5
factorial=120

IMPLEMENTING INTERFACE
CODING:
class student
{
int rollnum;
void getnumber(int n)
{
rollnum=n;
}
void putnumber()
{
System.out.println("Roll no:"+rollnum);
}
}
class test extends student
{
float part1,part2;
void getmarks(float m1,float m2)
{
part1=m1;
part2=m2;
}
void putmarks()
{
System.out.println("marks obtained");
System.out.println("part1="+part1);
System.out.println("part2="+part2);
}
}
interface sports
{
float sportwt=6.0F;
void putwt();
}
class results extends test implements sports
{
float total;
public void putwt()
{
System.out.println("sports Wt="+sportwt);
}
void display()
{
total=part1+part2+sportwt;
putnumber();
putmarks();
putwt();
System.out.println("Total score="+total);
}
}
class hybrid
{
public static void main(String args[])
{
results student1=new results();
student1.getnumber(1234);
student1.getmarks(27.5F,33.0F);
student1.display();
}
}

OUTPUT:
Roll no:1234
Marks obtained
Part1=27.5
Part2=33
Sportwt =6
Total score=66.5

MULTITHREADING

CODING:

class A extends Thread


{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("Exit from A");
}
}
}

class B extends Thread


{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("\tfrom thread B:j="+j);
}
System.out.println("exit from B");
}
}

class C extends Thread


{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("\tFrom thread C:k="+k);
}
System.out.println("Exit from c");
}
}

class threadtest
{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
}
}

OUTPUT:
From thread A:i=1
From thread A:i=2
From thread B:i=1
From thread B:j=2
From thread C:k=1
From thread C:k=2

ASCENDING AND DESCENDING ORDER


CODING:

Class order
{
public static void main(String args[])
{
int i,j,c,n;
int a[]=new int[25];
n=Integer.parseInt(args[0]);
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(args[i+1]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
c=a[i];
a[j]=a[j+1];
a[j+1]=c;
}
}
}
System.out.println(“ascending order is”);
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println(“descending order is”);
for(i=n;i>=0;i--)
{
System.out.println(a[i]);
}
}
}

OUTPUT:
Ascending order is
5
10
12
25
30
Descending order is
30
25
12
10
5

PACKAGE
CODING:
package pack1;
public class classa
{
public int m=10;
public void displayb()
{
System.out.println("m="+m);
}
}

import pack1.*;
class test
{
public static void main(String args[])
{
classa obj1=new classa();
obj1.displayb();
}
}

OUTPUT:

java
STRING MANIPULATION

CODING:
import java .io.*;
class pmr
{
public static void main(String args[])
{
StringBuffer str=new StringBuffer("Object language");
System.out.println("Original String:"+str);
System.out.println("Length of string:"+str.length());
for(int i=0;i<str.length();i++)
{
int p=i+1;
System.out.println("Character at position:"+p+"is"+str.charAt(i));
}
String aString=new String(str.toString());
int pos=aString.indexOf("language");
str.insert(pos,"oriented");
System.out.println("modified string:"+str);
str.setCharAt(6,'_');
System.out.println("String now:"+str);
str.append("improves security");
System.out.println("appended string:"+str);
}
}
OUTPUT:
Original string:object language
Length of string:15
Character at positon:1 is o
Character at positon:2 is b
Character at positon:3 is j
Character at positon:4 is e
Character at positon:5 is c
Character at positon:6 is t
Character at positon:7 is
Character at positon:8 is l
Character at positon:9 is a
Character at positon:10 is n
Character at positon:11is g
Character at positon:12 is u
Character at positon:13 is a
Character at positon:14 is g
Character at positon:15 is e
Modified string:object oriented language
String now:object oriented language
Appended string:object-oriented language improves security

USER DEFINED EXCEPTION

CODING:

import java.lang.exception;
class myexception extends exception
{
myexception(string message)
{
super(message);
}
}
class testmyexception
{
public static void main(String args[])
{
int x=5;
y=1000;
try
{
float z=(float)x/(float)y;
if(z<0.01)
{
throw new myexception(“number is too small”);
}
}
catch(myexception e)
{
System.out.println(“caught my exception”);
System.out.println(e.getmessage());
}
finally
{
System.out.println(“I am always here”);
}
}
}

OUTPUT:
Caught my exception
Number is too small
I am always here

You might also like