Lect 5 - Introduction To Classes and Objects
Lect 5 - Introduction To Classes and Objects
OOP makes it easy to maintain and modify existing code as new objects
can be created with small differences to existing ones.
OOP provides a good framework for code libraries where supplied
software components can be easily adapted and modified by the
programmer.
ADVANTAGES
Object-oriented programming has several advantages over procedural
programming:
OOP is faster and easier to execute.
OOP provides a clear structure for the programs.
OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes
the code easier to maintain, modify and debug.
OOP makes it possible to create full reusable applications with less code and
shorter development time.
Objects are modelled on real world entities.
It provides data hiding .
It provides data encapsulation.
CLASS IN C++
Class is just a blue print, containing only list of variables and methods
(functions) where no memory is allocated to them.
A class is a group of objects that has common properties.
Classes are expanded version of structures.
Structures can contain only multiple variables.
Classes can contain multiple variables, classes can also contain functions
as class member
In other words , they are functions + structures with some differences.
CLASS IN C++
Encapsulates data(attributes) and functions(behavior) into packages called
classes .
Class is a place where we can define the properties and functionalities of the
objects.
Variables declared in class are called data members.
Functions declared or defined in class are called member functions.
The instance of class are called object.
So classes are the user defined (programmer defined) types having
Variables( data members)
Functions ( member functions )
CLASS DECLARATION
Declaration of class must start with the keyword class followed by the
class name.
Members of a class are declared within braces.
A terminator (;) must be inserted after the closing bracket.
class class_name
{
// data members
// member functions
};
EXAMPLE
class Employee
{
private:
char name[25];
int age;
long salary;
public:
void getdata();
void putdata();
};
EXAMPLE
class Employee
{
private:
char name[25];
int age;
long salary; Body of the
public: class
void getdata();
void putdata();
};
EXAMPLE
class Employee
{
Class is a
private:
keyword
char name[25];
int age;
long salary;
public:
void getdata();
void putdata();
};
EXAMPLE
class Employee
{
Class name
private:
char name[25];
int age;
long salary;
public:
void getdata();
void putdata();
};
EXAMPLE
class Employee
{
Starting
private:
class scope
char name[25];
int age;
long salary;
public:
void getdata();
void putdata();
};
EXAMPLE
class Employee
{
Access
private:
specifier
char name[25];
int age;
long salary;
public:
void getdata();
void putdata();
};
EXAMPLE
class Employee
{
private:
char name[25];
int age;
Private data
long salary;
members of
public: class
void getdata();
void putdata();
};
EXAMPLE
class Employee
{
private:
char name[25];
int age;
long salary;
public:
Access
void getdata(); specifier
void putdata();
};
EXAMPLE
class Employee
{
private:
char name[25];
int age;
long salary;
public:
void getdata(); Public member
void putdata(); functions of
}; class
EXAMPLE
class Employee
{
private:
char name[25];
int age;
long salary;
public:
void getdata(); Semicolon
void putdata(); and end of
}; class scope
CLASSES (POINTS TO REMEMBER)
If a member of a class is a variable, you declare it just like any other variable.
Also, in C++ versions prior to C++11, in the definition of the class, you
cannot initialize a variable when you declare it.
If a member of a class is a function, you typically use the function prototype to
declare that member.
If a member of a class is a function, it can (directly) access any member of the
class—member variables and member functions.
when you write the definition of a member function, you can directly access any
member variable of the class without passing it as a parameter.
The only condition is that you must declare an identifier before you can use it.
OBJECTS
An Object is an identifiable entity with some characteristics and
behavior.
An Object is an instance of a Class. When a class is defined, no memory
is allocated but when it is instantiated (i.e. an object is created) memory
is allocated.
Object is identified by its unique name.
Object take up space in memory and have an associated address like a
record in pascal or structure or union in C.
When a program is executed the objects interact by sending messages
to one another.
OBJECTS (CONT…)
There are two ways in which the member functions can be defined
Inside the class definition
Outside the class definition
INSIDE CLASS DEFINITION
As the name suggests, here the functions are defined inside the class.
Functions defined inside the class are treated as inline functions
automatically if the function definition doesn’t contain looping
statements or complex multiple line operations.
You can access methods (functions) just like you access attributes by
creating an object of the class and by using the dot syntax(.).
EXAMPLE
#include <iostream> cin>> car_model;
using namespace std; }
class car void showdata()
{ {
private: cout<<“car number is <<“car_number<<endl;
int car_number ; cin<<“car model is”<<car_model<<endl;
char car_model[10] }};
public: int main()
void getdata() { car c1;
{ c1.getdata();
cout<<“enter car number”<<endl; c1.showdata(); Function definition
cin>> car_number; system(“pause”); inside the class
cout<<“enter car model”<<endl; return 0; }
OUTSIDE CLASS DEFINITION
As the name suggests, here the functions are defined outside the class
however, they are declared inside the class.
Functions should be declared inside the class to bound it to the class and
indicate it as it’s member but they can be defined outside of the class.
To define a function outside of a class, scope resolution operator :: is
used.
This is done by specifying data type than the name of the class ,
followed the scope resolution :: operator , followed by the name of
the function.
OUTPUT:
Hello World!
EXAMPLE
class MyClass
{ // The class
public: // Access specifier
void myMethod(); // function declaration
};
void MyClass::myMethod() //function definition outside the class
{
cout << "Hello World!";
}
int main()
{
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
CONSTRUCTOR
class Car // The class // Create Car objects and call the constructor with different values
{ int main()
public: // Access specifier {
string brand; // Attribute Car carObj1("BMW", "X5", 1999);
string model; // Attribute Car carObj2("Ford", "Mustang", 1969);
int year; // Attribute // Print values
// Constructor with parameters cout << carObj1.brand << " " <<
Car(string x, string y, int z) carObj1.model << " " << carObj1.year << "\n";
{ brand = x;
cout << carObj2.brand << " " <<
model = y;
year = z; carObj2.model << " " << carObj2.year << "\n";
} return 0;
}; }
CONSTRUCTOR OUTSIDE THE CLASS
Just like functions, constructors can also be defined outside the class.
First, declare the constructor inside the class, and then define it outside
of the class by specifying the name of the class, followed by the scope
resolution :: operator, followed by the name of the constructor (which is
the same as the class):
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z); // Constructor declaration
};
Car::Car(string x, string y, int z) // Constructor definition outside the class
{ brand = x;
model = y;
year = z; }
int main()
{ // Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0; }
PRACTICE
Public means that the data members and member functions can be
accessed by other classes too
If the members are public it means that it can be accessed and
modified outside the class
Syntax
class Publicaccess
{
public: // public access specifier
int x; // data member declaration
void display(); //member function declaration
};
PROTECTED