practical 3(c++)
practical 3(c++)
3:
3.a. Write a C++ program using classes and objects Student to
print name of the student, roll_no, display the same.
#include<iostream.h>
#include<conio.h>
class student
{
public:
char name[20];
int roll no;
getinfo()
{
cout<<endl<<”Enter name of student:”;
cin>>name;
cout<<endl<<”Enter roll no:”;
cin>>roll no;
}
displayinfo()
{
cout<<endl<<”The name is:”<<name;
cout<<endl<<”The rollno is:”<<rollno;
}
};
void main()
{
student s1,s2;
s1.getinfo();
s1.displayinfo();
s2.getinfo();
s2.displayinfo();
getch();
}
Output:
Enter name of student: XYZ
Enter rollno:20
The name is: XYZ
The rollno is:20
3.b. Write a C++ program for Structure bank employee
to print name of the employee, account_no. and
balance. Display the same also display the balance after
withdraw and deposit.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class bank
{
public:
char name[20];
char account_type[20];
int account_number;
int balance;
void initialize()
{
cout<<”\n enter account holder name:”;
gets(name);
cout<<”\n enter account type:”;
gets(account_type);
cout<<”\n enter account number:”;
gets(account_ number);
cout<<”\n enter balance to deposit:”;
cin>>balance;
}
void deposit()
{
int bal;
cout<<”\n enter amount to deposit:”;
cin>>bal;
balance+=bal;
cout<<”\n amount deposited successfully, your new balance
is:”<<balance;
}
void check()
{
int bal;
cout<<”\n enter amount to withdraw:”;
cin>>bal;
if(bal<=balance)
{
balance -=bal;
cout<<”\n remaining balance:”<<balance;
}
else
{
exit(0);
}
}
void display()
{
cout<<”\n name is:”;
puts(name);
cout<<”\n balance:”<<balance;
}
};
void main()
{
int i;
bank bk;
clrscr();
bk.initialize();
cout<<”\n 1. Your information \n 2.deposit information \n 3.
Withdrawal information”;
cin>>i;
if(i==1)
{
bk.display();
}
else if(i==2)
{
bk.deposit();
}
else if(i==3)
{
bk.check();
}
getch();
}
Output:
enter account holders name: radha
enter account type: saving
enter account number: 574646687
enter balance to deposit: 1000
1. Your information
2. Deposit information
3. Withdrawal information
(if user press 1 then output will be):
name: radha
balance:1000
(if user press 2 then output will be):
enter the amount to deposit: 500
amount deposited successfully, your new balance is:1500
#include<iostream.h>
#include<conio.h>
#include<math.h>
class number
{
private:
int num;
void readno()
{
cout<<”enter number:”;
cin>>num;
}
public:
int factorial()
{
int factorial()
{
int fact=1;
readno();
for(int i=1;i<=num;i++)
{
fact*=i;
}
return fact;
}
int reversenumber()
{
int rev=0, remainder;
readno();
int temp=num;
while(temp!=0)
{
remainder=temp%10;
rev=rev*10+remainder;
temp/=10;
}
return rev;
}
int palindrome()
{
int rev=reversenumber();
return (num==rev);
}
int isArmstrong()
{
int sum=0, temp, remainder;
int digits=0;
temp=num;
while(temp!=0)
{
temp/=10;
digits++;
}
temp=num;
while(temp!=0)
{
remainder=temp%10;
sum+=pow(remainder,digits);
temp/=10;
}
return(sum=num);
}
};
void main()
{
clrscr();
number obj;
cout<<”Factorial of number:”<<obj.factorial()<<endl;
cout<<”Reverse of number:”<<obj. reversenumber()<<endl;
if (obj.palindrome())
{
cout<<”The number is palindrome.”<<endl;
}
else
{
cout<<”The number is not palindrome.”<<endl;
}
if(obj.isArmstrong())
{
cout<<”The number is Armstrong number.”<<endl;
}
else
{
cout<<”The number is not Armstrong number.”<<endl;
}
getch();
}
Output:
enter number: 5
Factorial of number: 120
enter number: 123
Reversed number: 321
enter number:454
The number is palindrome.
The number is not Armstrong number.
3d.Write a program to demonstrate function
outside class and accessing class members in
function definition.
#include<iostream.h>
#include<conio.h>
class student
{
private:
int rollno;
char name[30];
public:
//member function declaration inside class
void getdetails();
void displaydetails();
};
//member function definition outside class
void student::getdetails()
{
cout<<”enter roll number:”;
cin>>rollno;
cout<<”enter name:”;
cin>> name;
}
void student::displaydetails()
{
cout<<”roll number:”<<rollno<<endl;
cout<<”name:”<<name<<endl;
}
void main()
{
clrscr();
student s1;
s1.getdetails();
s1.displaydetails();
getch();
}
Output:
enter roll number:101
enter name: radha
roll number:101
name: radha