Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Title: Basics of Objected Oriented Programming in C++
Description: This will explain you how to define classes and make use of them in C++. These programs will explain the easy ways coded on Microsoft Visual Studio. The output of programs has also been there in the uploaded notes.
Description: This will explain you how to define classes and make use of them in C++. These programs will explain the easy ways coded on Microsoft Visual Studio. The output of programs has also been there in the uploaded notes.
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
[AYESHA KHAN] Cs-030
OOP ASSIGNMENT
PROGRAM #1:
(Programs in class & homework)
Class Bankaccount:
Source code:
#include
#include
setid(1);
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
1
[AYESHA KHAN] Cs-030
}
b1
...
display();
b2
...
setbalance(6000);
b2
...
withdraw(1000);
b1
...
withdraw(8000);
b2
...
deposit(2050);
if(b1
...
getbalance())
cout<<"equal";
getch();
Output:
PROGRAM #2:
Class Student:
Source code:
#include
#include
cal_per();
s2
...
display();
s2
...
h>
usingnamespace std;
class bankaccount
{
private:
int id;
double balance;
public:
bankaccount();
bankaccount(int i,double b);
void display();
int getid();
double getbalance();
void setid(int i);
void setbalance(double bal);
void withdraw(double amt);
void deposit(double amt);
};
bankaccount::bankaccount()
{
cout<<"Enter id:";
cin>>id;
cout<<"Enter balance:";
cin>>balance;
}
bankaccount::bankaccount(int i,double b)
{
id=i;
balance=b;
}
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
4
[AYESHA KHAN] Cs-030
void bankaccount::display()
{ cout<<"Id:"<
{ return id; }
double bankaccount::getbalance()
{ return balance;}
void bankaccount::setid(int i)
{ id=i; }
void bankaccount::setbalance(double bal)
{
if(bal>500)
balance=bal;
else
cout<<"Error"<
void bankaccount::withdraw(double amt)
{
if(balance>amt+500)
balance-=amt;
else
cout<<"Error"<
void bankaccount::deposit(double amt)
{
balance+=amt;
}
void main()
{
bankaccount b1,b2;
b1
...
display();
b1
...
setbalance(30000);
b1
...
setid(2);
b2
...
display();
b1
...
display();
b2
...
display();
b1
...
getbalance()==b2
...
h>
usingnamespace std;
class rectangle
{
private:
float length,width;
double area;
public:
rectangle();
rectangle(float l,float w);
void show();
float getlength();
float getwidth();
void setlength(float l);
void setwidth(float w);
void cal_area();
};
rectangle::rectangle()
{
cout<<"Enter length:";
cin>>length;
cout<<"Enter width:";
cin>>width;
}
rectangle::rectangle(float l,float w)
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
6
[AYESHA KHAN] Cs-030
{
length=l;
width=w;
}
void rectangle::show()
{
cout<<"Length:"<
float rectangle::getlength()
{ return length; }
float rectangle::getwidth()
{ return width; }
void rectangle::setlength(float l)
{ length=l; }
void rectangle::setwidth(float w)
{ width=w; }
void rectangle::cal_area()
{
area=length*width;}
void main()
{
rectangle r1(3
...
9);
rectangle r2;
r1
...
cal_area();
r1
...
show();
getch();
}
Output:
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
7
[AYESHA KHAN] Cs-030
PROGRAM # 5:
Calss Bankaccount (with copy constructor):
Source code:
#include
#include
id;
balance=b
...
display();
b2
...
display();
b4
...
setid(1);
b1
...
display();
b2
...
setbalance(6000);
b2
...
withdraw(1000);
b1
...
withdraw(8000);
b2
...
deposit(2050);
if(b1
...
getbalance())
cout<<"equal";
getch();
}
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
9
[AYESHA KHAN] Cs-030
Output:
PROGRAM # 6:
Class Employee:
Source code:
#include
#include
name;
id=e
...
bassicpay;
allownces=e
...
total;
}
void employee::show()
{
cout<<"Name:"<
void employee::calculatepay()
{
total=bassicpay+allownces-0
...
calculatepay();
e2
...
show();
e2
...
h>
#include
usingnamespace std;
class employee
{
string name;
int id;
staticint count;
double bassicpay,allownces,total;
public:
employee();
employee(string n,double bp, double a);
employee(employee &e);
void show();
void calculatepay();
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
12
[AYESHA KHAN] Cs-030
void setname(string n);
void setid(int i);
void setbassicpay(double bp);
void setallownces(double a);
void getname(string *p);
int getid();
double getbassicpay();
double getallownces();
};
int employee::count=0;
employee::employee()
{
count++;
id=count;
cout<<"Enter name:";
cin>>name;
cout<<"Enter bassic pay:";
cin>>bassicpay;
cout<<"Enter allownces:";
cin>>allownces;
}
employee::employee(string n,double bp, double a)
{
count++;
name=n;
id=count;
bassicpay=bp;
allownces=a;
}
employee::employee(employee &e)
{
name=e
...
id;
bassicpay=e
...
allownces;
total=e
...
2*bassicpay;
}
void employee::setname(string n)
{ name=n;}
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
13
[AYESHA KHAN] Cs-030
void employee::setid(int i)
{ id=i;}
void employee::setbassicpay(double bp)
{ bassicpay=bp;}
void employee::setallownces(double a)
{ allownces=a;}
void employee::getname(string *p)
{ p=&name;}
int employee::getid()
{return id;}
double employee::getbassicpay()
{return bassicpay;}
double employee::getallownces()
{return allownces;}
void main()
{
employee e1,e2("Nimra",25000,10000);
e1
...
calculatepay();
e1
...
show();
getch();
Output:
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
14
[AYESHA KHAN] Cs-030
PROGRAM # 8:
Class Complex No:
Source code:
#include
#include
Real;
Imaginary=c
...
Real=Real+c
...
Imaginary=Imaginary+c
...
Real=Real-c
...
Imaginary=Imaginary-c
...
Real=Real*c
...
Imaginary;
r
...
Imaginary+c
...
Real==Real&&c
...
5),r(0,0);
c1
...
display();
c1
...
magnitude();
r=c1
...
display();
r=c1
...
display();
r=c1
...
display();
cout<<"Compare c1 & c2:"<
h>
usingnamespace std;
class abc
{
int _x;
staticint count;
public:
abc(int x);
~abc();
int getx();
};
int abc::count=0;
abc::abc(int x)
{
count++;
_x=x;
}
abc::~abc()
{
count--;
cout<<"Destroying object"<<_x<
}
int abc::getx()
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
17
[AYESHA KHAN] Cs-030
{ return _x;}
void main()
{
cout<<"Inside main"<
abc obj2(3);
if(obj1
...
getx())
{
cout<<"Inside if"<
}
}
Output:
PROGRAM # 10:
Pointers to objects:
Source code:
#include
#include
show();
abc *ob=new abc("object 2");
delete ob;
Output:
PROGRAM # 11:
Array of object:
Source code:
#include
#include
Real;
Imaginary=c
...
magnitude();
cout<<"Magnitude of "<
getimag()<<"i is"<
Complex_No array2[3]={Complex_No(2,3),Complex_No(1,2),Complex_No(4,5)};
getch();
}
Output:
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
20
[AYESHA KHAN] Cs-030
PROGRAM # 12:
Pointer to an array:
Source code:
//pointer to array
#include
#include
Real;
Imaginary=c
...
h>
#include
usingnamespace std;
class Complex_No
{
float Real;
float Imaginary;
public:
Complex_No();
Complex_No(float R,float I);
Complex_No(Complex_No & c);
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
22
[AYESHA KHAN] Cs-030
void display();
void setreal(float r);
void setimag(float i);
float magnitude();
float getreal();
float getimag();
};
Complex_No::Complex_No(float R,float I)
{
Real=R;
Imaginary=I;
}
Complex_No::Complex_No()
{
cout<<"Enter real part:";
cin>>Real;
cout<<"Enter imaginary part:";
cin>>Imaginary;
}
Complex_No::Complex_No(Complex_No & c)
{
Real=c
...
Imaginary;
}
void Complex_No::display()
{
cout<<" The complex number is "<
float Complex_No::magnitude()
{
float mag;//magnitude
mag=sqrt(pow(Real,2)+pow(Imaginary,2));
return mag;
}
void Complex_No::setreal(float r)
{ Real=r;}
void Complex_No::setimag(float i)
{ Imaginary=i;}
float Complex_No::getreal()
{return Real;}
float Complex_No::getimag()
{return Imaginary;}
void main()
{
Complex_No arrp[3];
Complex_No *p;
p=arrp;//or p=&arrp[0]
float m=p->magnitude();
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
23
[AYESHA KHAN] Cs-030
}
p->display();
p++;
m=p->magnitude();
p->display();
p=arrp;
for(int i=0;i<3;i++)
{
p->display();
p++;
}
getch();
Output:
PROGRAM # 14:
This pointer:
Source code:
#include
#include
show();
obj1
...
h>
usingnamespace std;
class abc
{
int _x;
public:
abc(int x);
abc();
void show();
};
abc::abc(int x)
{
_x=x;
}
abc::abc()
{
cout<<"Enter x:";
cin>>_x;
}
void abc::show()
{ cout<<"Value of x is "<<_x;}
void main()
{
abc obj(2);
abc arr[5];
obj
...
show();
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
25
[AYESHA KHAN] Cs-030
arr[1]
...
h>
using namespace std;
class Student{
private:
char Name[50];
int Rollno;
double Marks[5];
float percentage;
public:
Student()
{
std::cout<<"Enter name of student:";
std::cin>>Name;
std::cout<<"Enter roll no:";
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
26
[AYESHA KHAN] Cs-030
std::cin>>Rollno;
for(int i=0;i<5;i++)
{
std::cout<<"Enter marks for subject#"<std::cin>>Marks[i];
}
std::cout<<"Enter percentage:";
cin>>percentage;
}
/* void Percentage()
{
int sum=0;
for(int i=0;i<5;i++)
{
sum+=Marks[i];
}
percentage=(sum/500
...
Percentage();
}
*/
for(int j=0;j<3;j++)
{
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
27
[AYESHA KHAN] Cs-030
s[j]
...
Real = Real + C
...
Imaginary = Imaginary + C
...
Display();
C2
...
Display();
C3
...
Add(C3);
C4
...
value=0;
}
int main()
{
Counter C;
C
...
Increment();
C
...
Decrement();
C
...
Increment();
C
...
h>
# include
using namespace std;
class Student{
private:
char Name[50];
int Rollno;
double Marks[5];
float percentage;
public:
Student()//Default constructor/input constructor
{
std::cout<<"Enter name of student:";
std::cin>>Name;
std::cout<<"Enter roll no:";
std::cin>>Rollno;
for(int i=0;i<5;i++)
{
std::cout<<"Enter marks for subject#"<std::cin>>Marks[i];
}
std::cout<<"Enter Percentage:";
std::cin>>percentage;
}
Student(char *name,int r_no,double marks[5],float per)//parametrized constructor
{
strcpy(Name,name);
Rollno=r_no;
for(int i=0;i<5;i++)
{
Marks[i]=marks[i];
}
percentage=per;
}
Student(Student &obj)//copy constructor
{
strcpy(Name, obj
...
Rollno;
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
35
[AYESHA KHAN] Cs-030
int i;
for (i = 0; i<5; i++)
{
Marks[i] = obj
...
0)*100;
return percentage;
//std::cout<<"Percentge is:"<
void Grade()
{
if(Calculate_Percentage()>=90)
{
cout<<"Grade A";
}
else if(Calculate_Percentage()>=80)
{
cout<<"Grade B";
}
else if(Calculate_Percentage()>=70)
{
cout<<"Grade C";
}
else if(Calculate_Percentage()>=60)
{
cout<<"Grade D";
}
else if(Calculate_Percentage()<60)
{
cout<<"Grade F";
}
}
void display()
{
std::cout<<"RECORD OF STUDENT"<<"\n";
std::cout<<"Name of student is:"<
for(int i=0;i<5;i++)
{
std::cout<<"Subject#"<}
std::cout<<"\n";
std::cout<<"Percentage is:"<
cout<<"\n\n";
}
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
37
[AYESHA KHAN] Cs-030
};
int main()
{
Student *s;
char name[50];
int r_no;
double marks[5];
float per;
s=new Student [5];
for(int i=0;i<5;i++)
{
s[i]
...
name;
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
39
[AYESHA KHAN] Cs-030
Rollno = s
...
Marks[j];
Percentage = s
...
display();
S2
...
h>
usingnamespace std;
class A
{
int _x;
public:
A(int x);
A();
int getx();
void show();
};
class B:public A
{
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
43
[AYESHA KHAN] Cs-030
public:
int _y;
B();
void show();
int gety();
};
A::A(int x)
{ _x=x;}
A::A()
{ cout<<"Enter x:";
cin>>_x;}
int A::getx()
{ return _x; }
void A::show()
{ cout<<"x="<<_x<
{ cout<<"Enter y:";
cin>>_y;}
int B::gety()
{ return _y;}
void B::show()
{ A::show();
cout<<"y="<<_y<
{
A obj1;
B obj2;
//obj1
...
getx();
obj1
...
gety();//error gety() is derived class function
//int t=obj2
...
getx();
//m=obj2
...
show();
getch();
}
Output:
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
44
[AYESHA KHAN] Cs-030
PROGRAM # 24:
Inheritance:
(use of “protected” access specifier):
Source code:
#include
#include
getx();
t=obj1
...
show();
t=obj2
...
gety();
t=obj2
...
show();
obj2
...
getx();
//t=obj3
...
getw();
obj3
...
show();
getch();
}
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
46
[AYESHA KHAN] Cs-030
Output:
PROGRAM # 25:
Inheritance parametarized constructor:
Source code:
#include
#include
show();
obj2
...
h>
#include
usingnamespace std;
class vector2D
{
protected:
float _x;
float _y;
public:
vector2D(float x,float y);
void display();
vector2D add(vector2D v);
bool compare(vector2D v);
};
class vector3D:public vector2D
{
float _z;
public:
vector3D(float x,float y,float z);
void display();
vector3D add(vector3D v);
bool compare(vector3D v);
};
vector2D::vector2D(float x,float y)
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
48
[AYESHA KHAN] Cs-030
{
_x=x;
_y=y;
}
void vector2D::display()
{
cout<<_x<<"i+"<<_y<<"j"<
vector2D vector2D::add(vector2D v)
{
vector2D r(0,0);
r
...
_x;
r
...
_y;
return r;
}
bool vector2D::compare(vector2D v)
{
if(v
...
_y==_y)
returntrue;
elsereturnfalse;
}
vector3D::vector3D(float x,float y,float z):vector2D(x,y)
{ _z=z;}
void vector3D::display()
{
cout<<_x<<"i+"<<_y<<"j+"<<_z<<"k"<
vector3D vector3D::add(vector3D v)
{
vector3D r(0,0,0);
r
...
_x;
r
...
_y;
r
...
_z;
return r;
}
bool vector3D::compare(vector3D v)
{
if(v
...
_y==_y&& v
...
9,7
...
display();
v2
...
add(v2);
Oop assignment#1 SUBMITTED TO MA’AM FAUZIA
49
[AYESHA KHAN] Cs-030
}
cout<<"Result=";
r1
...
compare(v2)<
...
display();
r2=v3
...
display();
cout<<"Compare v3 and v4:"<
Title: Basics of Objected Oriented Programming in C++
Description: This will explain you how to define classes and make use of them in C++. These programs will explain the easy ways coded on Microsoft Visual Studio. The output of programs has also been there in the uploaded notes.
Description: This will explain you how to define classes and make use of them in C++. These programs will explain the easy ways coded on Microsoft Visual Studio. The output of programs has also been there in the uploaded notes.