Anda di halaman 1dari 30

Program-1

Aim: Program to enter personal details name,surname


,total marks ,gender(M/F),result(Pass/Fail)
Theory: here we will use classes for the all the work we
need to do.
Source Code:
//Program to enter personal details name,surname ,total marks
,gender(M/F),result(Pass/Fail)
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<conio.h>
using namespace std;
class student
{
private:
char name[50];
char surname[50];
int totalmarks;
char gender;
char result1[7];
public:
void accept()
{
cout<<"\n Enter the name of the student";
gets(name);
cout<<"\n Enter the surname";
gets(surname);
cout<<"\n Enter the total marks of 5 subjects out of 500 ";
cin>>totalmarks;
cout<<"\n Enter the gender of the student(M/F) ";
cin>>gender;
}
char*result()
{
if(totalmarks>225)
{
strcpy(result1,"PASSED");
}
else
{
strcpy(result1,"FAILED");
}
return result1;
}
void display()
{
char*p;
p=result();
cout<<"\n
======================================RESULT=======================
=================";
cout<<"\n\n\n\n\n\n \t\t NAME OF THE STUDENT: ";
puts(name);
puts(surname);
cout<<"\n GENDER OF THE STUDENT: "<<gender;
cout<<"\n TOTAL MARKS OF THE STUDENT: "<<totalmarks;
cout<<"\n THE STUDENT HAS ";
for(int i=0;i<7;i++)
{
cout<<*p++;
}
cout<<" THE EXAMINATION";
}
};
int main()
{
student s;
s.accept();
s.display();
return 0;
}

Output:

Conclusion: Here we have used the classes for entering details of a


student and displaying them.
Program-2
Aim: Program to find the area of the rectangle with the
help of inheritance
Theory: here we will use the concept of inheritance to
make a class for a polygon and inherit it into the
rectangle class .
Source Code:
#include<iostream>
using namespace std;
class shape
{
protected:
int width;
int height;
public:
void setwidth(int w)
{
width=w;
}
void setheight(int h)
{
height=h;
}
};
class rectangle:public shape
{
public:
void area()
{
int area;
area=width*height;
cout<<"\n the area of the rectangle is "<<area;
}

};
int main()
{ int w,h;
rectangle s1;
cout<<"\nEnter the width of the rectangle ";
cin>>w;
cout<<"\n Enter the height of the rectangle ";
cin>>h;
s1.setwidth(w);
s1.setheight(h);
s1.area();
return 0;
}

Output:

Conclusion: Here we used inheritance for finding the area of the


rectangle .
Program-3:
Aim: Program to accept employee details and sort it
according to the employee number.
Theory: Here will accept the data using class member
functions and sort them using sort function included in
algorithm header file.
Source Code:
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
class Employee
{
private:
char name[50];
int empno;
public:
void accept()
{
cout<<"\n Enter the employee name";
cin>>name;
cout<<"\n Enter the employee number";
cin>>empno;
}
void display()
{
cout<<"\n Employee name:";
puts(name);
cout<<"\n Employee number:"<<empno;
}
int getnum()
{

return empno;
}
};
bool cmp(Employee e1,Employee e2)
{
e1.getnum()>e2.getnum();
}
int main()
{
Employee e[5];
int i;
for(i=0;i<5;i++)
{
e[i].accept();
}
for(i=0;i<5;i++)
{
e[i].display();
}
sort(e,e+5,cmp);
for(i=0;i<5;i++)
{
e[i].display();
}
return 0;
}

Output:
Conclusion: Here we learnt how to use the sort function and Boolean
function.
Program-4
Aim: Program for banking system in which a user can
deposit withdraw and calculate the compound interest
in the account.
Theory: Here we will use class to declare the balance
and the rate of interest and use constructor to initialize
their values respectively and use member function to
perform the various operation given to us and display
them using menu using the do while loop.
Source Code:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
class bank
{
private:
int balance;
float rate;
public:
bank(int b,float r)
{
rate = r;
balance = b;
}
void deposit()
{ int d;
cout<<"\n Enter the amount to be deposited : ";
cin>>d;
balance+=d;
}
void withdraw()
{ int c;
cout<<"\n Enter the amount to be withdrawn : ";
cin>>c;
if(balance>c)
{
cout<<"\n Amount withdrawn successfully";
balance=balance-c;
}
else
{
cout<<"\n Amount is not possible to be withdrawn because it exceeds your
current account balance";
}
}
void compound()
{ int a,t;
rate=(rate/100);
cout<<"\n Enter the time till which you want the compound interest to be applied :
";
cin>>t;
a=balance*pow(1+rate,t);
cout<<"\n the compound interest is : "<<a;
balance=a;

}
int getbalance()
{
return balance;
}
void menu()
{
char ch;
int ch1;
do
{

cout<<"\n============================================================
Menu ============================================================ ";
cout<<"\n\n\n\n\n\n 1. Deposit amount ";
cout<<"\n 2. Withdraw amount";
cout<<"\n 3. Calculate the compound interest ";
cout<<"\n 4. Check balance";
cout<<"\n 5. Exit";
cout<<"\n Enter the choice ";
cin>>ch;
switch(ch)
{
case '1':
deposit();
break;
case '2':
withdraw();
break;
case '3':
compound();
break;
case '4':
{
int h;
h=getbalance();
cout<<"\n Balance remaining in your account is : "<<h;
break;
}

case '5':
cout<<"\n Thanks for visiting the bank";
exit(1);
break;
}
cout<<"\n Want to enter more 1/2 (1:Yes,2:No)";
cin>>ch1;
cout<<ch1;
}while(ch1!=2);
}
};
int main()
{ int bal;
float r;
cout<<"\n Enter the balance : ";
cin>>bal;
cout<<"\n Enter the rate of interest : ";
cin>>r;
bank b(bal,r);
b.menu();
return 0;
}
Output:

Conclusion: Here we learned how to use constructor and destructor


and switch case as well to perform the operations which are required
to be performed.
Program-5:
Aim: Program to accept 2 numbers and swap it by call
by value or call by reference method and the method
depends on the user.
Theory: Here we’ll us the switch case for asking the user which
method is wanted by the user. And we’ll use pointers for call by
reference method instead of the regular numbers.
Source Code:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
void swap_value(int a,int b)
{
int temp;
cout<<"\n Values before swapping "<<a<<"\n"<<b;
temp=a;
a=b;
b=temp;
printf("\n Values after swapping %d\n%d",a,b);
}
void swap_reference(int*p,int*q)
{
int*t;
cout<<"\n Values before swapping"<<*p<<"\n"<<*q;
t=p;
p=q;
q=t;
cout<<"\n Values after swapping"<<*p<<"\n"<<*q;

}
int main()
{
int a,b;
char ch;
int ch1;
do
{

cout<<"\n============================================MENU====================
=====================";
cout<<"\n1. Swapping by value";
cout<<"\n2. Swapping by reference";
cout<<"\n3. Exit";
cout<<"\n Enter the choice";
cin>>ch;
switch(ch)
{
case '1':
{
cout<<"\n Enter the 2 numbers which are to be swapped";
cin>>a>>b;
swap_value(a,b);
}
break;
case '2':
{
cout<<"\n Enter the 2 numbers which are to be swapped";
cin>>a>>b;
swap_reference(&a,&b);
}
break;
case '3':
cout<<"\n Execution complete now exiting the program";
exit(1);
break;

}
cout<<"\n Want to enter more 1/2 1:Yes 2:No";
cin>>ch1;

}while(ch1!=2);
return 0;
}

Output:

Conclusion: Here we learnt how to use pointers for call by reference


method.
Program-6
Aim: Program to accept 5 different numbers by creating
classes called frienfunc1 and friendfunc2 taking 2 and 3
arguments respectively and calculate the average of
these arguments by passing the object to the friend
function.
Theory: Here we’ll use friend function for accessing the private
members of both the classes.
Source Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
class friendfunc1
{
private:
int a,b;
public:
void getd1()
{
cout<<"\n Enter two numbers";
cin>>a>>b;
}
friend int average(friendfunc1,class friendfunc2);
};
class friendfunc2
{
private:
int p,q,r;
public:
void getd2()
{
cout<<"\n Enter three numbers";
cin>>p>>q>>r;
}
friend int average(friendfunc1,friendfunc2);

};
int average(friendfunc1 x,friendfunc2 y)
{

return ((x.a+x.b+y.p+y.q+y.r)/5);
}
int main()
{
system("COLOR F4");
int avg;
friendfunc1 x;
friendfunc2 y;
x.getd1();
y.getd2();
avg=average(x,y);
cout<<"\n the average of 5 numbers is "<<avg;
return 0;
}
Output:

Conclusion: Here we learnt how to use the friend function to access


the private members of the class through the object the class.
Program-7
Aim: Program to perform different arithmetic
operations using inline functions.
Theory: Here we’ll use classes and make return type functions of the
operations we want to perform .
Source Code:
//Here we'll perform some basic arithmetic operations using inline functions
#include<iostream>
#include<stdlib.h>
using namespace std;
class Calc
{
private:
float a;
float b;
public:
Calc(float h,float k):a(h),b(k)
{}
Calc():a(0),b(0)
{}
inline float sum()
{
return (a+b);
}
inline float difference()
{
return (a-b);
}
inline float product()
{
return (a*b);
}
inline float divide()
{
return (a/b);
}
};
int main()
{
system("COLOR F8");
float x,y;
cout<<"\n Enter two numbers which you want to perform the arithmetic operations";
cin>>x>>y;
Calc D(x,y);
cout<<"\n Sum of the 2 numbers is : "<<D.sum();
cout<<"\n The difference of the 2 numbers will is : "<<D.difference();
cout<<"\n The product of the 2 numbers is : "<<D.product();
cout<<"\n The division the 2 numbers is : "<<D.divide();
return 0;
}

Output:

Conclusion: Here we learned how to use return type inline functions


in performing the various operations.
Program-8
Aim: Program to find the absolute value two numbers
one is of type float and other is of the type integer.
Theory: Here we’ll use the basic principle of the modulus function or
the absolute value function using the else and if statement in the
return type functions.
Source Code:
#include<iostream>
using namespace std;
#include<stdlib.h>
class Absolute
{
private:
float b;
int a;
public:
int absolute(int a)
{
if(a<0)
{
return (-a);
}
else
{
return a;
}
}
float absolute(float b)
{
if(b<0)
{
return (-b);
}
else
{
return b;
}
}
};
int main()
{
system("COLOR F8");
int x;
float y;
cout<<"\n Enter 2 numbers whose absolute value you want to be found";
cin>>x>>y;
Absolute d;
cout<<"\n Absolute value of the integer number is : "<<d.absolute(x);
cout<<"\n Absolute value of the float number is :"<<d.absolute(y);
return 0;
}

Output:

Conclusion: Here we learned how to find the absolute value using


function overloading .
Program-10
Aim: Program to count the number of instances created
Theory: Here we’ll use the static integer to type to count the no. of
objects created as we’ll increment it in the constructor.
Source Code:
//Program to count the number of instances created in a class
#include<iostream>
using namespace std;
class cont
{
static int cnt;
public:
cont()
{
cnt++;
}
~cont()
{
cnt--;
}
void showcnt()
{
cout<<"\n Number of instances active at the moment are "<<cnt;
}
};
int cont::cnt;
int main()
{
cont c1,c2,c3,c4,*c5;
c1.showcnt();
delete c5;
c2.showcnt();
return 0;
}

Output:

Conclusion: Here we learned how to use static integers and


constructors to count the number of instances.
Program-11
Aim: Program to demonstrate polymorphism using
inheritance.
Theory: Here we’ll use virtual functions to access the functions of the
derived class using pointer of base class to demonstrate
polymorphism using inheritance.
Source Code:
#include<iostream>
using namespace std;
class father
{
protected:
int age;
public:
father(int x):age(x)
{}
virtual void iam()
{
cout<<"\n I am the father, my age is : "<<age;
}
};
class son:public father
{
protected:
int age_son;
public:
son(int x,int y):age_son(y),father(x)
{}
void iam()
{
cout<<"\n I am the son , my age is : "<<age_son;
}

};
class daughter:public father
{
protected:
int age_daughter;
public:
daughter(int x,int z):age_daughter(x),father(z)
{}
void iam()
{
cout<<"\n She is the daughter , her age is : "<<age_daughter;
}
};
int main()
{
int x,y,z;
cout<<"\n Enter the age of the father";
cin>>z;
cout<<"\n Enter the age of the son ";
cin>>y;
cout<<"\n Enter the age of the daughter";
cin>>x;
father f(z);
son s(z,y);
daughter d(x,z);
f.iam();
s.iam();
d.iam();
father *f1;
f1=&f;
f1->iam();
f1=&s;
f1->iam();
f1=&d;
f1->iam();
return 0;
}

Output:

Conclusion: Here we learned how to use virtual function to access the


function of derived class using pointer of the base class
Program-12
Aim: Program to create 3 objects for a class and print
the object which has invoked 2 functions which we have
created.
Theory: Here we’ll use this pointer to determine which object has
invoked the functions.
Source Code:
//program to use this pointer and find out which object has invoked the function using this
pointer
#include<iostream>
using namespace std;
#define size 50
class pntr_obj
{
int roll_no;
char name[size];
public:
void set_data()
{
cout<<"\n Enter the roll number";
cin>>roll_no;
cout<<"\n Enter the name of the student";
cin>>name;
}
void print()
{
cout<<"\n Object "<<this;
cout<<"\n Roll number : "<<this->roll_no;
cout<<"\n Name : "<<this->name;
}
};
int main()
{
pntr_obj p1,p2,p3;
p1.set_data();
p1.print();
p2.set_data();
p2.print();
p3.set_data();
p3.print();
return 0;
}

Output:

Conclusion: Here we learned how to use this pointer.

Anda mungkin juga menyukai