Major C Programs
Major C Programs
Major C Programs
void display()
{ static int a=1;
cout << "\nA : " << a;
a++; }
Output :
A:1
A:2
A:3
//example of external or global storage class
#include<iostream.h>
void display();
int a=10; //global variable
void main()
{ cout << "\nA : " << a;
increment();
display();
cout << "\nA : " << a; }
void increment()
{ a = 20; }
void display()
{ cout << "\nA : " << a; }
Output :
A : 10
A : 20
A : 20
//Use of macros
#include<iostream.h>
#include<conio.h>
#define mul(a,b) a*b //macro definition
#define div(a,b) a/b
void main()
{
int a=15,b=3;
cout<<endl<<"Value a:"<<a<<endl;
cout<<"Value b:"<<b<<endl;
cout<<endl<<"a*b:"<<mul(a,b);
cout<<endl<<"a/b:"<<div(a,b);
getch();}
//Default arguments
#include<iostream.h>
#include<conio.h>
float div(int a,int b=2)
{return a/b;}
void main()
{cout<<endl<<"20/5:"<<div(20,5);
cout<<endl<<"Divide 30 by default value:"<<div(30);
getch();
}
//structure to a function 2
#include<iostream.h>
#include<conio.h>
struct student{ char name[20];
int clas;
float height;
}s[2];
void display(struct student s[])
{for(int i=0;i<2;i++){
cout<<endl<<s[i].name<<endl<<s[i].clas<<endl<<s[i].height;}}
void main()
{for(int i=0;i<2;i++){
cout<<"Enter Name:";cin>>s[i].name;
cout<<"Enter Height:";cin>>s[i].height;
cout<<"Enter Class:";cin>>s[i].clas;}
display(s);
getch();}
//Pointer Arithmetic
#include<iostream.h>
#include<conio.h>
void main()
{
int a[]={1,2,3,4,5};
int *p;
p=a;
for(int i=0;i<5;i++)
cout<<endl<<*(p+i);
getch();
}
//return by reference
#include<iostream.h>
#include<conio.h>
int& max(int &a,int &b)
{if(a>b)
return a;
else return b;}
void main()
{int x=5,y=8;
max(x,y)=0;
cout<<endl<<"x:"<<x<<endl<<"y:"<<y<<endl;
getch();
}
//return by pointer
#include<iostream.h>
#include<conio.h>
int* max(int *a,int *b)
{if(*a>*b)
return a;
else return b;}
void main()
{int x=5,y=8;
cout<<endl<<"The maximum no. is "<<*max(&x,&y);
getch();
}
//passing by pointer
#include<iostream.h>
#include<conio.h>
void square(int num,int *sq)
{*sq=num*num;}
void main(){
int num=5,sq;
square(num,&sq);
cout<<"The square is "<<sq;
getch();
}
//Passing by Reference
#include<iostream.h>
#include<conio.h>
void square(int num,int &sq)
{sq=num*num;}
void main(){
int num=5,sq;
square(num,sq);
cout<<"The square is "<<sq;
getch();
}
//Using namespaces
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
namespace digit
{int count=0;};
namespace character
{int count=0;};
namespace other
{int count=0;};
void main()
{
char str[]="1.Nepal 2.India 3.China";
int i=0;
while(str[i]!='\0')
{ if(isdigit(str[i]))
digit::count++;
else if(isalpha(str[i]))
character::count++;
else
other::count++;
i++;
}
cout<<"\nNumber of Digits="<<digit::count;
cout<<"\nNumber of Characters="<<character::count;
cout<<"\nNumber of other Characters="<<other::count;
getch();
}
//using manipulators
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
void main()
{ int l;
char nam[5][25]={"Ravi Thapa","Arun Rai","Binita Gurung","Pabitra Sharma","Janak KC"};
char add[5][25]={"Butwal","Kathmandu","Pokhara","Chitwan","Dang"};
cout<<"\nName"<<setw(46)<<"Address\n";
cout<<"\n--------------------------------------------------\n";
for(int i=0;i<5;i++)
{l=strlen(nam[i]);
cout<<endl<<nam[i]<<setw(50-l)<<add[i];}
getch();
}
//Type casting
#include<iostream.h>
#include<conio.h>
void main()
{
int a=5,b=2;float z,c=5;
z=a/b;
cout<<a/2<<endl;
cout<<z<<endl;
z=(float)a/b;
cout<<z<<endl;
z=c/b;
cout<<z;
getch();}
output
2
2
2.5
2.5
Unit 3: Classes & Objects
//dynamic constructors
#include <iostream.h>
#include <conio.h>
class Account
{
private:
int account_no;
int balance;
public :
Account(int a,int b)
{
account_no=a;
balance=b;
}
void display()
{
cout<< "\nAccount number is : "<<account_no;
cout<< "\nBalance is : " <<balance;
}
};
void main()
{
clrscr();
int an,bal;
cout<< "Enter account no : ";
cin>>an;
cout<< "\nEnter balance : ";
cin>>bal;
Account *acc=new Account(an,bal); //dynamic constructor
acc->display(); //'->' operator is used to access the method
getch();
}
//Overloading Constructors
#include<iostream.h>
#include<conio.h>
class count{
private:
int a,b;
public:
count()//default constructor
{a=0;b=0;}
count(int x,int y)//parameterized constructor
{a=x;b=y;}
count(int &x)//copy constructor
{}
void display()
{cout<<endl<<"A="<<a<<endl;
cout<<"B="<<b<<endl;} };
void main()
{count c;
c.display();
count d(5,6);
d.display();
count &e=d;
e.display();
count f=e;//default copy constructor
f.display();
getch(); }
//A program to take name and roll no using function and display the data.
#include<iostream.h>
#include<string.h>
#include<conio.h>
class student{
private:
char nam[20];int sid;
public:
int getdata(char n[],int id)
{strcpy(nam,n);
sid=id;return 0;}
int display()
{
cout<<"\nName:"<<nam<<endl<<"\nSID="<<sid;return 0;
}
};
int main()
{
student s1;
s1.getdata("Ravi",45); clrscr();
s1.display();
getch();
return 0;
}
//A program to use parameterized constructor for name and roll no.
#include<iostream.h>
#include<string.h>
#include<conio.h>
//using namespace std;
class student{
private:
char nam[20];int sid;
public:
student(char n[],int id)
{strcpy(nam,n);
sid=id;}
int display()
{
cout<<"\nName:"<<nam<<endl<<"\nSID="<<sid;return 0;
}
};
int main()
{
student s1("Ravi",45);
clrscr();
s1.display();
getch();
return 0;
}
//A program to use parameterized constructor for name and roll no.
#include<iostream.h>
#include<conio.h>
class student
{ char *nam;int age;
public:
student(char *n,int a)
{nam=n;
age=a;}
void display()
{cout<<"Name:"<<nam<<endl;
cout<<"Age:"<<age<<endl;}};
void main()
{student fs("Steven",21);
fs.display();
getch();
}
//Example of Type conversion from Basic data type to user defined data type
#include<iostream.h>
#include<conio.h>
class distance
{int feet,inch;
public:
distance(float m)
{feet=int(m);
inch=12*(m-feet);
}
void display()
{cout<<"Feet="<<feet<<endl<<"Inch="<<inch;}
};
void main()
{
distance d(2.5);
d.display();
getch();
}
Output:
Feet=2
Inch=6
//Example of Type conversion from user defined data type to basic type
#include<iostream.h>
#include<conio.h>
class distance
{int feet,inch;
public:
distance(int f,int i)
{feet=f;inch=i;}
float convert()
{float a=feet+inch/12.0;
return a;}};
void main()
{distance d(5,6);
cout<<"\nThe distance is "<<d.convert();
getch();}
//Example of Type conversion from user defined data type to basic type using operator
function
#include<iostream.h>
#include<conio.h>
class distance
{int feet,inch;
public:
distance(int f,int i)
{feet=f;
inch=i;}
operator float()
{float a=feet+inch/12.0;
return a;}};
void main()
{distance d(5,6);float x=(float)d;
cout<<"\nThe distance is "<<d;
getch();}
Output:
The distance is 5.5
//Type conversion from object to object or userdefined data type to userdefined type
#include<iostream.h>
#include<conio.h>
class distance
{int feet,inch;
public:
distance()
{feet=0;
inch=0;}
distance(int f,int i)
{feet=f;
inch=i;}
void display()
{cout<<endl<<feet<<"ft "<<inch<<"inch"<<endl;}};
class dist{int meter, centi;
public:
dist(int m,int c)
{meter=m;centi=c;}
operator distance()//operator function
{distance d;int f,i;
f=meter*3.3;
i=centi*0.4;
f=f+i/12;
i=i%12;
return distance(f,i);}};
int main()
{dist d2(4,50);
distance d1;
d1=d2;
d1.display();
getch();}
Unit 5: Inheritance
Inheritance
//Example of single inheritance
#include<iostream.h>
#include<conio.h>
class student
{ char *nam;int age;
public:
student(char *n,int a)
{nam=n;
age=a;}
void display()
{cout<<"Name:"<<nam<<endl;
cout<<"Age:"<<age<<endl;}};
class fstudent:public student
{char *country;
public:
class fstudent(char *n,int a, char *c):student(n,a)
{country=c;}
void displayf()
{display();
cout<<"Country:"<<country<<endl;}
};
void main()
{fstudent fs("Steven",21,"UK");
fs.displayf();
getch();
}
//Hybrid Inheritance
//Virtual Base Class
#include<iostream.h>
#include<conio.h>
class ClassA
{ public: int a; };
class ClassB : virtual public ClassA
{ public: int b; };
class ClassC : virtual public ClassA
{ public: int c; };
class ClassD : public ClassB, public ClassC
{ public: int d; };
void main()
{ ClassD obj;
obj.a = 10; //Statement 1
obj.a = 100; //Statement 2
obj.b = 20; obj.c = 30;
obj.d = 40;
cout<< "\n A : "<< obj.a; cout<< "\n B : "<< obj.b;
cout<< "\n C : "<< obj.c;
cout<< "\n D : "<< obj.d; }
//Static Function
#include <iostream.h>
#include <conio.h>
class Demo
{private:
//static data members
static int X;
static int Y;
public:
//static member function
static void Print()
{cout <<"Value of X: " << X << endl;
cout <<"Value of Y: " << Y << endl;
}};
int Demo::X=10;
int Demo::Y=20;
//static data members initializations
void main()
{Demo OB;
//accessing class name with object name
cout<<"Printing through object name:"<<endl;
OB.Print();
//accessing class name with class name
cout<<"Printing through class name:"<<endl;
Demo::Print();
getch();}
Use of 'this' when local variables and class data members are same (while defining a
member function)
#include <iostream.h>
#include <conio.h>
class Number
{ private:
int a;
public:
void get_a(int a)
{this->a=a;}
void put_a() {cout<<"a= "<<a<<endl;}};
int main()
{ Number N; N.get_a(36);
N.put_a();getch();
return 0;}
void main()
{
int a,b;
float x,y;
cout<<"Enter two integer data: ";
cin>>a>>b;
cout<<"Enter two float data: ";
cin>>x>>y;
sum(a,b); // adding two integer type data
sum(x,y); // adding two float type data
sum(a,x); // adding a float and integer type data
getch(); }
Exception Handling
Simple program for exception handling using try, throw and catch
#include<iostream>
using namespace std;
int main()
{ try {
throw 6; }
catch (int a) {
cout << "An exception occurred!" << endl;
cout << "Exception number is: " << a << endl;
}}
Below program contains single catch statement to handle errors.
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{int a=10,b=0,c;
try //try block activates exception handling
{if(b==0)
throw "Division by zero not possible";//throws exception
c=a/b;}
catch(char* ex)//catches exception
{cout<<ex;
}
getch();
return 0;
}
Unit 8: File handling (6 Hrs.)
Reading the contents of a file using getline() method and detecting the end of file using
EOF() member function.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char str[100];
ifstream fin;
fin.open("abc.txt");
while(!fin.eof())
{fin.getline(str,99);
cout<<str;
}
fin.close();
getch();
}
Reading the contents of a file using getline() method and detecting the end of file using
filestream object.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char str[100];
ifstream fin;
fin.open("abc.txt");
while(fin)
{fin.getline(str,99);
cout<<str;
}
fin.close();
getch();
}
Sample Project for Students’ Information System
A project on Students’ Information system which is menu base and has the features to add new
records of students, find the individual record, update the record and delete the record.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class student{
int roll;
char name[30];
char add[60];
public:
void input()
{
cout<<"\nEnter Roll:";
cin>>roll; cin.sync();
cout<<"\nStudent Name:";
cin.getline(name,30);
cout<<"\nEnter Address:";
cin.getline(add,60);}
void display()
{cout<<"\nRoll:"<<roll;
cout<<"\nName:"<<name;
cout<<"\nAddress:"<<add;
}
int getrno()
{return roll;}
};
void updaterecord()
{student s;fstream fin;int key;int found=0,pos;
fin.open("student.dat",ios::binary|ios::in|ios::out);
fin.read((char *)&s,sizeof(student));
cout<<"Enter the Roll No. to search record for update:";cin>>key;
while(fin)
{ if(key==s.getrno())
{cout<<"\nExisting Record:\n";s.display();found=1;pos=fin.tellg()-sizeof(s);cout<<"\nPos:"<<pos;
fin.seekp(pos,ios::beg);
cout<<"\nEdit the record:\n";
s.input();
fin.write((char *)&s,sizeof(student));
}
fin.read((char *)&s,sizeof(student));
}if(found==1)
cout<<"\nSearch Successful....\n";
else
cout<<"\nRecord not found...\n";
fin.close();
}
void delrecord()
{fstream file("student.dat", ios::in|ios::binary);int r;
fstream newfile("newstu.dat",ios::out|ios::binary);
student s;
cout<<"\n enter the rollno no of student whose record to be deleted";
cin>>r;
file.read((char *)&s,sizeof(s));
while(file)
{if (r!=s.getrno())
{newfile.write((char *)&s,sizeof(s));}
file.read((char *)&s,sizeof(s));}
file.close();
newfile.close();
fstream file2("student.dat", ios::out|ios::binary);
fstream newfile2("newstu.dat",ios::in|ios::binary);
newfile2.read((char *)&s,sizeof(s));
while(newfile2)
{file2.write((char *)&s,sizeof(s));
newfile2.read((char *)&s,sizeof(s));}
file2.close();
newfile2.close();
}
void findrecord()
{student s;ifstream fin;int key;int found=0;
fin.open("student.dat",ios::binary|ios::in);
fin.read((char *)&s,sizeof(student));
cout<<"Enter the Roll No. to search:";cin>>key;
while(fin)
{ if(key==s.getrno())
{s.display();found=1;}
fin.read((char *)&s,sizeof(student));
}if(found==1)
cout<<"\nSearch Successful....\n";
else
cout<<"\nRecord not found...\n";
fin.close();
}
void main()
{int ch;clrscr();
do{
cout<<"\n***Menu***\n1.Input Record\n2.Display Record\n3.Find Record\n4.Update
Record\n5.Delete Record\n";
cout<<"Enter Your Choice:";cin>>ch;
switch(ch){
case 1:
{student s;s.input();
ofstream fout;
fout.open("student.dat",ios::app);
fout.write((char *)&s,sizeof(student));
fout.close();break; }
case 2:
{student s;ifstream fin;
fin.open("student.dat",ios::binary|ios::in);
fin.read((char *)&s,sizeof(student));
while(fin)
{ s.display();
fin.read((char *)&s,sizeof(student));
}
fin.close();break;
}
case 3: findrecord();break;
case 4: updaterecord();break;
case 5: delrecord();break;
}}
while(ch<6);
}