CSI 250 Homework 1, Fall 2011
American University of Science and Technology · Computer Programming II · 4 pages · Mr. Elie Nasr
More CSI 250 files
Show the text of this homework
Rouba Jardak
#3.10:
the data members declared as private in class are accessible only to member functions of the
class for which they are declared. they cannot be accessed by functions outside the class such as
main or by member functions of other classes in the program. so in order to access these private
data members we might have set and get functions as public in this class (so we can fill
information in these public data members or modify them from the set function and have their
content from get function).
#3.12:
#include<iostream.h>
class account {
private:
int balance;
public :
account (int=0);
void credit ();
void debit ();
int getbalance();
};
account ::account (int n)
{
int balance=n;
if(balance<0)
cout<<"the initial balance is invalid"<<endl;
}
void account::credit()
{
int l;
cout<<"enter new amount:";
cin>>l;
balance+=l;
}
void account::debit()
{
int d;
cout<<"enter debit:";
cin>>d;
balance-=d;
if(balance<0)
{
cout<<"debit amount exeeded account balance"<<endl;
balance+=d;
}
}
int account::getbalance()
{
return balance;
}
int main()
{
account s1 (1000);
s1.credit();
s1.debit();
cout<<"the balance is:"<<s1.getbalance<<endl;
account s2 (500);
s2.credit();
s2.debit();
cout<<"the balance is:"<<s2.getbalance<<endl;
return 0;
}
#3.14:
#include<iostream.h>
#include<string.h>
class employee {
private:
char fname[20];
char lname[20];
int salary;
public:
employee(char []=" ",char []=" ",int=0);
void setfname (char*);
void setlname (char*);
void setsalary (int);
char * getfname();
char * getlname();
int getsalary();
};
employee::employee(char * f, char * l,int s)
{
setfname(f);
setlname(l);
if(s<0)
{
s=0;
setsalary(s);
}
else
setsalary(s);
}
void employee::setfname(char * f)
{
strcpy(fname,f);
}
void employee::setlname(char * l)
{
strcpy(lname,l);
}
void employee::setsalary(int s)
{
salary=s;
}
char * employee::getfname()
{
return fname;
}
char * employee::getlname()
{
return lname;
}
int employee::getsalary()
{
return salary;
}
int main()
{
char f[20];
char l[20];
int s;
employee e1;
cout<<"enter first and last name for employee 1:";
cin>>f>>l;
cout<<"enter monthly salary:";
cin>>s;
e1.setfname(f);
e1.setlname(l);
e1.setsalary(s);
employee e2;
cout<<"enter first and last name for employee 2:";
cin>>f>>l;
cout<<"enter monthly salary:";
cin>>s;
e2.setfname(f);
e2.setlname(l);
e2.setsalary(s);
int s1=e1.getsalary();
cout<<"the yearly salary for the employee: "<<e1.getfname()<<" "<<e1.getlname()<<"
is:"<<12*s1<<endl
<<"after the raise it becomes :"<<s1+(s1*10)/100<<endl;
int s2=e2.getsalary();
cout<<"the yearly salary for the employee: "<<e2.getfname()<<" "<<e2.getlname()<<"
is:"<<12*s2<<endl
<<"after the raise it becomes:"<<s2+(s2*10)/100<<endl;
return 0;
}