Showing posts with label Programs For C. Show all posts
Showing posts with label Programs For C. Show all posts

An example to illustrate Dynamic memory allocation and deallocation



#include <iostream.h>
#include <conio.h>
void main( )
{
int *ptr;
clrscr();
ptr = new int;
*ptr = 15;
cout<<”Value at pointer = “<< *ptr<<”\n”;
delete ptr;
getch();
}
Output :
Value at pointer = 15

An example to illustrate Dynamic memory allocation and deallocation

#include <iostream.h>
#include <conio.h>
void main( )
{
int *ptr;
int i,n;
clrscr();
cout<<”Enter how many elements you want to enter\n”;
cin>>n;
ptr = new int[n];
cout<<”Enter”<<n<<” elements\n”;
for(i=0; i<n;i++)
cin>> ptr[i];
cout<<”You have entered \n”;
for(i=0; i<n;i++)
cout<< ptr[i]<<”\n”;
delete ptr;
getch();
}
Output :
Enter how many elements you want to enter
5
Enter 5 elements
1
2
3
4
5
You have entered :
1
2
3
4
5

An example to illustrate Pointer

#include <iostream.h> #include <conio.h>
void main( )
{
int x=3;
int *ptr;
clrscr();
ptr = &x;
cout<<”Address of x = “<< &x<<”\n”;
cout<<”Address in ptr = “<< ptr<<”\n”;
cout<<”Address of pointer = “<< &ptr<<”\n”
cout<<”Value in x = “<< x<<”\n”;
cout<<”Value at address of x = “<< *(&x)<<”\n”;
cout<<”Value at pointer = “<< *ptr<<”\n”;
getch();
}
Output :
Address of x = 64482
Address in ptr = 64482
Address of pointer = 64484
Value in x = 3
Value at address of x = 3
Value at pointer = 3

Array of Objects

#include <iostream.h>
#include <conio.h>
class Book
{
private :
int idno;
float price;
public :
void readData( )
{
cin>>idno;
cin>>price;
}
void printData( )
{
cout<<”Id no =”<<idno<<”\n”;
cout<<”Price =”<<price<<”\n”;
}
};
void main( )
{
Book books[5];
cout<<”Enter id no and price for 5 books\n”;
for(int i =0; i<5; i++)
books[i]. readData( );
cout<<”You have entered :\n”;
for(i=0 ; i<5; i++)
books[i]. printData( );
}
Output :
Enter id no and price for 5 books
112 150
224 345
342 120
445 450
566 230
You have entered :
Id no = 112 Price = 150
Id no = 224 Price = 345
Id no = 342 Price = 120
Id no = 445 Price = 450
Id no = 566 Price = 230

Class and Object : Passing and returning Objects


#include <iostream.h>
#include <conio.h>
class Complex
{
private :
float x;
float y;
public :
void getData(float a, float b)
{
x = a;
y = b;
}
void showData( )
{
Cout<<x<<y<<”\n”;
}
Complex add(Complex c2)
{
Complex c3;
c3.x = x + c2.x;
c3.y = y + c2.y;
return( c3);
}
};
void main( )
{
Complex p1, p2, p3;
p1.getData(10,20);
p2.getData(30,40);
p3 = p1.add(p2);
p3.showData( );
}
Output :
40 60

Class and Object

#include <iostream.h>
#include <conio.h>
class Data
{
private :
int num;
public :
void getNumber(int n)
{
num = n;
}
void showNumber( )
{
Cout<<”Num = “<<num<<”\n”;
}
};
void main( )
{
Data d1;
d1.getNumber(10);
d1.showNumber( );
}
Output :
Num = 10

Example : Recursion


#include <iostream.h>
#include <conio.h>
void fun( int ); // prototype declaration
void main( )
{
int n;
clrscr( );
n = 5;
fun(n);
getch();
}
void fun( int k)
{
if ( k > 0)
{
cout<<k<<”\n”;
fun(k-1);
cout<<k<<”\n”;
}
}
Output :
5
4
3
2
1
1
2
3
4
5

Example : Using function with arguments

#include <iostream.h>
#include <conio.h>
int add(int, int ); // prototype declaration
void main( )
{
int a,b,c;
clrscr( );
cout<<”Enter two numbers\n”;
cin>>a>>b;
c=add(a,b);
cout<<”Ans = “<<c<<”\n”;
getch();
}
int add(int x, int y )
{
int z;
z = x + y;
return(z);
}
Output :
Enter two numbers
10 20
Ans = 30

Example : Using function

#include <iostream.h>
#include <conio.h>
void fun( ); // prototype declaration
void main( )
{
clrscr( );
cout<<”We are in main\n”;
fun( ); // function call
cout<<”Back in main\n”;
getch();
}
void fun( ) // function definition
{
cout<<”We are in function\n”;
}
Output :
We are in main
We are in function
Back in main

Example : Using switch-case

#include <iostream.h>
#include <conio.h>
//Read a number and display weekday accordingly
void main( )
{
int day;
clrscr();
cout<<”Enter a number between 1..7\n”;
cin>>day;
switch (day)
{
case 1 : cout<<”Monday\n”;
break;
case 2 : cout<<”Tuesday\n”;
break;
case 3 : cout<<”Wednesday\n”;
break;
case 4 : cout<<”Thursday\n”;
break;
case 5 : cout<<”Friday\n”;
break;
case 6 : cout<<”Saturday\n”;
break;
case 7 : cout<<”Saturday\n”;
break;
default : cout<<”Invalid input\n”;
}
getch();
}
Output :
Enter a number between 1..7
5
Friday

Exception handling

#include <iostream.h>
#include <conio.h>
void main( )
{
int num,denum,ans;
clrscr();
cout<<”Enter two numbers\n”;
cin>>num>>denum;
try
{
If ( denum ==0)
throw(denum);
ans = num/denum;
cout<<”Ans =”<<ans<<”\n”;
}
catch(int x)
{
cout<<”Division by zero is not allowed\n”;
}
Output :
Enter two numbers
10 0
Division by zero is not allowed

Inheritance : Hierarchical

#include <iostream.h>
#include <string.h>
#include <conio.h>
class Person
{
protected :
char name[30];
int age;
public :
void getPersonData( char *n, int a)
{
strcpy(name,n);
age = a;
}
};
class Manager : public Person
{
protected :
char department[30];
public :
void getDepartment( char * dpt)
{
Strcpy(department, dpt)
}
void showDetails( )
{
cout<<”Manager details :\n”;
cout<<”Name :”<<name<<”\n”;
cout<<”Age :”<<age<<”\n”;
cout<<”Department :”<<department<<”\n”;
}
};
class Scientist : public Person
{
private :
int publications;
public :
void getPublications( int pub)
{
publications = pub;
}
void showDetails( )
{
cout<<”Scientist details :\n”;
cout<<”Name :”<<name<<”\n”;
cout<<”Age :”<<age<<”\n”;
cout<<”Publications :”<<publications<<”\n”; }
};
void main( )
{
Manager m1;
Scientist s1;
m1.getPersonData(“Kumar”, 36);
m1.getDepartment(“Sales”);
s1.getPersonData(“Krishna”, 42);
s1.getPublications(23);
m1.showDetails( );
s1.showDetails( );
}
Output :
Manager details :
Name : Kumar
Age : 36
Department : Sales
Scientist details :
Name : Krishna
Age : 42
Publications : 23

Inheritance : Multiple

#include <iostream.h>
#include <string.h>
#include <conio.h>
class Person
{
protected :
char name[30];
int age;
public :
void getPersonData( char *n, int a)
{
strcpy(name,n);
age = a;
}
};
class Typist
{
protected :
int speed;
public :
void getTypingSpeed( int s)
{
speed = s;
}
};
class Staff : public Person, public Typist
{
private :
char department[30];
public :
void getDepartment( char * dpt)
{
Strcpy(department, dpt)
}
void showDetails( )
{
cout<<”Name :”<<name<<”\n”;
cout<<”Age :”<<age<<”\n”;
cout<<”Speed :”<<speed<<”\n”;
cout<<”Department :”<<department<<”\n”;
}
};
void main( )
{
Staff myStaff;
myStaff.getPersonData(“Ramesh”, 32);
myStaff.getSpeed(60);
myStaff.getDepartment(“Library”);
myStaff.showDetails( );
}
Output :
Name : Ramesh
Age : 32
Speed : 60
Department : Library

Inheritance : Single level

#include <iostream.h>
#include <conio.h>
class Data
{
protected :
int num1;
int num2;
public :
void getData( int x, int y)
{
num1 = x;
num2 = y;
}
};
class Test : public Data
{
private :
int sum;
public :
void display( )
{
sum = num1 + num2;
cout<<”Num1 =”<<num1<<” Num2 =”<<num2<<”\n”;
cout<<”Sum =”<< sum<<”|n”;
}
};
void main( )
{
Test t1;
t1.getData(10,20 );
t1.display( );
}
Output :
Num1 = 10 Num2 = 20
Sum = 30

Initializing the Class : Constructor

#include <iostream.h>
#include <conio.h>
class Time
{
private :
int hour;
int min;
public :
Time ( )
{
hour = min = 0;
}
Time ( int h, int m)
{
hour = h;
min = m;
}
void showTime()
{
cout<<hour<<” : “<<min<<”\n”;
}
};
void main( )
{
Time t1;
t1.showTime( );
Time t2(11,30);
t2.showTime();
}
Output :
0 : 0
11 : 30

Operator overloading (Binary operator)

#include <iostream.h>
#include <conio.h>
class Complex
{
private :
float x;
float y;
public :
void getData(float a, float b)
{
x = a;
y = b;
}
void showData( )
{
Cout<<x<<y<<”\n”;
}
Complex operator + (Complex c2)
{
Complex c3;
c3.x = x + c2.x;
c3.y = y + c2.y;
return( c3);
}
};
void main( )
{
Complex p1, p2, p3;
p1.getData(10,20);
p2.getData(30,40);
p3 = p1 + p2;
p3.showData( );
}
Output :
40 60

Operator Overloading (Unary operator)

#include <iostream.h>
#include <conio.h>
class Point
{
private :
int x;
int y;
public :
Point( )
{
x=y=0;
}
Point(int a, int b)
{
x = a;
y = b;
}
void show( )
{
cout<<”x =”<<x<<”y=”<<y<<”\n”;
}
void operator ++ ( )
{
x++;
y++;
}
void operator -- ( )
{
x--;
y--;
}
};
void main( )
{
Point p1(12,15);
p1.show( );
p1++;
p1++;
p1.show( );
p1--;
p1.show( );
}
Output :
x = 12 y = 15
x = 13 y = 16
x = 14 y = 17
x = 13 y = 16

Simple example : Decision making

#include <iostream.h>
#include <conio.h>
void main( )
{
int num1, num2;
clrscr();
cout<<”Enter two numbers\n”;
cin>>num1>>num2;
if(num1 > num2 )
cout<<”Largest =”<< num1<<”\n”;
else
cout<<”Largest =”<< num2<<”\n”;
getch();
}
Output :
Enter two numbers
10 20
Largest = 20

Simple example for C 2

Simple example

#include <iostream.h>
#include <conio.h>
void main( )
{
int num;
clrscr();
cout<<”Enter an integer number \n”;
cin>>num;
cout<<”You have entered :”<<num<<”\n”;
getch();
}
Output :
Enter an integer number
10
You have entered : 10

Simple example For C

Simple example in C
#include <iostream.h>
#include <conio.h>
void main( )
{
clrscr();
cout<<”Hello World \n”;
getch();
}
Output :
Hello World