Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
41 views5 pages

13A Classes & Objects-All Programs

The document contains code for several C++ programs that demonstrate classes and objects. Program 1 defines a Circle class with private radius and area variables and public setradius() and calc_area() methods. It creates a circle object, sets its radius, and prints the area. Program 2 defines the Circle class methods outside the class. Program 3 adds new methods like inc_rad() to increase radius and disp_area() to display area. It demonstrates passing objects to methods. Programs 4 and 5 demonstrate friend functions, defining classes for Sample and Complex numbers and defining friend functions to access private members.

Uploaded by

darshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views5 pages

13A Classes & Objects-All Programs

The document contains code for several C++ programs that demonstrate classes and objects. Program 1 defines a Circle class with private radius and area variables and public setradius() and calc_area() methods. It creates a circle object, sets its radius, and prints the area. Program 2 defines the Circle class methods outside the class. Program 3 adds new methods like inc_rad() to increase radius and disp_area() to display area. It demonstrates passing objects to methods. Programs 4 and 5 demonstrate friend functions, defining classes for Sample and Complex numbers and defining friend functions to access private members.

Uploaded by

darshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PDPU, Gandhinagar 13 Classes & Objects-All Programs

Object1.cpp

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

class circle
{
private:
int radius;
float area;
public:
void setradius(int r)
{
radius = r;
cout << "The value of radius is set to " << radius << endl;
area = calc_area();
cout << area << endl;
}
float calc_area(void)
{
area = 22.0/7*radius*radius;
return area;
}
};

void main()
{
clrscr();
circle c1;
int r = 5;
c1.setradius(r);
cout << "The area of circle having radius "<< r << " is " << c1.calc_area() << endl;
// cout << "The value of radius of c1 is " << c1.radius;
// error: not accessible outside circle.
// cout << setradius(10);
// again an error : setradius should have prototype.
getch();
};

By Darshit Shah 1
PDPU, Gandhinagar 13 Classes & Objects-All Programs

Object2.cpp (Member Functions outside the Class)

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

class circle
{
private:
int radius;
float area;
public:
void setradius(int); // here, we declare only prototypes of different
float calc_area(void); // functions.
};

void circle::setradius(int r)
{
radius = r;
cout << "The value of radius is set to " << radius << endl;
area = calc_area();
cout << area << endl;
}

float circle::calc_area(void)
{
area = 22.0/7*radius*radius;
return area;
}

void main()
{
clrscr();
circle c1;
int r = 5;
c1.setradius(r);
cout << "The area of circle having radius "<< r << " is " << c1.calc_area() << endl;
getch();
};

By Darshit Shah 2
PDPU, Gandhinagar 13 Classes & Objects-All Programs
Object3.cpp

#include <iostream.h>
#include <conio.h>
class circle
{
private:
int radius;
float area;
public:
void setradius(int);
float calc_area(void);
void inc_rad(circle *,int); // increases radius;
void disp_area(void);
};
void circle::setradius(int r)
{
radius = r;
cout << "from function setradius()" << endl;
cout << "\tThe value of radius is set to " << radius << endl;
area = calc_area();
cout << "from function setradius()" << endl;
cout << "\t" << area << endl;
}
float circle::calc_area(void)
{
cout << "From function calc_area()" << endl;
area = 22.0/7*radius*radius;
return area;
}
void circle::inc_rad(circle * c,int r)
{
c->radius += r;
cout << "from function inc_rad()" << endl;
cout << "\tradius is increased by " << r << endl;
cout << "\tnew radius is " << c->radius << endl;
c->area = c->radius * c->radius * 22.0 / 7;
}
void circle::disp_area(void)
{
cout << "from function disp_area()" << endl;
cout << "\tarea for radius" << radius << " is " << area << endl;
}

void main()

By Darshit Shah 3
PDPU, Gandhinagar 13 Classes & Objects-All Programs
{
clrscr();
circle c1;
int r = 5;
c1.setradius(r);
cout << "from function main()" << endl;
cout << "\t" << r << " " << c1.calc_area() << endl;
c1.inc_rad(&c1,10);
c1.disp_area();
};

Object4.cpp

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

class sample
{
int a, b;
public:
void setvalue()
{
a = 25;
b = 30;
}
friend float mean(sample);
};

float mean(sample s)
{
return float(s.a + s.b) / 2.0;
}

void main()
{
clrscr();
sample X;
X.setvalue();
cout <<"Mean value = " << mean(X) << endl;
getch();
};
/* note: The friend function accesses the class variables a and b by using the dot operator and the
object passed to it. The function call mean(X) passes the object X by value to the friend function.
*/

Object5.cpp

By Darshit Shah 4
PDPU, Gandhinagar 13 Classes & Objects-All Programs

#include <iostream.h>
#include <conio.h>
class complex // x + iy form;
{
float x;
float y;
public:
void setval(float real, float imag)
{
x = real;
y = imag;
}
friend complex sum(complex, complex);
void show(void);
// 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 complex:: show(void)
{
cout << x << " + j" << y << "\n";
}
/*void complex:: show(complex c)
{
cout << c.x << " + j" << c.y << "\n";
} */
void main()
{
clrscr();
complex A, B, C;
A.setval(3.1, 5.65);
B.setval(2.75, 1.2);
C = sum(A,B);
cout << "A = "; A.show();
cout << "B = "; B.show();
cout << "C = "; C.show();
getch();
};

By Darshit Shah 5

You might also like