0% found this document useful (0 votes)
16 views

8.classes & Objects

The document discusses object-oriented concepts in C++ like creating classes, defining functions inside and outside classes, creating objects, accessing data members, arrays of objects, passing objects as function arguments, and returning objects from functions.

Uploaded by

Shaunak Sardesai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

8.classes & Objects

The document discusses object-oriented concepts in C++ like creating classes, defining functions inside and outside classes, creating objects, accessing data members, arrays of objects, passing objects as function arguments, and returning objects from functions.

Uploaded by

Shaunak Sardesai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

OBJECT-ORIENTED CONCEPTS

Rohan R Kerkar
Object-Oriented Concepts
• Creating Class

• Function definition – Inside & Outside class

• Creating Objects

• Accessing data members

• Array of Objects

• Passing Objects as Function argument

• Returning Object from a Function


Object-Oriented Concepts
Class:
• A Class is a user defined data-type which has
data members and member functions.

• A class definition starts with the keyword


class followed by the class name; and the
class body, enclosed by a pair of curly braces.

• A class definition must be followed either by


a semicolon or a list of declarations.
Class Definition
#include <iostream>
#include <string>
using namespace std;

class MyClass // The class


{
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; //Attribute (string variable)
};
int main()
{
MyClass myObj; //Create an object of MyClass
// Access attributes and set values
myObj.myNum = 25;
myObj.myString = " Hello World ";
Class Definition
// Print values
cout << myObj.myNum << "\n“;
cout << myObj.myString;
return 0;
}
Function Definition – Inside & Outside the class
• A function is a set of statements that take inputs, do some specific
computation and produces output.

• The idea is to put some commonly or repeatedly done task together


and make a function so that instead of writing the same code again
and again for different inputs, we can call the function.

• The general form of a function is:


• return_type function_name([ arg1_type arg1_name, ... ]) { code }

int max(int x, int y)


{
if (x > y)
return x;
else
return y;
}
Function Definition – Inside & Outside the class
#include <iostream>
#include <string>
using namespace std;

class MyClass
{
public:
int myNum;
string myString;
void setData(int num, string str)
{ //Function definition inside the class
myNum = num;
myString = str;
}
void printData();
};
Function Definition – Inside & Outside the class
void MyClass::printData()
{ //Function definition outside the class
cout <<myNum << "\n";
cout <<myString;
}

int main()
{
MyClass myObj;
myObj. setData(5, "Hello World");
myObj. printData();
}
Object-Oriented Concepts

Objects:
• An Object is an instance of a Class.

• When a class is defined, no memory is


allocated but when it is instantiated (i.e. an
object is created) memory is allocated.
Creating Objects
#include <iostream>
#include <string>
using namespace std;

class Car { //Creating Class Car


public:
string brand;
string model;
int year;
};
int main()
{
Car carObj1; //Creating object of Class Car
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
Creating Objects

Car carObj2; //Creating object of Class Car


carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;
//Printing the data members of the objects

cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
Accessing Data Members

• The public data members of objects of a class can be


accessed using the direct member access operator . (dot).

#include <iostream>
using namespace std;

class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
Accessing Data Members
int main()
{
Box Box1; // Object Box1 of type Box
Box Box2; // Object Box2 of type Box
double volume = 0.0; // Store the volume of a box here

// Accessing data members of Box1


Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;

// Accessing data members of Box2


Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
Accessing Data Members
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;

// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}
Array of Objects
• You can store objects of user defined datatype in a C++ Array.

• To store objects of user defined datatype in an array, you can declare


an array of the specific type and initialize the array just like an array
of int.

• ClassName ObjectName[number of objects];

• The Array of Objects stores objects. An array of a class type is also


known as an array of objects.
Array of Objects
#include <iostream>
#include <string>
using namespace std;

class Car {
public:
string brand;
string model;
int year;

void getData( string brd, string mdl, int yr)


{
brand = brd;
model = mdl;
year = yr;
}
Array of Objects
void printData()
{
cout <<brand << " " <<model << " " <<year << "\n";
} };
int main() {
Car carObj[3]; //Array of Object Car ; size = 3

carObj[0].getData("BMW","X5",1999);
carObj[1].getData("Ford","Mustang",1969);
carObj[2].getData("Hyundai","Verna",2012);

carObj[0].printData();
carObj[1].printData();
carObj[2].printData();
return 0;
}
Passing Objects as Function argument
• In C++ programming language, we can pass an object as an argument
within the member function of class.

• This is useful, when we want to initialize all data members of an


object with another object, we can pass objects and assign the values
of supplied object to the current object.

• To pass an object as an argument we write the object name as the


argument while calling the function the same way we do it for other
variables.
function_name(object_name);
Passing Objects as Function argument
#include <iostream>
using namespace std;

class Addition
{
public:
int num;

void setData(int n)
{
num = n;
}
Passing Objects as Function argument
void sum(Addition ob1, Addition ob2)
{
num = ob1.num + ob2.num;
}

void printData()
{
cout<<"Value of num: "<<num<<endl;
}
};
Passing Objects as Function argument
int main()
{
//Creating Objects
Addition obj1;
Addition obj2;
Addition obj3;

//Assigning values to the data member of objects


obj1.setData(10);
obj2.setData(20);

//passing object obj1 and obj2 as an argument/paremeter


obj3.sum(obj1,obj2);
Passing Objects as Function argument
//printing the values
obj1.printData();
obj2.printData();
obj3.printData();

return 0;
}
Returning Object from a Function

• In C++ programming language, we can return an object from


member function.

#include <iostream>
using namespace std;
class Addition
{
public:
int num;

void setData(int n)
{
num = n;
}
Returning Object from a Function
//Function accepts objects as parameters and return object as result
Addition sum(Addition ob1, Addition ob2)
{
Addition result;
result.num = ob1.num + ob2.num;
return result;
}

void printData()
{
cout<<"Value of num: "<<num<<endl;
}
};
Returning Object from a Function
int main()
{
//object declarations
Addition obj1;
Addition obj2;
Addition obj3;

//assigning values to the data member of objects


obj1.setData(10);
obj2.setData(20);

//passing object obj1 and obj2 and returning object


obj3 = obj3.sum(obj1,obj2);
Returning Object from a Function
//printing the values
obj1.printData();
obj2.printData();
//printig
obj3.printData();

return 0;
}

You might also like