0% found this document useful (0 votes)
112 views25 pages

C++ CLASSES AND OBJECTS, Constructors Update

The document discusses classes and objects in C++. It defines a class as a blueprint for creating objects with attributes and methods. A class is defined using the class keyword followed by the class name. Objects are instantiated from classes and can access attributes using dot notation. Methods are functions defined within the class that act on the object's data. The document provides examples of defining classes, creating objects, and calling methods.

Uploaded by

shaddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views25 pages

C++ CLASSES AND OBJECTS, Constructors Update

The document discusses classes and objects in C++. It defines a class as a blueprint for creating objects with attributes and methods. A class is defined using the class keyword followed by the class name. Objects are instantiated from classes and can access attributes using dot notation. Methods are functions defined within the class that act on the object's data. The document provides examples of defining classes, creating objects, and calling methods.

Uploaded by

shaddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Classes and Objects

Class Definitions and Objects


Member Functions
Data Members
Get and Set functions
Constructors

Introduction to Classes and Objects, Constructors and


1
destructors
C++ Classes/Objects

C++ is an object-oriented programming language.


Everything in C++ is associated with classes and objects, along with
its attributes and methods.
 For example: in real life, a car is an object. The car has attributes,
such as weight and color, and methods, such as drive and brake.
Attributes and methods are basically variables and functions that
belongs to the class. These are often referred to as "class members".
A class is a user-defined data type that we can use in our program,
and it works as an object constructor, or a "blueprint" for creating
objects.

Introduction to Classes and Objects, Constructors and destructors2


C++ Classes/Objects

•C++ supports object-oriented (OO) style of


programming which allows you to divide
complex problems into smaller sets by
creating objects.
•Object is simply a collection of data and
functions that act on those data.
Create a Class

Before you create an object in C++,


you need to define a class.
A class is a blueprint for the object.
How to define a class in C++?

• A class is defined in C++ using keyword class followed by


the name of class.
• The body of class is defined inside the curly brackets and
terminated by a semicolon at the end.
class className
{
// some data
// some functions
};
Create a Class
Example • Example explained
The class keyword is used to
Create a class called "MyClass": create a class called MyClass.
The public keyword is an access
specifier, which specifies that
class MyClass members (attributes and
methods) of the class are
{ // The class accessible from outside the class.
public: // Access specifier You will learn more about access
specifiers later.
int myNum; // Attribute Inside the class, there is an
string myString; // Attribute integer variable myNum and a
string variable myString. When
}; variables are declared within a
class, they are called attributes.
At last, end the class definition
with a semicolon ;.
Create an Object
• In C++, an object is created from a class.
• We have already created the class named MyClass, so now we can use
this to create objects.
• To create an object of MyClass, specify the class name, followed by
the object name.
• Syntax to Define Object in C++
className objectVariableName;
• To access the class attributes (myNum and
myString), use the dot syntax (.) on the object:
Create an Object
Example
Create an object called "myObj" and access the attributes:
class MyClass {
public:
int myNum;
string myString;
};

int main() {
MyClass myObj; // Create an object of MyClass

// Access attributes and set values


myObj.myNum = 15;
myObj.myString = “Welcome to OOP";

// Print attribute values


cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}
Multiple Objects
• You can create multiple objects of one class:
• Example
• // Create a Car class with some attributes
class Car {
public:
string brand;
string model;
int year;
};
int main() {
// Create an object of Car
Car Obj1;
Obj1.brand = “Mercedes";
Obj1.model = “E class";
Obj1.year = 2019;
// Create another object of Car
Car Obj2;
Obj2.brand = “Toyota";
Obj2.model = “Fielder";
Obj2.year = 2015;
// Print attribute values
cout << Obj1.brand << " " << Obj1.model << " " <<
Obj1.year << "\n";
cout << Obj2.brand << " " << Obj2.model << " " <<
Obj2.year << "\n";
return 0;
}
C++ Class Methods

Methods are functions that belongs to the


class.
There are two ways to define functions that
belongs to a class:
Inside class definition
Outside class definition
Note: You access methods just like you
access attributes; by creating an object of the
class and by using the dot syntax (.):
Inside class definition

• 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

• class MyClass { // The class


public: // Access specifier
void Display(); // Method/function
declaration
};
// Method/function definition outside the class
void MyClass::Display() {
cout << " WELCOME TO OOP PROGRAMMING!";
}
int main() {
MyClass myObj; // Create an object of MyClass
myObj.Display(); // Call the method
return 0;
}
#include <iostream>
using namespace std;
class Car {
public:
int speed(int maxSpeed);
};
int Car::speed(int maxSpeed) {
return maxSpeed;
}
int main() {
Car myObj; // Create an object of Car
cout << myObj.speed(200); // Call the method with an
argument
return 0;
}
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*

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

• Write a program to print the area and perimeter of a triangle having


sides of 3, 4 and 5 units by creating a class named 'Triangle' with a
function to print the area and perimeter.
• Write a C++ program that will implement a class named sphere. The
program should accept the radius and determine the volume. The
program should then output the volume. Use pie as 3.14.
volume=4/3πr3.
SOLUTION…
#include <iostream>
using namespace std;
class Triangle
{
public:
void print_area(int s1, int s2, int s3)
{
float s = (s1*s2)/2.0;
float p = s1+s2+s3;
cout <<“The Area=” << s << endl;
cout << "Perimeter is " <<p << endl;
}
};
int main()
{
Triangle t;
t.print_area(3,4,5);
return 0;
}
#include <iostream>
using namespace std; int main()
class Triangle {
{ Triangle t;
public: t.b=3;
int b; t.h=4;
int h; t.l=5;
int l; cout<<"The Area="<<t.area()<<endl;
cout<<"The perimeter="<<t.peri()<<endl;
float peri() return 0;
{ }
return b+h+l;

}
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

You might also like