Oop manual
-
Upload
nishank100ny -
Category
Engineering
-
view
107 -
download
6
Transcript of Oop manual
Index
S.No. Program Date Signature1 To find greatest number among two numbers 12/82 To find the greatest number among three numbers 12/83 To find whether the given number is even or odd 20/84 To swap the values of two variables without using the third
variable20/8
5 To find out the area and circumference of the circle 25/86 To find out the nth term of the Fibonacci series 25/87 To find out the reverse of an n-digit number 1/98 To check whether the given number is palindrome or not 1/99 To check whether the given number is prime or not 8/910 To find the factorial of the given number 23/911 To display all the numbers between 1 and 1000 that are
divisible by a number ‘n’, where n is between 1 and 1003/11
12 To check whether the given number is an Armstrong number or not
3/11
13 To display student’s personal information using the concept of class and objects
10/11
14 To find the mean of two numbers using a friend function 10/1115 To find the greatest value from two values belonging to
two different classes using a friend function.17/11
16 To compute the sum of two complex numbers using constructors
17/11
17 To deposit and withdraw an amount from the bank account 24/1118 To implement hybrid inheritance 24/11
Program.1 Aim: Write a C++ program that accepts two numbers from the keyboard and displays the greatest number.
#include<iostream.h>using namespace std;main(){
int num1, num2, temp;cout<<”Enter the first number=”<<”\n”;cin>>num1;cout<<”Enter the second number=”<<”\n”;cin>>num2;if(num1>=num2)
temp=num1;else
temp=num2;cout<<”Greatest number=”<<temp<<”\n”
}
Program.2 Aim: Write a C++ program that accepts three numbers from the keyboard and displays the greatest number.
#include<iostream.h>using namespace std;main(){
int num1, num2, num3, temp;cout<<”Enter the first number=”<<”\n”;cin>>num1;cout<<”Enter the second number=”<<”\n”;cin>>num2;cout<<”Enter the third number=”<<”\n”;cin>>num3;if(num1>=num2){
if(num1>=num3){
temp=num1;}else{
temp=num3;}
}else{
if(num2>=num3){
temp=num2;}else{
temp=num3;}
}cout<<”Greatest number=”<<temp<<”\n”
}
Program.3Aim: Write a C++ program that accepts a number from the keyboard and checks whether the number is even or odd.
#include<iostream.h>using namespace std;main(){
int num1;cout<<”Enter the number=”<<”\n”;cin>>num1;if(num1%2==0)
cout<<”Number is even”<<”\n”;else
cout<<”Number is odd”<<”\n”;}
Program.4Aim: Write a C++ program that accepts two numbers from the keyboard and swaps their values without using a third variable
#include<iostream.h>using namespace std;main(){
int x, y;cout<<”Enter the first number(x) =”<<”\n”;cin>>num1;cout<<”Enter the second number(y) =”<<”\n”;cin>>num2;x=x+ y;y=x - y;x=x - y;cout<<”After swapping”<<”\n”;cout<<”First number(x) =”<<x<<”\n”;cout<<”Second number(y) =<<y<<”\n”;
}
Program.5 Aim: Write a C++ program that displays the area and circumference of the circle
#include<iostream.h>using namespace std;main(){
int radius; float area, circum;cout<<”Enter the radius of the circle=”<<”\n”;cin>>radius;circum = 2.0 * 3.1427 * float(radius);area = 3.1427 * float(radius * radius);cout<<”Circumference=”<<circum<<”\n”;cout<<”Area=”<<area<<”\n”;
}
Program.6 Aim: Write a C++ program that displays the ‘n’ terms of a Fibonacci series. [Hint: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ..........., n]
#include<iostream.h>using namespace std;main(){
int n, a=0, b=1, c, i;cout<<”Enter the total terms(n)=”<<”\n”;cin>>n;cout<<”Fibonacci Series for “<<n<<”terms:”<<”\n”;cout<<a<<”\n”<<b<<”\n”;for(i=3; i<=n; i++){
c=a+b;cout<<c<<”\n”;a=b;b=c;
}}
Program.7 Aim: Write a C++ program that accepts an ‘n’ digit number and displays its reverse.
#include<iostream.h>using namespace std;main(){
int n, num, i, a, b;cout<<”Enter the number of digits in the number=”<<”\n”;cin>>n;cout<<”Enter the number=”<<”\n”;cin>>num;a=b=num;cout<<”After reversing=”;for(i=1; i<=n; i++){
a=a%10;cout<<a;b=b/10;a=b;
}}
Program.8Aim: Write a C++ program that accepts an ‘n’ digit number from the keyboard and checks whether the number is a palindrome or not.
#include<iostream.h>using namespace std;main(){
int n, num, i, a, b, rev=0;cout<<”Enter the number of digits in the number=”<<”\n”;cin>>n;cout<<”Enter the number=”<<”\n”;cin>>num;a=b=num;for(i=1; i<=n; i++){
a=a%10;rev=(rev*10)+a;b=b/10;a=b;
}if(num==rev)
cout<<”The number is a palindrome”<<”\n”;else
cout<<”The number is not a palindrome”<<”\n”;}
Program.9Aim: Write a C++ program that accepts a number from the keyboard and checks whether the number is a prime number or not.
#include<iostream.h>using namespace std;main(){
int num, i, flag=0;cout<<”Enter the number=”<<”\n”;cin>>num;for(i=2; i<=num-1; i++){
if(num%i==0){
flag=1;break;
}}if(flag==0)
cout<<”Number is Prime”<<”\n”;else
cout<<”Number is not Prime”<<”\n”;}
Program.10Aim: Write a C++ program that accepts a number from the keyboard and find out its factorial.
#include<iostream.h>using namespace std;main(){
int num, fact=1, i=1, n;cout<<”Enter the number=”<<”\n”;cin>>num;n=num;while(i<=n){
fact=fact*(num-1);i=i+1;
}cout<<”Factorial=”<<fact<<”\n”;
}
Program.11Aim: Write a C++ program which accepts a number n between 1 and 100 from the user and displays all numbers between 1 and 1000 not divisible by n.
#include<iostream.h>using namespace std;main(){
int n, i;cout<<”Enter a number between 1 and 100=”<<”\n”;cin>>n;cout<<”Numbers not divisible by “<<n<<”=”<”\n”;i=n+1;while(i<=1000){
if(i%n!=0){
cout<<i<<”\n”;}i=i+1;
}}
Program.12
Aim: If the sum of cubes of the digits of a natural number is equal to the number itself, then such a number is called an Armstrong number. For instance 153 is an Armstrong number as 153 = 13+53+33. Write a program which checks that whether the given number is an Armstrong number or not.
#include<iostream.h>using namespace std;main(){
int n, p, q, sum=0;cout<<”Enter the number=”<<”\n”;cin>>n;p=n;while(p!=0){
q=p%10;sum=sum+(q*q*q);p=p/10;
}if(n==sum)
cout<<”Number is an Armstrong Number.”<<”\n”;else
cout<<”Number is not an Armstrong Number.”<<”\n”;}
Program.13Aim: Write a C++ program using the concept of class & objects to display the student’s personal information.
#include<iostream.h>using namespace std;class student{
private:char name[20], course[30];int enrol;
public:void getstudentdata(void);void displaystudentdata(void);
};void student :: getstudentdata(void){
cout<<”Enter Enrolment No. =”<<”\n”;cin>>enrol;cout<<”Enter Name =”<<”\n”;cin>>name;cout<<”Enter the course =”\n”;cin>>course;
}void student :: displaystudentdata(void){
cout<<”Enrolment No. =”<<enrol<<”\n”;cout<<”Name =”<<name<<”\n”;cout<<”Course =”<<course<<”\n”;
}const int size=3; main(){
student s[size];for(int i=0; i<size; i++){
cout<<”Student”<<i+1<<”\n”;s[i].getstudentdata();
}cout<<”\n”;for(int i=0; i<size; i++){
cout<<”Student”<<i+1<<”\n”;s[i].displaystudentdata();
}
}
Program.14Aim: Write a C++ program using the concept of class & objects to find the mean of two numbers using a friend function.
#include<iostream.h>using namespace std;class sample{
private:int a, b; float c;
public:void setvalue(void);friend float mean(sample);
};void sample :: setvalue(void){
a=25;b=40;
}float mean (sample s){
s.c = (s.a + s.b)/2.0;cout<<”\nMean Value =”<<s.c;
}main(){
sample x;x.setvalue();mean(x);
}
Program.15Aim: Write a C++ program to find the greatest value from two values belonging to two different classes using a friend function.
#include<iostream.h>using namespace std;class ABC;class XYZ{
private:int x;
public:void setvalue(int i){
x=i;}friend void max(XYZ, ABC);
};
class ABC{
private:int y;
public:void setvalue(int i);{
y=i;}friend void max(XYZ, ABC);
};
void max(XYZ m, ABC n){
if(m.x >= n.y)cout<<m.x;
elsecout<<n.y;
}
main(){
ABC abc;abc.setvalue(10);XYZ xyz;xyz.setvalue(20);max(xyz, abc);
}
Program.16Aim: Write a C++ program to compute the sum of two complex numbers using constructors
#include<iostream.h>using namespace std;class complex{
private:float x, y;
public:complex(){}complex(float a){
x=y=a;}complex(float real, float imag){
x=real;y=imag;
}friend complex sum(complex, complex);friend void show(complex);
};
complex sum(complex c1, complex c2){
complex c3;c3.x = c1.x + c2.x;c3.y = c1.y + c2.y;return(c3);
}
void show(complex c){
cout<<c.x<<”+i”<<c.y<<”\n”;}
main(){
complex A(2.7, 3.5);complex B(1.6);complex C;C=sum(A, B);cout<<”A=”;show(A);cout<<”B=”;show(B);cout<<”C=”;show(C);
}
Program.17Aim: Write a C++ program to represent a bank account. Include the following members:
Data members:• Name of the depositor• Account Number• Balance amount in the account
Member functions:• To assign initial values• To deposit an amount• To withdraw an amount after checking the balance• To display name and address
#include<iostream.h>using namespace std;class bankaccount{
private:char name[30];int accno, bal, wd;
public:void bankaccount(void);void getdata(void);void deposit(void);void withdraw(void);void putdata(void);
};
void bankaccount :: bankaccount(void){
bal=0;wd=0;
}
void bankaccount :: getdata(void){
cout<<”\nEnter customer name:”;cin>>name;cout<<”\nEnter account number:”;cin>>accno;
}
void bankaccount :: deposit(void){
cout<<”\nEnter the balance to be deposit:”;cin>>bal;
}
void bankaccount :: withdraw(void){
cout<<”\nEnter the balance to be withdrawn:”;cin>>wd;if(wd<=bal){
bal=bal-wd;cout<<”\nThe amount can be successfully withdrawn.”;
}else{
cout<<”\nThe amount cannot be successfully withdrawn.”;cout<<”\nThe amount is greater than the available balance.”;
}}
void bankaccount :: putdata(void){
cout<<”\nName:”<<name;cout<<”\nAccount No.:”<<accno;cout<<”\nAvailable balance:”<<bal;
}
const int size=3;main(){
bankaccount customer[size];for(i=0;i<size;i++){
cout<<”\nCustomer”<<i+1;customer[i].getdata();customer[i].deposit();customer[i].withdraw();customer[i].putdata();
}}
Program.18Aim: Write a C++ program to implement hybrid inheritance (combination of hierarchical, multilevel and multiple inheritance)
#include<iostream.h>using namespace std;class student{
protected:int rollno;
public:void getdata(int a){
rollno=a;}void putdata(void){
cout<<”\nRoll Number:”<<rollno;}
};
class test :: public student{
protected:float sub1, sub2;
public:void getmarks(float x, float y){
sub1=x;sub2=y;
}void putmarks(void){
cout<<”\nMarks obtained”;cout<<”\nSubject 1:”<<sub1;cout<<”\nSubject 2:”<<sub2;
}};
class sports{
protected:float score;
public:void getscore(float s){
score=s;}void putscore(void){
cout<<”\nSports score:”<<score;}
};
class result :: public test, public sports
{float total;public:
void display(void);};
void result :: display(void){
total = sub1+sub2+score;putdata();putmarks();putscore();cout<<”\nTotal score:”<<total;
}
main(){
result student1;student1.getdata(12);student1.getmarks(27.5, 33.0);student1.getscore(6.0);student1.display();
}