programming in c++ lecture 9 object oriented programming

19
Programming in C++ Lecture 9 –Object Oriented Programming By T.K.Malwatta Senior Lecturer Department of Software Technology(FIT) University of Vocational Technology 1

Upload: others

Post on 28-Jun-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming in C++ Lecture 9 Object Oriented Programming

Programming in C++ Lecture 9 –Object Oriented Programming

By T.K.Malwatta

Senior Lecturer Department of Software Technology(FIT)

University of Vocational Technology

1

Page 2: Programming in C++ Lecture 9 Object Oriented Programming

OOP Concepts

There are two common programming methods:

- procedural programming

- object-oriented programming (OOP).

So far you have been created procedural programs.

Procedural Programming

In a procedural program data is typically stored in a collection of variables and

there is a set of functions that perform operations on the data. The data and the

functions are separate entities.. However, as programs become larger and more

complex, the separation of a program’s data and the code that operates on the

data can lead to problems.

2

Page 3: Programming in C++ Lecture 9 Object Oriented Programming

Object Oriented programming

An object is an entity that combines both data and procedures in a single unit. An

object’s data items, also referred to as its attributes, are stored in member

variables. The procedures that an object performs are called its member

functions. This wrapping of an object’s data and procedures together is called

encapsulation, they also permit data hiding.

Advantages of Object oriented programming

- Software complexity can be easily managed

- Object-oriented systems can be easily upgraded

- It is quite easy to partition the work in a project based on object

3

Page 4: Programming in C++ Lecture 9 Object Oriented Programming

4

What is an object oriented Programming? An object oriented programming is a new way to code your program for

making new applications. This object oriented programming is also called procedure oriented

programming. Object oriented programming has blocks of code that are called by the

objects when require . C++ and Java are an object oriented programming.

An object oriented programming includes: · Objects. · Class. · Access specifier · Variables(Data members). · Functions(Member functions). · Keywords.

Page 5: Programming in C++ Lecture 9 Object Oriented Programming

An Overview about Objects and Classes In object-oriented programming language C++, the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an class. One of the popular way to solve a programming problem is by creating object. A class is an extended concept similar to that of structure in C++ programming language In C++ programming language, class describes both the properties (data) and behaviors (functions) of objects. Classes are not objects, but they are used to instantiate objects. General Form class class_name { access specifiers: data member; member function; access specifiers: data member; member function; };

5

Page 6: Programming in C++ Lecture 9 Object Oriented Programming

Classes contain data known as members and member functions. As a unit, the collection of members and member functions is an object. Therefore, this unit of objects make up a class. Access Specifier: Access specifiers are used to identify access rights for the data and member functions of the class. There are three main types of access specifiers in C++ programming language:

-private -public -protected

private members of a class are accessible only from within other members of the same class. You cannot access it outside of the class. public members are accessible from anywhere where the object is visible protected members are accessible from members of their same class and also from members of their derived classes. (class created from another exiting class) .

6

Page 7: Programming in C++ Lecture 9 Object Oriented Programming

class item { private: int x,y; public: void sum() { ……… ……… } };

Generally, in class, all members (data) would be declared as private and the member functions would be declared as public. Private is the default access level for specifiers. If no access specifiers are identified for members of a class, the members are defaulted to private access.

7

Page 8: Programming in C++ Lecture 9 Object Oriented Programming

Example class item { private: int x,y; public: void sum() { ……… ……… } }; main() { item e1; …………… …………… }

8

Object Declaration Once a class is defined, you can declare objects of that type. The syntax for declaring a object is the same as that for declaring any other variable. The following statements declare two objects of type Item: Class name followed by object name *Item e1,e2;

Page 9: Programming in C++ Lecture 9 Object Oriented Programming

class item{ private: int x,y; public: void sum() { ……… ……… } }e1 ;

The object can also be declared immediately after the class definition. In other words the object name can also be placed immediately before the closing brace symbol } of the class declaration.

It is important to understand that in object-oriented programming language, when a class is created no memory is allocated. It is only when an object is created is memory then allocated.

9

Page 10: Programming in C++ Lecture 9 Object Oriented Programming

Access C++ Class Members

It is possible to access the class members after a class is defined and objects are created. General syntax to access class member: Object_name.function_name (arguments); Example class item { int a, b; public: void sum(int,int); } e1; Then the member access is written as: e1.sum(5,6);

It is also possible to declare more than one object within a class:

10

Page 11: Programming in C++ Lecture 9 Object Oriented Programming

class item { private: int a; public: void sum(int) { ……… ……… } }; main() { item e1,e2; ………… ………… }

In these two objects e1 and e2 are declared of class item

11

Page 12: Programming in C++ Lecture 9 Object Oriented Programming

Defining member functions Member functions can be defined in 2 places 1. Inside the class definition 2. Outside the class definition

Inside the class definition Small functions may be defined inside the class declaration class item { Int number; float cost; public: void getdata(int a,float b); void putdata( void) { cout<< number<<“\n”; cout<< cost<<“\n”; } }

12

Page 13: Programming in C++ Lecture 9 Object Oriented Programming

Outside the class definition

return type class name :: function name(arguments declaration) { function body }

void item::getdata(int a,int b) { number=a; cost =b; } void item::putdata(void) { cout<<“Number=”<<number; cout<<“Cost=<<cost”; }

13

Page 14: Programming in C++ Lecture 9 Object Oriented Programming

Example 1 # include<iostream> using namespace std; class item { private: int number; float cost; public: void getdata(int a,float b); void putdata(void) { cout<<"Number:"<<number<<endl; cout<<"Cost:"<<cost<<endl; } }; //member function definition void item ::getdata(int a,float b) { number=a; cost=b; } //main program int main() { item x; // create object x.getdata(100,29.5); //call member function x.putdata(); return 0;

14

Page 15: Programming in C++ Lecture 9 Object Oriented Programming

Example 2 Write a program to display “Hello World” using classes #include <iostream> using namespace std; class Hello { public: void sayHello() { cout << "Hello World" << endl; } }; int main() { Hello h; h.sayHello(); return 0; }

15

Page 16: Programming in C++ Lecture 9 Object Oriented Programming

Example 3 Program to find sum of 5 numbers using classes #include<iostream> using namespace std; class sum { private: int n,s=0; public: void calc(); void display(); }; void sum::calc() { for(n=1;n<=5;n++) s=s+n; } void sum::display() { cout<<"Sum of the 5 numbers :"<<s; } int main() { sum s1; s1.calc(); s1.display(); return 0; } 16

Page 17: Programming in C++ Lecture 9 Object Oriented Programming

Example 4 Define a class student with the following specification Private members of class student admno integer sname 20 character eng,math, science float total float ctotal() a function to calculate eng + math + science with float return type. Public member function of class student Takedata() Function to accept values for admno, sname, eng, math,science and invoke ctotal() to calculate total. Showdata() Function to display all the data members on the screen.

17

Page 18: Programming in C++ Lecture 9 Object Oriented Programming

18

#include<iostream>

using namespace std;

class student

{

private:

int admno;

char sname[20];

float eng,math,science;

float total;

float ctotal()

{

return eng+math+science;

}

public:

void Takedata()

{

cout<<"Enter admission number ";

cin>>admno;

cout<<"Enter student name " ;

cin>>sname;

cout<< "Enter marks in english, math, science ";

cin>>eng>>math>>science;

total=ctotal();

}

void Showdata()

{

cout<<"Admission number "<<admno<<"\nStudent name "<<sname<<"\nEnglish "

<<eng<<"\nMath "<<math<<"\nScience "<<science<<"\nTotal "<<total;

}

};

int main ()

{

student obj ;

obj.Takedata();

obj.Showdata();

return 0;

}

Page 19: Programming in C++ Lecture 9 Object Oriented Programming

1. Enter name and age using classes - Take 2 member functions - one member function to read name and age inside the class - and other member function outside the class which display the result. 2. Display 2 numbers using classes. Use 2 member functions within the class. First member function which is assigned the both values 3. Calculate sum of 2 numbers using classes use 3 member functions inside the class to read values, calculate the sum, display the result . 4. Write a program to display student details using classes. - 2 members outside the class (Name, RegNo, Semester) 5. Find the factorial of a number using classes - Take 3 member functions outside the class

19