OOP_Lecture_3(Class and Object)
OOP_Lecture_3(Class and Object)
--------------------------------------
1: Programming Paradigm
1: Imperative Paradigm
a: Structural Paradigm
b: Object Oriented Paradigm
2: Descriptive Paradigm
a: Functional Paradigm
b: Logical Paradigm
--------------------------------------
2: How to define Class
struct StructName
{
member1;
member2;
.......
memberN;
};
class ClassName
{
member1;
member2;
.......
memberN;
Method1();
Method2();
....
MethodN();
};
class Person
{
string name;
int age;
float height;
void displayInfo()
{
cout<<"Name: " <<name<<" Age: "<<age<<" Height: "<<height<<endl;
}
};
------------------------------
3: Create variable(object/instance)
DataType NameVar;
int a;
struct student
{
string name;
int age;
};
student s1;
s1.name="Ali";
s1.age=20;
Person p1;
--------------------------------
4: Access Modifiers
a): Private // Attribute/Methods define under private access modifier will
not
access outside the class.
b): Public // Attribute/Methods define under public access modifier will be
accessible outside the class through object of that class.
c): Protected // Discuss in detail at Inheritance
----------------------------------
5: Built-in Operator
a) dot/period (.)
b) assignment(=)
p2=p1;
----------------------------------
6: Add getter setter methods in class
-------------------------------------
Example 2:
class Clock{
private:
int hr, min , sec;
public:
void setHr(int h)
{
if(h>=0 && h<24 )
hr=h;
else
hr=0;
}
void setMin(int m)
{
if(m>=0 && m<60 )
min=m;
else
min=0;
}
void setSec(int s)
{
if(s>=0 && s<60 )
sec=s;
else
sec=0;
}
int getHr()
{
return hr;
}
int getMin()
{
return min;
}
int getSec()
{
return sec;
}
void incHr()
{
hr++;
if(hr>23)
hr=0;
}
void incMin()
{
min++;
if(min>59)
{min=0;
incHr();
}
}
void incSec()
{
sec++;
if(sec>59)
{sec=0;
incMin();
}
}
void DisplayTime()
{
cout<<"Current Time is: " <<hr<< ":"<<min<<":"<<sec<<" (HH:MM:SS)"<<endl;
}
};