cpp Class Object
Published on: 04 February 2025
Resources
Aim: Construct a program that illustrates the concept and implementation of class and objects
Practice Exercise 1: Write a program to calculate the density using class and object.
Theory:
Density ($\rho$) is the ratio of mass ($m$) of the fluid to its volume ($V$). It is given by:
$$ \rho = \frac{m}{V} $$
Algorithm:
- Define a class
Density
with float variablesmass
andvolume
. - Create a member function
mDensity()
that returnsmass/volume
. - Create an object
d
of classDensity
. - Assign values to
d.mass
andd.volume
. - Call
d.mDensity()
and print the result.
Code:
#include <iostream>
using namespace std;
class Density
{
public:
float mass;
float volume;
float mDensity()
{
return mass / volume;
}
};
int main()
{
Density d;
d.mass = 10;
d.volume = 5;
cout << d.mDensity() << endl;
return 0;
}
Self Assessment
- Declare an Car Class with data members and member functions with private and public access specifiers.
- Declare an object to call the show function printing the variable values on screen.
Extra Self Practice Material
Self Practice Program 1: Write a program to calculate the position of a particle using class and object.
Theory:
A class is a blueprint for creating objects. It encapsulates data (attributes) and methods (functions). In this example, the class 'Particle' represents a 2D particle with attributes 'x' and 'y' for position and a method 'move' to update its position. Objects are instances of the class, allowing manipulation of their attributes and behaviors.
Algorithm:
- Define a class 'Particle' with attributes 'x' and 'y' for position and a method 'move(dx, dy)' to update the position.
- In the 'main' function, create an object of the 'Particle' class and initialize its position.
- Print the initial position of the particle.
- Call the 'move' method with appropriate values to update the particle's position.
- Print the updated position of the particle.
Code:
#include <iostream>
using namespace std;
class Particle {
public:
float x, y; // Position
void move(float dx, float dy) {
x += dx;
y += dy;
}
};
int main() {
Particle p;
// Set initial position
p.x = 0.0;
p.y = 0.0;
cout << "Initial position: (" << p.x << ", " << p.y << ")" << endl;
// Move the particle
p.move(2.0, 3.0);
cout << "Final Position: (" << p.x << ", " << p.y << ")" << endl;
return 0;
}
Self Practice Program 2: Write a program to print material properties using class and object.
Theory:
The program uses a Material
class to store and display material properties like modulus of elasticity, modulus of rigidity, Poisson's ratio, and unit weight. Each material is initialized using a setter method and displayed in a formatted table.
Algorithm:
- Define a
Material
class with properties and methods for setting and displaying values. - Create objects for materials (e.g., Brass, Cast Iron).
- Initialize properties using the
setValues
method. - Display a table header.
- Use the
display
method to show material properties. - End program.
Code:
#include <iostream>
#include <string>
using namespace std;
class Material
{
public:
string name;
double modulus_of_elasticity;
double modulus_of_rigidity;
double poisson_ratio;
double unit_weight;
void setValues(string n, double E, double G, double v, double w)
{
name = n;
modulus_of_elasticity = E;
modulus_of_rigidity = G;
poisson_ratio = v;
unit_weight = w;
}
void display();
};
void Material::display()
{
cout << name << "\t\t\t\t\t" << modulus_of_elasticity << "\t\t\t\t\t"
<< modulus_of_rigidity << "\t\t\t\t" << poisson_ratio
<< "\t\t " << unit_weight << endl;
}
int main()
{
Material brass, cast_iron, copper, glass;
// Using setter function to set values
brass.setValues("Brass", 106.0, 40.1, 0.324, 83.8);
cast_iron.setValues("Cast Iron", 100.0, 41.4, 0.211, 70.6);
copper.setValues("Copper", 119.0, 44.7, 0.326, 87.3);
glass.setValues("Glass", 46.2, 18.6, 0.245, 25.4);
cout << "\n\t\t\t\t\t\t\t-------------------------------";
cout << "\n\t\t\t\t\t\t\t Material Property Table";
cout << "\n\t\t\t\t\t\t\t-------------------------------\n";
cout << "\n-----------------------------------------------------------------------------------------------------------------------------------------------";
cout << "\nMaterial\t\t\tModulus of Elasticity(E)\t\tModulus of Rigidity(G)\t\tPoisson's Ratio(v)\tUnit Weight(w)\n\t\t\t\t\tGPa\t\t\t\t\tGPa\t\t\t\t\t\t kN/m^3";
cout << "\n-----------------------------------------------------------------------------------------------------------------------------------------------\n";
// Display material properties using member function
brass.display();
cast_iron.display();
copper.display();
glass.display();
return 0;
}
Self Practice Program 3
Code:
#include <iostream>
using namespace std;
class Material {
private:
float mass;
float volume;
public:
void setValues(float m, float v) {
mass = m;
volume = v;
}
float calculateDensity() {
return mass / volume;
}
void displayDensity() {
float density = calculateDensity();
cout << "The density of the material is: " << density << " kg/m^3" << endl;
}
};
int main() {
float mass, volume;
cout << "Enter the mass of the material (in kilograms): ";
cin >> mass;
cout << "Enter the volume of the material (in cubic meters): ";
cin >> volume;
Material material;
material.setValues(mass, volume);
material.displayDensity();
return 0;
}
References
There may be some AI Generated content in this article used for demonstration purposes.