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

Classes and Objects

Uploaded by

jummoxchangmha01
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)
22 views

Classes and Objects

Uploaded by

jummoxchangmha01
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

CSE 1203 (Object-oriented Programming)

Chapter 2
Classes & Objects in OOP
Class in OOP
What is a class?

Vehicle -> is a class University -> is a class


Car -> is object RUET -> is object
bus -> is object BUET -> is object
Truck -> is object Cambridge -> is object
Define a Class Type
Header class Rectangle
class class_name {
{ private:
permission_label:
int width;
member;
int length;
Body permission_label:
member; public:
... void set(int w, int l);
}; int area();
};
public - members are accessible from outside the class.
private - members cannot be accessed (or viewed) from outside the class. 3
Class in OOP: C++ example
#include <iostream> Question:
using namespace std; 1. What happens if private keyword is replaced by public?
2. What happens if public keyword is replaced by private?
class Rectangle{
private:
int height;
int width;
Data & Member Functions
public:
void SetData(int h,int w){
height=h;
width=w;
}
void Display(){
cout<<"Height="<<height<<" Weight="<<width;
}
};
int main()
{
Rectangle r1,r2;
r1.SetData(3,4);
r1.Display();
return 0;
}
Class in OOP: Data & Member Function
Data & Member Functions Example

Member Functions inside the Class


Class in OOP: Data & Member Function
Member Functions outside the Class Example

Scope resolution operator


Class in OOP: Setters & Getters
• Setters allow for a private variable to be modified.
• Getters give public access to private data.

Example: setters() Example: getters()

Write a complete C++ program


using these setters and getters
Class in OOP: Access Control
Access Specifiesrs public specifier

public specifier: Example


Class in OOP: Access Control
private specifier private specifier: Example

protected specifier protected specifier: Example


Class in OOP: Constructors
What is a constructor? Constructor: Initializaton

• constructor has no return type

Example
Class in OOP: Destructors
What is a destructor? Destructor: C++ Program
class Test{
public:
Test(){
cout<<"Constructor is called"<<endl;
}
~Test(){
cout<<"Destructor is called"<<endl;
}
};
void CreateObj(){
Example Test t1,t2;
cout<<"Inside the CreateObj()"<<endl;
}
int main()
{
CreateObj();
return 0;
}
Class in OOP: Class in Different File
What is a destructor? reg.h
#include <iostream> class Circle{
#include "reg.h“ private:
int r;
using namespace std; public:
void SetRadius(int x){
int main(){ r=x;
Circle c1; }
c1.SetRadius(3); float GetArea(){
cout<<c1.GetArea(); return(3.14*r*r);
} }
};
Class in OOP: static Data Member
What is a static data member? // This program counts no. of objects
#include <iostream>
Static data members are class members that are declared using namespace std;
using static keywords. A static member has certain class Base{
special characteristics which are as follows: int x;
static int y;
• Only one copy of that member is created for the public:
entire class and is shared by all the objects of that Base(int X){
class, no matter how many objects are created. x=X;
• It is initialized before any object of this class is y++;
created, even before the main starts. }
• It is visible only within the class, but its lifetime is the static int getY(){ return y;}
entire program. };
• The value must be initialized outside the class int Base::y=0;
• The getter function must be static int main()
Syntax: {
Base c1(10),c2(20);
static data_type data_member_name; cout<<Base::getY();
return 0;
}
Class in OOP: Passing Object as Parameter
Example Contd..
#include <iostream> int main()
using namespace std; {
Ball b1(130),b2(140);
class Ball{
b1.AvgSpeed(&b2);
private:
int s; return 0;
public: }
Ball(){}
Ball(int x){ b
s=x;
} s=130 s=140
void AvgSpeed(Ball *b){
cout<<(s+b->s)/2;
}
};
b1 b2
Class in OOP: Object as Function Return
Example Contd..
#include <iostream> int main()
using namespace std; {
class Ball{ Ball b1(130),b2(140);
private: Ball k;
float s; k=b1.AvgSpeed(&b2);
public: cout<<k.GetSpeed()/2;
Ball(){} return 0;
Ball(int x){ }
s=x;
}
float GetSpeed (){ s=130 s=140 s=170 s=170
return s;
}
Ball AvgSpeed(Ball *b){
Ball t;
t.s=s+b->s; b1 b2 t k
return(t); b
}
};
Class in OOP: Copy Constructor
What is a Copy Constructor?
A copy constructor is a member function that initializes an object using another object of the same
class. In simple terms, a constructor which creates an object by initializing it with an object of the same
class, which has been created previously is known as a copy constructor.
Copy constructor is used to initialize the members of a newly created object by copying the members of
an already existing object.

Copy constructor takes a reference to an object of the same class as an argument.

Sample(Sample &t) { int main() { int id


id=t.id; Sample s1(100);
} Sample s2(s1);
In Sample class }
In main()( Sample Class
Class in OOP: Copy Constructor Example
Example Contd..
#include <iostream> int main()
using namespace std; {
Ball b1(150); Copy constructor
class Ball{ Ball b2(&b1);
b2(b1);
private: cout<<b2.GetSpeed();
float s; return 0;
public: }
Ball(){}
Ball(int x){
s=x;
}
Copy constructor
s=150 s=150
Ball(Ball &b){
s=b.s;
}
float GetSpeed (){
return s; b1 b2
} b
};
Class in OOP: const Member Function
const member function
declaration
return_type func_name (para_list) const;
definition
return_type func_name (para_list) const { … }
return_type class_name :: func_name (para_list) const { … }
• Makes no modification about the data members
(safe function)
• It is illegal for a const member function to
modify a class data member
Class in OOP: const Member Function
class Base{
mutable int x;
public: function declaration
void setX(int a){ x=a;}
int getX()const {
x++;
return x;} Data Member can't be changed

};

To make const function executable making data member mutable


Class in OOP: static Member Function
• static member function
– Static member function can contain only static data
member
– Non-static Static member function can contain both static
and non-static data member
– Static function can run using
<classname>::<static function()
Class in OOP: static Member Function
class Base{ int main()
int x; {
static int y; Base c1(10),c2(20);
public:
Base(int X){ cout<<Base::getY();
x=X; return 0;
y++; }
}
static int getY(){ return y;}
}; Static method can be called
by class name with scope
int Base::y=0; resolution
Class in OOP: friend Member Function
Class in OOP: friend Function Example_01
#include <iostream> int main()
using namespace std; {
Test p(10);
class Test{ show(p);
private: return 0;
int n; }
public:
Test(int x){
n=x;
}
friend void show(Test t);
};
void show(Test t){
cout<<"n="<<t.n;
}
Class in OOP: friend Function Example_02
#include <iostream> class B{ int main()
using namespace std; private: {
int n; A oa(10);
class B; //forwrad declaration public: B ob(20);
class A{ B(int x){ Add(oa,ob);
private: n=x; return 0;
int n; } }
public: friend void Add(A,B);
A(int x){ };
n=x; void Add(A a,B b){
} cout<<"Sum="<<a.n+b.n;
friend void Add(A,B); }
};
Here private members of different classes are added
using friend function
Introduction to OOP

End

You might also like