C
C
com
#include <iostream>
#include <conio.h>
using namespace std;
class Box
{
public:
float length;
float width;
float height;
};
void main()
{
Box ob1;
Box ob2;
float volume = 0;
cout << "Introduce the first box's characteristics: " << endl;
cin >> ob1.length;
cin >> ob1.width;
cin >> ob1.height;
cout << "Introduce the second box's characteristics: " << endl;
cin >> ob2.length;
cin >> ob2.width;
cin >> ob2.height;
volume = ob1.length*ob1.width*ob1.height;
cout <<"The volume of the first box is: " << volume << endl;
volume = ob2.length*ob2.width*ob2.height;
cout << "The volume of the second box is: " << volume << endl;
_getch();
}
#include <iostream>
#include <conio.h>
using namespace std;
class Box
{
public:
float length;
float width;
float height;
float Volume ();
void setLength (float len);
void setWidth (float wid);
void setHeight (float hei);
};
float Box::Volume()
{
return length*width*height;
}
void Box::setLength(float len)
{
length = len;
}
void Box::setWidth(float wid)
{
width = wid;
}
void Box::setHeight(float hei)
{
height = hei;
}
void main()
{
Box ob1;
Box ob2;
float volume = 0;
cout << "Introduce the first box's characteristics: " << endl;
cin >> ob1.length;
cin >> ob1.width;
cin >> ob1.height;
cout << "Introduce the second box's characteristics: " << endl;
cin >> ob2.length;
cin >> ob2.width;
cin >> ob2.height;
volume = ob1.Volume();
cout << "The volume of the first box is: " << volume << endl;
volume = ob2.Volume();
cout << "The volume of the second box is: " << volume << endl;
_getch();
}
#include <iostream>
#include <conio.h>
using namespace std;
class Line
{
public:
float length;
void setLength(float len);
float getLength();
};
float Line::getLength()
{
return length;
}
void Line::setLength(float len)
{
length = len;
}
void main()
{
Line ob1;
ob1.setLength(6);
cout << "Length of line: " << ob1.getLength() << endl;
cout << "Introduce the length of the line: " << endl;
cin >> ob1.length;
cout << "Length of line: " << ob1.length << endl;
_getch();
}
b) Private members:
#include <iostream>
#include <conio.h>
using namespace std;
class Box
{
float width;
public:
float length;
void setWidth(float wid);
float getWidth();
};
float Box::getWidth()
{
return width;
}
void Box::setWidth(float wid)
{
width = wid;
}
void main()
{
Box ob;
float wid = 0;
cout << "Introduce the length of the box: " << endl;
cin >> ob.length;
cout << "The length of the box is: " << ob.length << endl;
ob.setWidth(6);
cout << "The length of the box is: " << ob.getWidth() << endl;
_getch();
}
c) Protected members:
#include <iostream>
#include <conio.h>
using namespace std;
class Box
{
protected:
float width;
};
class smallBox:Box
{
public:
void setsmallWidth(float wid);
float getsmallWidth();
};
float smallBox::getsmallWidth()
{
return width;
}
void smallBox::setsmallWidth(float wid)
{
width = wid;
}
void main()
{
smallBox ob;
ob.setsmallWidth(100);
cout << "Width of the box: " << ob.getsmallWidth() << endl;
_getch();
}
#include <iostream>
#include <conio.h>
using namespace std;
class Line
{
float length;
public:
void setLength(float len);
float getLength();
Line();
};
Line::Line()
{
cout << "Object is being created!" << endl;
}
void Line::setLength(float len)
{
length = len;
}
float Line::getLength()
{
return length;
}
void main()
{
Line ob;
ob.setLength(100);
cout << "The length of the line is: " << ob.getLength() << endl;
_getch();
}
b) Parameterized constructor:
#include <iostream>
#include <conio.h>
using namespace std;
class Line
{
float length;
public:
void setLength(float len);
float getLength();
Line(float len);
};
Line::Line(float len)
{
cout << "Object is being created and its length is: " << len << endl;
length = len;
}
void Line::setLength(float len)
{
length = len;
}
float Line::getLength()
{
return length;
}
void main()
{
Line ob(100);
cout << "Length of the line: " << ob.getLength() << endl;
ob.setLength(50);
cout << "The new length of the line: " << ob.getLength() << endl;
_getch();
}
#include <iostream>
#include <conio.h>
using namespace std;
class Line
{
float length;
public:
void setLength(float len);
float getLength();
Line();
~Line();
};
Line::Line()
{
cout << "Object is being created!" << endl;
}
Line::~Line()
{
cout << "Object is being destroyed!" << endl;
_getch();
}
void Line::setLength(float len)
{
length = len;
}
float Line::getLength()
{
return length;
}
void main()
{
Line ob;
ob.setLength(100);
cout << "The length of the line is: " << ob.getLength() << endl;
}
#include <iostream>
#include <conio.h>
using namespace std;
class Line
{
int *ptr;
public:
int getLength();
Line(int len);
Line(const Line &obj);
~Line();
};
Line::Line(int len)
{
cout << "Normal constructor allocating ptr" << endl;
ptr = new int;
*ptr = len;
}
Line::Line(const Line &obj)
{
cout << "Copy constructor allocating ptr" << endl;
ptr = new int;
*ptr = *obj.ptr;
}
Line::~Line()
{
cout << "Freeing memory!" << endl;
delete ptr;
_getch();
}
int Line::getLength()
{
return *ptr;
}
void display(Line obj)
{
cout << "Length of line: " << obj.getLength() << endl;
}
void main()
{
Line ob(10);
display(ob);
}
#include <iostream>
#include <conio.h>
using namespace std;
inline int Max(int x, int y)
{
return (x > y) ? x : y;
}
void main()
{
int m, n;
cout << "Introduce m: ";
cin >> m;
cout << "Introduce n: ";
cin >> n;
cout << "Max (m, n): " << Max(m, n) << endl;
cout << "Max (20, 100): " << Max(20, 100) << endl;
cout << "Max (20, 0): " << Max(20, 0) << endl;
cout << "Max (10, 100): " << Max(10, 100) << endl;
_getch();
}
#include <iostream>
#include <conio.h>
using namespace std;
class Box
{
float length;
float width;
float height;
public:
Box(float l, float w, float h)
{
cout << "Constructor called!" << endl;
length = l;
width = w;
height = h;
}
float volume()
{
return length*width*height;
}
int compare(Box box)
{
return this->volume() > box.volume();
}
};
void main()
{
Box ob1(3.3, 1.2, 1.5);
Box ob2(8.5, 6, 2);
if (ob1.compare(ob2))
cout << "ob2 is smaller than ob1" << endl;
else
cout << "ob2 is equal to or larger to ob1" << endl;
_getch();
}
#include <iostream>
#include <conio.h>
using namespace std;
class Box
{
float length;
float width;
float height;
public:
static int objcount;
Box(float l, float w, float h)
{
cout << "Constructor called!" << endl;
length = l;
width = w;
height = h;
objcount++;
}
float volume()
{
return length*width*height;
}
static int getcount()
{
return objcount;
}
};
int Box::objcount = 0;
void main()
{
cout << "Initial stage count: " << Box::objcount << endl;
Box ob1(4, 5, 6);
Box ob2(4, 5, 6);
cout << "Final stage count: " << Box::objcount << endl;
_getch();
}