WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.

Slides:



Advertisements
Similar presentations
Chapter 14: Overloading and Templates
Advertisements

Starting Out with C++: Early Objects 5th Edition
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 11: Classes and Data Abstraction
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Guide To UNIX Using Linux Third Edition
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
C++ for Engineers and Scientists Third Edition
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Microsoft Visual C++.NET Chapter 51 Introduction to Classes.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 10 Introduction to Classes
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Function Overloading and References
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Classes C++ representation of an object
2 Chapter Classes & Objects.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Classes & Objects.
Introduction to Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 5 Classes.
Introduction to Classes
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Submitted By : Veenu Saini Lecturer (IT)
Classes C++ representation of an object
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.

CHAPTER 4 Classes & Objects

What is Class? The mechanism that allows you to combine data and the function in a single unit is called a class. Once a class is defined, you can declare variables of that type. A class variable is called object or instance. In other words, a class would be the data type, and an object would be the variable. Classes are generally declared using the keyword class.

What is Class? A class is the collection of related data and function under a single name. A C++ program can have any number of classes. When related data and functions are kept under a class, it helps to visualize the complex problem efficiently and effectively.

What is Class? A class is a definition of an object. It's a type just like int. A class resembles a struct with just one difference : all struct members are public by default. All classes members are private. Objects are the biggest difference between C++ and C. One of the earliest names for C++ was C with Classes. Remember : A class is a type, and an object of this class is just a variable.

What is Class? A class is a user defined type or data structure declared with keyword class that has data and functions (also called methods) as its members whose access is governed by the three access specifiers private, protected or public (by default access to members of a class is private). A class (declared with keyword class) in C++ differs from a structure (declared with keyword struct) as by default, members are private in a class while they are public in a structure.

What is Class? The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class. Instances of these data types are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer.

Defining Class class class_name { Data Members; Methods; }; OR class class_name { private: members1; protected: members2; public: members3; };

Defining Class Where class_name is a valid identifier for the class. The body of the declaration can contain members, that can be either data or function declarations, The members of a class are classified into three categories: private, public, and protected. private, protected, and public are reserved words and are called member access specifies. These specifies modify the access rights that the members following them acquire.

Defining Class private members of a class are accessible only from within other members of the same class. You cannot access it outside of the class. protected members are accessible from members of their same class and also from members of their derived classes. protected members are accessible from other members of the same class (or from their "friends"), but also from members of their derived classes.

Defining Class Finally, public members are accessible from anywhere where the object is visible. By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifies automatically has private access.

Defining Class Example class Circle { private: double radius; public: void setRadius(double r) { radius = r; } double getArea() { return 3.14 * radius * radius; } };

Defining Member Function of Class Member functions can be defined in two ways, 1.Inside the class 2.Outside the class

Defining Member Function of Class 1.Inside the class You can define Functions inside the class as shown in previous slides. Member functions defined inside a class this way are created as inline functions by default. It is also possible to declare a function within a class but define it elsewhere. Functions defined outside the class are not normally inline. When we define a function outside the class we cannot reference them (directly) outside of the class. In order to reference these, we use the scope resolution operator, :: (double colon). In this example, we are defining function setRadius outside the class:

Defining Member Function of Class 2. Outside the class A large program may contain many member functions. For the clarity of the code, member functions can be defined outside the class. To do so, member function should be declared inside the class(function prototype should be inside the class). Then, the function definition can be defined using scope resolution operator ::.

Defining Member Function of Class 2. Outside the class When we define a function outside the class we cannot reference them (directly) outside of the class. In order to reference these, we use the scope resolution operator, :: (double colon). In this example, we are defining function setRadius outside the class: void Circle :: setRadius(double r) { radius = r; }

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 circle: Circle c1, c2; Circle is class and c1 and c2 are the objects. Once the class is defined you can define any number of objects.

Object Declaration Accessing Class Members Once an object of a class is declared, it can access the public members of the class using (. ) dot membership operator. c1.setRadius(2.5);

Arrays within Class The array can be used as member variables in a class. The following class definition is valid. const int size=10; class array { int a[size]; public: void setval(void); void display(void); };

Arrays within Class The array variable a[] declared as private member of the class array can be used in the member function, like any other array variable. We can perform any operations on it. For instance, in the above class definition, the member function setval() sets the value of element of the array a[], and display() function displays the values. Similarly, we may use other member functions to perform any other operation on the array values.

Access modifiers Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components

Access modifiers In C++, there are only three access modifiers. C# extends the number of them to five, while Java has four access modifiers, [2] but three keywords for this purpose. In Java, having no keyword before defaults to the package-private modifier. Access specifiers for classes:- When a class is declared as public, it is accessible to other classes defined in the same package as well as those defined in other packages. This is the most commonly used specifier for classes. [2]

Access modifiers A class cannot be declared as private. If no access specifier is stated, the default access restrictions will be applied. The class will be accessible to other classes in the same package but will be inaccessible to classes outside the package. When we say that a class is inaccessible, it simply means that we cannot create an object of that class or declare a variable of that class type. The protected access specifier too cannot be applied to a class.

Access modifiers C++ uses the three modifiers called public, protected, and private. C# has the modifiers public, protected, internal, private, and protected internal. Java has public, package, protected, and private.

Local Classes in C++ A class declared inside a function becomes local to that function and is called Local Class in C++. For example, in the following program, Test is a local class in fun(). void fun() { class Test // local to fun { /* members of Test class */ }; }

Global Classes in C++ A class declared outside the body of all the function in a program is called Global Class in C++. For example class sample { int x; public: void readx() { cin>>x;} }; void main() { sample s1; s1.readx(); }

Local and Global Objects in C++ A class declared outside the body of all the function in a program is called Global Class in C++. For example class sample { int x; public: void readx() { cin>>x;} }; sample s1; // Global Object void main() { sample s2; // Local Object s1.readx(); }

STATIC FUNCTION MEMBERS We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. We can't put it in the class definition but it can be initialized outside the class as done in the following example by redeclaring the static variable, using the scope resolution operator :: to identify which class it belongs to.

STATIC FUNCTION MEMBERS By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::. A static member function can only access static data member, other static member functions and any other functions from outside the class. Static member functions have a class scope and they do not have access to the this pointer of the class. You could use a static member function to determine whether some objects of the class have been created or not.

class test { static int i; public: void static geti() { ++i; } void printi() { cout<<"\n i val is = "<<i; } }; int test :: i=0; // needs to be initilized. void main() { clrscr(); test t1, t2; t1.geti(); t2.geti(); t1.printi(); getch(); }

What is C++ inline functions In C, we have used Macro function an optimized technique used by compiler to reduce the execution time etc. So Question comes in mind that what’s there in C++ for that and in what all better ways? Inline function is introduced which is an optimization technique used by the compilers especially to reduce the execution time. We will cover “what, why, when & how” of inline functions.

What is C++ inline functions The inline functions are a C++ enhancement feature to increase the execution time of a program. Functions can be instructed to compiler to make them inline so that compiler can replace those function definition wherever those are being called. Compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime. Inline functions are defined with inline keyword. For Example: inline int sqr(int x) { int y; y = x * x; return y; }

What is C++ inline functions Pros :- 1. It speeds up your program by avoiding function calling overhead. 2. It save overhead of variables push/pop on the stack, when function calling happens. 3. It save overhead of return call from a function. 4. It increases locality of reference by utilizing instruction cache.

What is C++ inline functions Pros 5. By marking it as inline, you can put a function definition in a header file (i.e. it can be included in multiple compilation unit, without the linker complaining)

What is C++ inline functions Cons :- 1. It increases the executable size due to code expansion. 2. C++ inlining is resolved at compile time. Which means if you change the code of the inlined function, you would need to recompile all the code using it to make sure it will be updated. 3. When used in a header, it makes your header file larger with information which users don’t care.

What is C++ inline functions Cons :- 4. As mentioned above it increases the executable size, which may cause thrashing in memory. More number of page fault bringing down your program performance. 5. Sometimes not useful for example in embedded system where large executable size is not preferred at all due to memory constraints.

When inline functions do not work? The function should be inlined only when they are small. There is no point in inlining large functions as there would be heavy memory penalty. The linlining does not work for the following situations. 1.For a functions that returns the values and are having a loop or a switch or goto statements. 2.For a functions not returning values, if return statement exists. 3.If a functions contain static variables. 4.If the function is recursive.

FRIEND FUNCTION A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows:

FRIEND FUNCTION class Box { double width; public: double length; friend void printWidth( Box box ); void setWidth( double wid ); }; void Box::setWidth( double wid ) { width = wid; }

FRIEND FUNCTION int main( ) { Box box; // set box width without member function box.setWidth(10.0); // Use friend function to print the wdith. printWidth( box ); return 0; }

? Any Questions Please

Thank You Very Much