C++ CLASSES AND OBJECTS, Constructors Update
C++ CLASSES AND OBJECTS, Constructors Update
int main() {
MyClass myObj; // Create an object of MyClass
• Example
class MyClass { // The class
public: // Access specifier
void Display()
{ // Method/function defined inside the class
cout << “WELCOME TO OOP PROGRAMMING!";
}
};
int main() {
MyClass myObj; // Create an object of MyClass
myObj.Display(); // Call the method
return 0;
}
Outside class definition
CSC 221: Object Oriented Introduction to Classes and Objects, Constructors and
14
Programming(C++) destructors
#include<iostream>
using namespace std;
class PATTERN
{
public:
void pattern()
{
int col,row;
for (col=1;col<=7;col++)
{
for (row=col;row<=7;row++)
{
cout<<"*"<<"\t";
}
cout<<endl;
}
}
};
int main()
{
PATTERN p;
p.pattern();
} 15
#include<iostream>
using namespace std;
int main()
class PATTERN
{
{
PATTERN p;
int i,j;
p.set_val(7,7);
public:
p.pattern();
void set_val(int a, int b)
}
{
i=a;
j=b;
}
void pattern()
{
int col,row;
for (col=1;col<=i;col++)
{
for (row=col;row<=j;row++)
{
cout<<"*"<<"\t";
}
cout<<endl;
}
}
}; 16
#include<iostream> int main()
using namespace std; {
class PATTERN PATTERN p;
{ p.i=7;
public: p.j=7;
int i,j; p.pattern();
}
void pattern()
{
int col,row;
for (col=1;col<=i;col++)
{
for (row=col;row<=j;row++)
{
cout<<"*"<<"\t";
}
cout<<endl;
}
}
};
7/1/2022 17
• A program to calculate area of a rectangle with dimensions 3 and 4
CSC 221: Object Oriented Introduction to Classes and Objects, Constructors and
18
Programming(C++) destructors
EXAMPLE int main () {
// classes example
Rectangle rect;
#include <iostream>
rect.set_values (3,4);
using namespace std;
cout << "area: " << rect.area();
class Rectangle {
return 0;
int width, height;
}
public:
void set_values (int x, int y) {
width = x;
height = y;
}
int area()
{
return width*height;
}
};
EXAMPLE
void Rectangle::set_values (int x,
// classes example int y) {
#include <iostream> width = x;
using namespace std; height = y;
class Rectangle { }
int width, height; int Rectangle:: area()
public: {
void set_values (int,int); return width*height;
int area(); }
}; int main () {
Rectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
EXERCISES
}
float area()
{
return (b*h)/2;
}
};
23
#include <iostream>
using namespace std; int main()
class Triangle {
{ Triangle t;
int b; t.sides(3,4,5);
int h; cout<<"The Area="<<t.area()<<endl;
int l; cout<<"The perimeter="<<t.peri()<<endl;
Public: return 0;
void sides(int s1, int s2, int s3) }
{
b=s1;
h=s2;
l=s3;
}
float peri()
{
return b+h+l;
}
float area()
{
return (b*h)/2;
}
};
24
#include <iostream>
using namespace std;
class Triangle
{ int main()
public: {
int b; Triangle t;
int h; t.sides(3,4,5);
int l; cout<<"The Area="<<t.area()<<endl;
void sides(int s1, int s2, int s3);
cout<<"The perimeter="<<t.peri()<<endl;
float peri(); return 0;
}
float area();
};
void Triangle :: sides(int s1, int s2, int s3)
{
b=s1;
h=s2;
l=s3;
}
float Triangle :: peri()
{
return b+h+l;
}
float Triangle :: area()
{
return (b*h)/2;
}
25