






































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A chapter from a c++ programming textbook that introduces the concepts of classes, objects, member functions, data members, constructors, and member function parameters. It includes examples and explanations of how to define and use classes, as well as an overview of uml diagrams and their relevance to c++ programming.
Typology: Slides
1 / 78
This page cannot be seen from the preview
Don't miss anything!







































































Introduction
Classes, Objects, Member Functions and Data Members and Constructor
Overview of the Chapter Examples
Defining a Class with a Member Function
Introduction to UML, Use case and Class diagram
Defining a Member Function with a Parameter
Data Members, set Functions and get Functions
Outline
Let's begin with a simple analogy to help you reinforce your understanding from of classes and their contents.
Suppose you want to drive a car and make it go faster by pressing down on its accelerator pedal.
What must happen before you can do this? Well, before you can drive a car, someone has to design it and build it.
A car typically begins as engineering drawings, similar to the blueprints used to design a house.
These drawings include the design for an accelerator pedal that the driver will use to make the car go faster.
In a sense, the pedal "hides" the complex mechanisms that actually make the car go faster, just as the brake pedal "hides" the mechanisms that slow the car, the steering wheel "hides" the mechanisms that turn the car and so on.
Performing a task in a program requires a function (such as main).
The function describes the mechanisms that actually perform its tasks.
The function hides from its user the complex tasks that it performs, just as the accelerator pedal of a car hides from the driver the complex mechanisms of making the car go faster.
In C++, we begin by creating a program unit called a class to house a function.
A class is a blueprint or prototype from which objects are created.
A class consists of members: data and functions.
Objects are instances of classes.
A constructor is a special member function that has the same name as the class. Its purpose is to ensure that each data member is set to sensible initial values.
you must create an object of a class before you can get a program to perform the tasks the class describes.
That is one reason C++ is known as an object- oriented programming language.
Note also that just as many cars can be built from the same engineering drawing, many objects can be built from the same class.
When you drive a car, pressing its gas pedal sends a message to the car to perform a task that is, make the car go faster.
Similarly, you send messages to an object each message is known as a member-function call and tells a member function of the object to perform its task.
Example: Constructor
class MyCal { public: int a,b,sum; MyCal() { a=10; b=20; } int add() { sum=a+b; return sum; } };
int main()
{
MyCal c1;
cout << c1.add() <<endl; return 0;
}
int main()
{
MyCal c1,c2(1,2);
cout <<c1.add() << endl; cout <<c2.add(); return 0;
}
The GradeBook class definition contains a member function called displayMessage that displays a message on the screen.
Recall that a class is like a blueprint so we need to make an object of class GradeBook and call its displayMessage member function to get line 15 to execute and display the welcome message.
Every class's body is enclosed in a pair of left and right braces ({ and }), as in lines 10 and 17. The class definition terminates with a semicolon (line 17).