OOP LAB JOURNAL OBJECT ORIENTED PROGRAMMING, Exercises of Cooperative Governance

I have done these oop,lab journals in my last semester which includes questions that might help students

Typology: Exercises

2020/2021

Uploaded on 04/16/2022

maliksharjeel00
maliksharjeel00 🇵🇰

1 document

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Like any other function, a constructor can also be overloaded with more than one function that have
the same name but different types or number of parameters. Remember that for overloaded
functions the compiler will call the one whose parameters match the arguments used in the function
call. In the case of constructors, which are automatically called when an object is created, the one
executed is the one that matches the arguments passed on the object declaration:
In this case, rectb was declared without any arguments, so it has been initialized with the
constructor that has no parameters, which initializes both width and height with a value of 5.
Important: Notice how if we declare a new object and we want to use its default constructor (the
one without parameters), we do not include parentheses ():
Output:
#include<iostream>
using namespace std;
class Crectangle
{
private:
int width, height;
public:
Crectangle()
{
width = 5;
height = 5;
}
Crectangle(int a,int b)
{
width = a;
height = b;
}
void rect_area()
{
int area = width * height;
cout << "Rect Area : " << area;
}
void rectb_area()
{
int areab = width * height;
cout << "\nRectb Area : " << areab;
}
};
int main()
{
Crectangle rect(9, 7);
Crectangle rectb;
rect.rect_area();
rectb.rectb_area();
}

Partial preview of the text

Download OOP LAB JOURNAL OBJECT ORIENTED PROGRAMMING and more Exercises Cooperative Governance in PDF only on Docsity!

Like any other function, a constructor can also be overloaded with more than one function that have

the same name but different types or number of parameters. Remember that for overloaded

functions the compiler will call the one whose parameters match the arguments used in the function

call. In the case of constructors, which are automatically called when an object is created, the one

executed is the one that matches the arguments passed on the object declaration:

In this case, rectb was declared without any arguments, so it has been initialized with the

constructor that has no parameters, which initializes both width and height with a value of 5.

Important: Notice how if we declare a new object and we want to use its default constructor (the

one without parameters), we do not include parentheses ():

Output:

#include using namespace std; class Crectangle { private: int width, height; public: Crectangle() { width = 5; height = 5; } Crectangle(int a,int b) { width = a; height = b; } void rect_area() { int area = width * height; cout << "Rect Area : " << area; } void rectb_area() { int areab = width * height; cout << "\nRectb Area : " << areab; } }; int main() { Crectangle rect(9, 7); Crectangle rectb; rect.rect_area(); rectb.rectb_area(); }