OBJECT-ORIENTED PROGRAMMING (C++)
We are learning about the following things by this
example:
- Classes in C++
- Constructors
- Parameterized Constructors
- Generic Data Input
Task Example:
QUESTION:
A class named as ComplexNumber has a real part(double) and an imaginary part(double) as private attributes.
·
Create
2 constructors, First, Non parameterized Constructor, and second which takes two
arguments are used to initialize the real part and imaginary part (i.e., Parametrized
Constructor),
·
write proper setter and getter function.
·
Find
the Sum of two Complex No and Display It
ü Now find the errors in the above
code!
Solution
#include <iostream>
#include <string>
using namespace std;
class Cat
{
public:
string name;
double weight;
Cat()
{
name = '\0';
weight = 0.0;
}
Cat(string n, double w)
{
name = n;
weight = w;
}
void Setname(string n)
{
name = n;
}
void Set weight(double w)
{
weight = w;
}
string Getname()
{
return name;
}
double Getweight()
{
return weight;
}
void display()
{
cout << "\n\n\t --RESULT--\n";
cout << " Name of Cat: " << name << endl;
cout << " Weight of Cat: " << weight << endl<<endl;
}
};
int main()
{
Cat obj;
string a;
double b;
cout << "Enter Name of Cat: ";
cin >> a;
cout << "Enter weight of Cat: ";
cin >> b;
obj.Setname(a);
obj.Setweight(b);
obj.display();
system("pause");
return 0;
}
That's exactly what today's post is all about. We hope you'll like it. You can also message us to see more posts, and if you like them, be sure to share them with your friends. Thank you very much for reading.
-----------------------------------------------------------------------------------
0 Comments