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

Lecture-3-Objects and Classes Concept

The document discusses classes and objects in C++. It defines a class as a user-defined type that contains data members and member functions. It describes how to declare a class with access specifiers like public and private. It provides an example of a class definition with a data member and member functions. It describes how objects are instances of classes and can access both data members and member functions. It also discusses constructors and destructors, how they are used to initialize and destroy class objects, and provides examples of parameterized and non-parameterized constructors.

Uploaded by

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

Lecture-3-Objects and Classes Concept

The document discusses classes and objects in C++. It defines a class as a user-defined type that contains data members and member functions. It describes how to declare a class with access specifiers like public and private. It provides an example of a class definition with a data member and member functions. It describes how objects are instances of classes and can access both data members and member functions. It also discusses constructors and destructors, how they are used to initialize and destroy class objects, and provides examples of parameterized and non-parameterized constructors.

Uploaded by

Dihahs Kilam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

OO Programming

Classes & Objects in C++

DR. RAHAT H. BOKHARI


A Class and its Objects
Classes and Objects
 A Class is user-defined type

Data (data members)

Functions (member functions or methods)

 A class definition begins with the keyword class.


 The body of a class is contained within a set of braces { }
 Terminated by a (semi-colon) ;
Class Declaration
The general form of a class declaration
access-specifier
class class-name {  public
private data and functions
 private
access-specifier:
data and functions
 Protected
 public - members are
access-specifier: accessible from outside the
data and functions class.
// ...  private - members cannot
access-specifier: be accessed from outside the
class
data and functions  protected- It is needed only
} object-list; when inheritance is involved
Note: The object-list is optional
A Class in C++
How to declare the Class? Any valid identifier

class class_name
Body of Class {
……..
Data member + Method
……..
};
Classes in C++
Access specifier
Reflects the access level of the members of the class
 private: (It is default)
Usually the data members of a class are declared as private
public:
 The member functions are declared as public
private members / methods

public members/ methods


Syntax of Class Definition
Classes in C++
#include <iostream> //include Directive // main program
using namespace std; //using Directive int main()
// Demo example {
// defining a class Example d1;
class Example d1.set_age(45);
{ d1.disp_age();
private: system ("pause");
// data member return 0;
int age; }
public:
// member function
void set_age(int d)
{
age = d;
}
void disp_age()
{
cout << "Age is : " << age << endl;
}
};
Classes in C++
#include <iostream> //include Directive int main()
using namespace std; //using Directive
{
// Demo example
// defining a class Example d1;
class Example d1.set_age(45);
{
d1.disp_age();
private:
// data member getchar();
int age; return 0;
public:
}
// member function
void set_age(int d);
void disp_age();
};
void Example::set_age(int d)
{
age = d;
}
void Example:: disp_age()
{
cout << "Age is : "<<age<<endl;

}
Classes and Objects
An object is said to be an instance of a class

 In Demo example, d1 is instance of Example class mentioned in


main ( ).

The data member age within a class is private

The Functions such as set_age ( ), disp_age ( ) within a class are


public
“using” Directive
vs
“using” Declaration
using Directive using Declaration
#include <iostream> #include <iostream>
using namespace std; int main()
{
int main() /* this using declaration tells the compiler
{ that cout should resolve to std::cout */
cout << "Hello world!\n"; using std::cout;
cout << "Hello world!\n"; // so no std:: prefix
return 0; is needed here!
} return 0;
} // the using declaration expires here
Understanding Constructors
A constructor is a function that is called automatically each time an
object is created.

A constructor is used to initialize the objects of the class.

Each class can have, at most, one default constructor.

A default constructor is one that does not require any arguments.

A constructor cannot have a return type not even void.

 The return type of a constructor is the class type implicitly where it


belongs.
Understanding Constructors
You should not use the term void when you code a constructor.

Constructors are always written without a return type

A constructor is not considered as a member of the class.


Understanding Constructors
Constructors may be:
Non-Parameterized
A constructors that have not parameter/parameters is known as non-
parameterized constructor.
Non-Parameterized constructors are used to initialize the object with default
values or certain specific constants depending upon the programmer’s
needs.
Parameterized
A parameterized constructor is the one that has defined parameters and
arguments specified by the programmer and are sent to the constructor
during object creation to initialize the object.
 As a general rule, an object's constructor is called when the object comes
into existence.
Non-Parameterized
Constructor
#include<iostream> int main()
#include <iomanip> {
using namespace std; emp xx;
class emp { xx.getid();
private: xx.getpay();
int empid; xx.disp();
float pay; system("pause");
public: return 0;
//Non-parameterized/default constructor }
void getid()
{
cout<<"Enter Id ";
cin>>empid;
}
void getpay()
{
cout<<"\n\nEnter Pay ";
cin>>pay;
}
void disp()
{
cout<<"\n\n\nEmployee ID = "<<empid<<"\n\n"<<"Pay =
"<<fixed<<setprecision(2)<<pay<<endl;
}
};
Parameterized Constructor
#include<iostream> int main()
using namespace std; {
class emp { emp xx (234, 56789.90);
private: xx.disp();
int empid; system("pause");
float pay; return 0;
public: }
//parameterized constructor
emp(int a, float pp)
{
empid=a;
pay=pp;
}
void disp()
{
cout<<" Employee ID = "<<empid<<"\n\n"<<"Pay "<<pay<<endl;
}
};

Note: Define the constructor's body and use the parameters to


initialize the object.
Parameterized Constructor
#include<iostream>
using namespace std;
class RR
{
private:
int num, den;
public:
RR(int n, int d)
{
num = n;
den = d;
}
void display()
{
cout <<"num= "<<num << " den = "<<den<<endl;
}
};
int main()
{
RR x(-12,3), y(22,7);
cout<<"OutPut as Under "<<"\n\n";
x.display();
y.display();
getchar();
}
Example
#include<iostream> int main()
#include<iomanip> {
using namespace std; Employee assistant;
class Employee assistant.setIdNum(1234);
{ assistant. setHourlyRate(56.50);
private: assistant.display();
int idNum; system("pause");
double hourlyRate; return 0;
public: }
void setIdNum(int);
void setHourlyRate(double);
void display();
};
// Functions defined out of class definition. Resolution operator used
void Employee::setIdNum(int id)
{
idNum = id;
}
void Employee::setHourlyRate(double rate)
{
hourlyRate = rate;
}
void Employee::display()
{
cout << " \nEmployee ID " <<idNum<<endl;

cout << " \nHourly Rate "


<<fixed<<setprecision(2)<<hourlyRate<<endl;
}
Destructor
The destructor destroys objects that the Constructor has created
within a C++ program.

An object's destructor is called when the object is destroyed.

The name of the destructor is the same as the name of the class,

except that it has a tilde ( ~ ) before its name.


Destructor
#include<iostream>
using namespace std;
class my {
public:
int who;
my(int id);
~my();
};
my::my(int id)
{
cout << "Initializing " << id << "\n";
who = id;
}
my::~my()
{
cout << "Destructing " << who << "\n";
}
int main()
{
my ob1(3);
my ob2(6);
return 0;
}
Questions?

Thank you
Reference
•Lafore, R. ( 2002) Object-Oriented Programming in
C++,4th Ed. SAMS, USA
•Farrell, J. (2009) Object-Oriented Programming Using
C++, 4th Edition, Course Technology, USA.
•Schildt, H. (2003) C++: The Complete Reference, 4th Ed.
McGraw-Hill, USA

You might also like