[go: up one dir, main page]

0% found this document useful (0 votes)
24 views24 pages

Lec#01

Uploaded by

ahadali.reach
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)
24 views24 pages

Lec#01

Uploaded by

ahadali.reach
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/ 24

OO Programming

Classes & Objects in


C++
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
The general form of a class declaration
Declaration
class class-name {
access-specifier
✔ public
private data and functions ✔ private
access-specifier: ✔ Protected
data and functions
✔ public - members are
access-specifier: accessible from outside the
data and functions class.
✔ private - members cannot
// ... be accessed from outside the
access-specifier: class
data and functions ✔ protected- It is needed only
when inheritance is involved
} object-list;
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: return 0;
// data member }
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
using namespace std; //using Directive
// Demo example
// defining a class
int main()
{
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;

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

void startEngine() {
// ...
}
};

int main() {
Car myCar;
Car friendCar;
// ...
}
Activity
Activity Title: Exploring Classes and
Objects with a Simple Calculator
Introduction:

In this activity, we're going to dive into the exciting world of


classes and objects in C++ by creating our very own
"Calculator" class. Classes and objects are fundamental
concepts in programming, and they play a crucial role in
organizing and encapsulating data and behavior in a
structured way.
Activity Details:
Activity Details:

Class Design:

You will design a class named Calculator.


This class will have two integer data members: num1 and num2.
It will also include member functions for performing basic arithmetic operations
(addition, subtraction, multiplication, and division) on these numbers.
Additionally, the class will have a function to display the result of each operation.
Object Creation:

You will create two instances (objects) of the Calculator class.


Each object will have different values for num1 and num2.
Display Results:

After each operation, you will call the appropriate member function to display
the results.
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
Understanding
A Constructors
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-Parameteriz
ed 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
#include<iostream> int main()

Constructor
using namespace std;
class emp {
private:
{
emp xx (234, 56789.90);
xx.disp();
system("pause");
int empid;
return 0;
float pay;
}
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();
}
Exampl
e
#include<iostream>
#include<iomanip>
using namespace std;
class Employee
int main()
{
Employee assistant;
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