BSC IT PRACTICALS: OOPS
Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Thursday, January 18, 2018

OOPS Write a program to demonstrate function definition outside class and accessing class members in function definition.

January 18, 2018 3
OOPS Write a program to demonstrate function definition outside class and accessing class members in function definition.

CODE:

#include<iostream.h>
#include<conio.h>

class fibo
{
public:
void fibonacci();
};

void fibo::fibonacci()
{
   int t1 = 0, t2 = 1, nextTerm = 0, n;

    cout << "\nEnter a positive number to calculate \nthe Fibonacci series upto entered number: ";
    cin >> n;
    cout << "\nFibonacci Series: " << t1 << ", " << t2 << ", ";

    nextTerm = t1 + t2;

    while(nextTerm <= n)
    {
        cout << nextTerm << ", ";
        t1 = t2;
        t2 = nextTerm;
        nextTerm = t1 + t2;
    }
}

void main()
{
clrscr();
fibo ob;
ob.fibonacci();
getch();

}

OOPS Design the class Demo which will contain the following methods: readNo(), factorial() for calculating the factorial of a number, reverseNo() will reverse the given number, isPalindrome() will check the given number is palindrome, isArmstrong() which will calculate the given number is armStrong or not.Where readNo() will be private method.

January 18, 2018 0
OOPS Design the class Demo which will contain the following methods: readNo(), factorial() for calculating the factorial of a number, reverseNo() will reverse the given number, isPalindrome() will check the given number is palindrome, isArmstrong() which will calculate the given number is armStrong or not.Where readNo() will be private method.

CODE:

#include<iostream.h>
#include<conio.h>

class abc
{
int a1,a2,a3,a4;
void readno_fact()
{
cout<<"enter the number to find the factorial:";
cin>>a1;

}
void readno_rev()
{
cout<<"\nenter the number to find the reverse number:";
cin>>a2;

}
void readno_pal()
{
cout<<"\nenter the number to find the palindrome number :";
cin>>a3;

}
void readno_ams()
{
cout<<"\nenter the number to find the amstrong number :";
cin>>a4;

}

public:
void factorial();
int reverseno();
void palindrome();
void amstrong();
};

void abc::factorial()
{       readno_fact();
double x,fact=1;
x=a1;
for(int i=1;i<=x;i++)
{
fact=fact*i;
}
cout<<"\nthe factorial of number is :"<<fact<<endl;
}

int abc::reverseno()
{        readno_rev();
int y,rem,temp=0;
y=a2;
while(y!=0)
{
rem=y%10;
temp=10*temp+rem;
y=y/10;
}
return temp;
}

void abc::palindrome()
{ readno_pal();
int pp,temp2=0,rem2=0;
pp=a3;
while(pp!=0)
{
rem2=pp%10;
temp2=10*temp2+rem2;
pp=pp/10;
}

if(temp2==a3)
{
cout<<"\nthe number is palindrome\n";
}
else
{
cout<<"\nthe number is not palindrome\n";
}
}

void abc::amstrong()
{       readno_ams();
int z,sum=0,r;
z=a4;
while(z!=0)
{
r=z%10;
sum=sum+r*r*r;
z=z/10;
}

if(sum==a4)
cout<<"\nthe number is amstrong";
else
cout<<"\nthe number is not amstrong";
}

void main()
{
clrscr();
abc anynum;
anynum.factorial();
int r=0;
        r=anynum.reverseno();
        cout<<"\nreverse of the number is :"<<r<<endl;
anynum.palindrome();
anynum.amstrong();
getch();


}

OOPS Design the class student containing getData() and displayData() as two of its methods which will be used for reading and displaying the student information respectively.Where getData() will be private method.

January 18, 2018 0
OOPS Design the class student containing getData() and displayData() as two of its methods which will be used for reading and displaying the student information respectively.Where getData() will be private method.

CODE:

#include<iostream.h>
#include<conio.h>

class student
{
char name[20],div[5],std[10],add[20];
int roll_no;
void getdata()
{
cout<<"enter the name :";
cin>>name;
cout<<"enter the roll number :";
cin>>roll_no;
cout<<"enter the division :";
cin>>div;
cout<<"enter the standerd :";
cin>>std;
cout<<"enter the address :";
cin>>add;
}
public:

void displaydata()
{
getdata();
cout<<"THE ENTERED INFORMATION OF STUDENT IS : "<<endl;
cout<<"the name is : "<<name<<endl;
cout<<"the roll no is : "<<roll_no<<endl;
cout<<"the division is : "<<div<<endl;
cout<<"the standerd is : "<<std<<endl;
cout<<"Address is : "<<add<<endl;
}
};

void main()
{
    clrscr();
student s1;
s1.displaydata();
getch();

}

OOPS Design an employee class for reading and displaying the employee information, the getInfo() and displayInfo() methods will be used repectively. Where getInfo() will be private method

January 18, 2018 2
OOPS Design an employee class for reading and displaying the employee information, the getInfo() and displayInfo() methods will be used repectively. Where getInfo() will be private method

CODE:

# include <iostream.h>
# include <conio.h>
class employee
{
private:
int emp_id;
char emp_name[30];
double salary;
void getinfo()
{
cout<<"Enter Name"<<endl;
cin>>emp_name;
cout<<"Enter ID"<<endl;
cin>>emp_id;
cout<<"Enter salary"<<endl;
cin>>salary;
}

public:
void displayinfo()
{
getinfo();
cout<<endl<<"THE INFORMATION ENTERED IS :"<<endl;
cout<<"Name is "<<emp_name<<endl;
cout<<"ID is "<<emp_id<<endl;
cout<<"Your salary is "<<salary<<endl;
}
};
void main()
{
clrscr();
employee e1;
e1.displayinfo();

getch();
}