/* C++ Program to calculate the area and perimeter of rectangles using concept of inheritance. */ #includeusing namespace std; class Rectangle { protected: float length, breadth; public: Rectangle(): length(0.0), breadth(0.0) { cout<<"Enter length: "; cin>>length; cout<<"Enter breadth: "; cin>>breadth; } }; /* Area class is derived from base class Rectangle. */ class Area : public Rectangle { public: float calc() { return length*breadth; } }; /* Perimeter class is derived from base class Rectangle. */ class Perimeter : public Rectangle { public: float calc() { return 2*(length+breadth); } }; int main() { cout<<"Enter data for first rectangle to find area.\n"; Area a; cout<<"Area = "<
Showing posts with label CPP. Show all posts
Showing posts with label CPP. Show all posts
Thursday, November 27, 2014
Wednesday, November 12, 2014
Implement array of Objects using C++
Sumit Kar November 12, 2014 C++ Programming, CPP No comments
Write a Program in C++ to implement array of Objects. Take the
user input of Name of player, Age and score. Sort the data based on the highest
Scores.
Program:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class det
{
public:
int age,score;
char name[20];
void input();
void show();
void init();
};
void det::init()
{
age=0;
score=0;
char name1[20]="Blank";
strcpy(name,name1);
}
void det::input()
{
cout<<"Enter the Name: ";
cin>>name;
cout<<"Enter the Age: ";
cin>>age;
cout<<"Enter the score: ";
cin>>score;
}
void det::show()
{
cout<<" Name : "
<<name;
cout<<"\t Age :
" <<age;
cout<<"\t Score : " <<score
<<endl<<endl<<endl;
}
int
main()
{
int i,n=0,j,choice,index,count=0;
char ch;
det a[10],temp;
clrscr();
for(i=0;i<10;i++)
a[i].init();
do
{
cout<<" : : Menu : : ";
cout<<"\n1.Add\n2.Edit\n3.Delete\n4.Sort\n5.Print\n0.Exit"<<endl;
cout<<"Enter Choice: ";
cin>>choice;
switch(choice)
{
case 1:
ch=121;
for(i=n;i<=10;)
{
if(ch==121||ch==89)
{
a[i].input();
count++;
cout<<"Add more?(Y/N) :";
cin>>ch;
i++;
}
else
break;
}
n=count;
break;
case 2:
for(i=0;i<n;i++)
{
cout<<i+1;
a[i].show();
}
cout<<"Enter the Index to be
edited:";
cin>>index;
a[index-1].input();
cout<<"Updated \n\n";
break;
case 3:
for(i=0;i<n;i++)
{
cout<<i+1;
a[i].show();
}
cout<<"Enter the Index to be
deleted:";
cin>>index;
if ( index >= n+1 )
cout<<"Deletion not possible.\n";
else
{
for
( i = index - 1 ; i < n - 1 ; i++ )
a[i] = a[i+1];
n=n-1;
cout<<"Deleted\n\n";
break;
}
case 4:
for (i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if (a[j].score>a[i].score)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"Sorted\n\n"<<endl;
break;
case 5:cout<<endl<<endl;
for(i=0;i<n;i++)
{
a[i].show();
}
break;
case 0:cout<<"Program
Terminated... Press Any key to Exit!";
break;
default : cout<<"Wrong Choice";
}
}while(choice!=0);
getch();
return 1;
}
Print Details using C++
Sumit Kar November 12, 2014 C++ Programming, CPP No comments
Write a Program in C++ to print the details.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class Details
{
public:
void
person(char[],int,int,int);
};
inline void Details :: person(char name[],int
id,int dob,int marks)
{
cout<<"Name : ";
puts(name);
cout<<"ID : "<<id;
cout<<"Date of birth :
"<<dob;
cout<<"Marks : "<<marks;
}
int main()
{
Details ob;
char
name[10];int dob,id,marks;
cout<<"Enter your name: ";
gets(name);
cout<<"Enter your ID: ";
cin>>id;
cout<<"Enter your Date of Birth:
";
cin>>dob;
cout<<"Enter your marks: ";
cin>>marks;
ob.person(name,id,dob,marks);
getch();
return
0;
}
Output:
Enter your name: Sumit
Enter your ID: 55
Enter your Date of Birth: 14031994
Enter your marks: 65
Name : Sumit
ID: 55
Date of birth: 14031994
Marks: 65
Find the Size of a Class and object in C++
Sumit Kar November 12, 2014 C++ Programming, CPP No comments
Write a Program to find the Size of a Class
and object.
#include <iostream.h>
#include <conio.h>
class
SI
{
public:
double p,t,r;
void interest(double,double,double);
};
inline
void SI :: interest(double a, double b, double c)
{
double i,sum;
p=a;
t=b;
r=c;
i=(p*t*r)/100;
sum=p+i;
cout<<"\nThe Interest is: "<<i;
cout<<"\nThe Sum is: "<<sum;
}
int
main()
{
double principal,time,rate;
clrscr();
cout<<"Principal: ";
cin>>principal;
cout<<"Time: ";
cin>>time;
cout<<"Rate: ";
cin>>rate;
SI
ob;
ob.interest(principal,time,rate);
cout<<"\nSize of Class: "<<sizeof(SI);
cout<<"\nSize of Object: "<<sizeof(ob);
getch();
return 0;
}
Output:
Principal: 5000
Time:10
Rate: 10
The Interest is: 5000
The Sum is: 10000
Size of Class: 24
Size of Object: 24
Find Simple Interest using C++
Sumit Kar November 12, 2014 C++ Programming, CPP No comments
Write a Program in C++ to find Simple
Interest.
#include <iostream.h>
#include <conio.h>
class
SI
{
public:
double p,t,r;
void interest(double,double,double);
};
void
SI :: interest(double a, double b, double c)
{
double i,sum;
p=a;
t=b;
r=c;
i=(p*t*r)/100;
sum=p+i;
cout<<"\nThe Interest is: "<<i;
cout<<"\nThe Sum is: "<<sum;
getch();
}
int
main()
{
double principal,time,rate;
clrscr();
cout<<"Principal: ";
cin>>principal;
cout<<"Time: ";
cin>>time;
cout<<"Rate: ";
cin>>rate;
SI
ob;
ob.interest(principal,time,rate);
return 0;
}
Output:
Principal: 5000
Time:10
Rate: 10
The Interest is: 5000
The Sum is: 10000
Print the digits of a number in words using C++
Sumit Kar November 12, 2014 C++ Programming, CPP No comments
Write a Program in
C++ to print the digits of a number in words.
Program:
#include <iostream.h>
#include <conio.h>
class PNIW
{
public:
void
ntow(int);
void
display(int);
};
void PNIW ::
ntow(int n)
{
int
i=0,j=0,temp,sum=0,count=0,ar[10];
temp=n;
while(temp!=0)
{
i=temp%10;
ar[count]=i;
count++;
temp=temp/10;
}
for(i=count-1;i>=0;i--)
{
j=ar[i];
display(j);
}
}
void PNIW ::
display(int i)
{
switch(i)
{
case
0:cout<<"Zero ";
break;
case
1:cout<<"One ";
break;
case
2:cout<<"Two ";
break;
case
3:cout<<"Three ";
break;
case
4:cout<<"Four ";
break;
case
5:cout<<"Five ";
break;
case
6:cout<<"Six ";
break;
case
7:cout<<"Seven ";
break;
case
8:cout<<"Eight ";
break;
case
9:cout<<"Nine ";
break;
default:
cout<<"Error ";
break;
}
}
int main()
{
int num;
clrscr();
cout<<"Enter Number: ";
cin>>num;
PNIW ob;
ob.ntow(num);
getch();
return 0;
}
Output:
Enter Number: 990
Nine Nine Zero
Monday, September 8, 2014
Write a Program in C++ to find the Factorial of a Number
Sumit Kar September 08, 2014 C++ Programming, CPP No comments

/*ITERATIVE*/
//Calling the Header File.
#include<iostream>
//Declaring the main function
int main()
{
//Tells the compiler that we'll be using all the standard C++ library functions
using namespace std;
int num,factorial=1;
//Ask for the number.
cout<<"Enter a number to calculate it's factorial"<<endl;
cin>>num;
for(int i=1;i<=num;i++)
{
factorial=factorial*i;
}
cout<<"Factorial of "<<num<<"="<<factorial<<endl;
cin.get();
return 0;
}
/*RECURSION */
#include<iostream>
using namespace std;
int fact(int);
int main(){
int num,f;
cout<<"\nEnter a number: ";
cin>>num;
f=fact(num);
cout<<"\nFactorial of "<<num<<" is: "<<f;
return 0;
}
//recursive function
int fact(int n){
if(n==1)
return 1;
else
return(n*fact(n-1));
}
/*
Summary: Factorial is represented using '!', so five factorial will be written as (5!),n factorial as (n!).Also
n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1.
*/