Professional Documents
Culture Documents
Programming
EC-204
Instructor
Ms. Kanwal Naveed
}
void showdata()
{cout << "Data is " << length << endl;
}
};
semicolon
Example: Step 1
class shape Class name
{private: Hidden data
keyword int length;
public: Public functions Set hidden variable
void setdata(int l) length equal to
{length = l; external variable l
}
void showdata()
{cout << "Data is " << length << endl;
}
Display value of hidden
}; variable length
semicolon
Objects
To use a class in a C++ program, its objects
must be instantiated
Similar to declaring a variable
name of the object
int main()
{shape line; An object of the class
shape
line1.setdata(5);
line1.showdata();
line2.setdata(4);
line2.showdata();
}
Example 2
Write a class called Area with data member as
length and width. Write three member
functions
getdata: get the value of length and width from the
user
showdata: displays the area of the shape
Declare two objects called square and
rectangle and display their areas.
class Area
{private:
int length, width, area;
public:
void getdata()
{cout << Enter length; cin >> length;
cout << Enter width; cin >> width;
}
void showarea()
{cout << Area is " << length*width;
}
};
int main()
{
Area Square, Rectangle;
Square.getdata();
Rectangle.getdata();
Square.showarea();
Rectangle.showarea();
}
Four Important Properties
Encapsulation
Inheritance
Re-usability
Polymorphism and overloading
Encapsulation
Member functions in C++, typically provide the only way to
access the data items in a class.
To read a data item in an object, its member function in the
object is called. It accesses the data and returns the value.
The data cannot be accessed directly. Thus data and its
function are said to be Encapsulated into a single entity.
Encapsulation leads to Data Hiding
The data is hidden to keep it safe from accidental alteration.
This techniques is known as Data Hiding.
To modify the data in an object, you need to access the
member functions in the object thus simplifying writing,
debugging, and maintaining the program.
Inheritance
The idea of classes and subclasses.
Derive other classes from an existing class
Each class shares common characteristics with
the class from which it was derived, and can
also add its own modifications, additions.
For instance, VEHICLE is a class from which
CAR, TRUCK, BUS, MOTORCYCLE classes
can be derived.
Inheritance
The original class is called the BASE CLASS;
the others are DERIVED CLASSES
Re-usability
Language extensibility (create new data types)
Reusability
Once a class has been written, created, and
debugged, it can be distributed to other
programmers for use in their own programs.
This is called reusability.
Inheritance: A programmer can take an
existing class and, without modifying it, add
additional features and capabilities to it. This is
done by deriving a new class from the existing
one.
Polymorphism and Overloading
Using operators or functions in different ways
depending on what they are operating on is
called polymorphism.
4 + 5 <-- integer addition
3.14 + 2.0 <-- floating point addition
s1 + "bar" <-- string concatenation!