0% found this document useful (0 votes)
11 views7 pages

SDWU-Session 2

Uploaded by

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

SDWU-Session 2

Uploaded by

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

OBJECT ORIENTED DESIGN AND PROGRAMMING

Feature : Objects and Classes


Objects and Classes
•Class is a user defined data type, which holds its own data members and
member functions, which can be accessed and used by creating instance of
that class.

–Attributes – member of a class.


• An attribute is the data defined in a class that maintains the
current state of an object. The state of an object is
determined by the current contents of all the attributes.

–Methods – member functions of a class

2
Objects and classes
• A class itself does not exist; it is merely a description of an object.
• A class can be considered a template for the creation of object

• Blueprint of a building  class


• Building  object

• An object exists and is definable.


• An object exhibits behavior, maintains state, and has traits. An object can
be manipulated.
• An object is an instance of a class.

3
Methods
• A method is a function that is part of the class definition. The methods of a
class specify how its objects will respond to any particular message.

• A method is a collection of statements that perform some specific task and


return the result to the caller. A method can perform some specific task
without returning anything.

• Methods allow us to reuse the code without retyping the code.

• A message is a request, sent to an object, that activates a method (i.e., a


member function).

4
Defining a Base Class - Example
•A base class is not defined on, nor does it inherit members from, any
other class.

#include <iostream>
using std::cout; //this example “uses” only the necessary
using std::endl; // objects, not the entire std namespace

class Fraction {
public:
void assign (int, int); //member functions
double convert();
void invert();
void print();
private:
int num, den; //member data
};
Continued…
5
Using objects of a Class - Example
The main() function:

int main()
{
Fraction x;
x.assign (22, 7);
cout << "x = "; x.print();
cout << " = " << x.convert() << endl;
x.invert();
cout << "1/x = "; x.print(); cout << endl;
return 0;
}

Continued…
6
Defining a Class – Example
Class Implementation:

void Fraction::assign (int numerator, int denominator)


{
num = numerator;
den = denominator;
}

double Fraction::convert ()
{
return double (num)/(den);
}

void Fraction::invert()
{
int temp = num;
num = den;
den = temp;
}

void Fraction::print()
{
7
cout << num << '/' <

You might also like