C++ Classes and Objects: Members, Constructors, and Functions, Slides of Programming for Engineers

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

2011/2012

Uploaded on 07/25/2012

hun_i
hun_i 🇮🇳

3.7

(3)

54 documents

1 / 78

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming for Engineers II
(EE112 )
Chapter-3
Introduction to Classes and Objects
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e

Partial preview of the text

Download C++ Classes and Objects: Members, Constructors, and Functions and more Slides Programming for Engineers in PDF only on Docsity!

Programming for Engineers II

(EE 112 )

Chapter-

Introduction to Classes and Objects

 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.

Class, Object and Constructor

 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;

}

Output

 Welcome to the Grade Book!

Class GradeBook

 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).