What is Class? Remember

Similar documents
OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

And Even More and More C++ Fundamentals of Computer Science

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Classes, Constructor & Object Oriented Programming

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

CS201 - Introduction to Programming Glossary By

Introduction Of Classes ( OOPS )

Short Notes of CS201

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

G52CPP C++ Programming Lecture 9

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Chapter 10 Introduction to Classes

Computer Components. Software{ User Programs. Operating System. Hardware

Lecture 18 Tao Wang 1

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

PROGRAMMING IN C++ COURSE CONTENT

Friend Functions. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Introduction to Classes

AN OVERVIEW OF C++ 1

Ch. 10: Name Control

System Design and Programming II

More C++ : Vectors, Classes, Inheritance, Templates

C++ Inheritance and Encapsulation

CLASSES AND OBJECTS IN JAVA

Friend Functions, Inheritance

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

C++ 8. Constructors and Destructors

The University Of Michigan. EECS402 Lecture 06. Andrew M. Morgan. Savitch Ch. 6 Intro To OOP Classes Objects ADTs.

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Darshan Institute of Engineering & Technology for Diploma Studies

EL6483: Brief Overview of C Programming Language

Inheritance, and Polymorphism.

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Object-Oriented Design (OOD) and C++

Scope. Chapter Ten Modern Programming Languages 1

C++ Important Questions with Answers

Object Oriented Design

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Introduction to Programming Using Java (98-388)

QUIZ Friends class Y;

Interview Questions of C++

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Function Declarations. Reference and Pointer Pitfalls. Overloaded Functions. Default Arguments

QUIZ. What are 3 differences between C and C++ const variables?

An Introduction to C++

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Object Oriented Programming COP3330 / CGS5409

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Fast Introduction to Object Oriented Programming and C++

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Protection Levels and Constructors The 'const' Keyword

Object Oriented Programming(OOP).

Programming Languages Third Edition. Chapter 7 Basic Semantics

Structs. Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: Memory Layout

Runtime Environments I. Basilio B. Fraguela

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Module 10 Inheritance, Virtual Functions, and Polymorphism

Object Oriented Programming. Solved MCQs - Part 2

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Practical C++ Programming

Abstraction in Software Development

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Friends and Overloaded Operators

Object Oriented Programming in C#

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

Programming by Delegation

Midterm Review. PIC 10B Spring 2018

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

Friends and Overloaded Operators

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Functions. (transfer of parameters, returned values, recursion, function pointers).

CSE 374 Programming Concepts & Tools

Ch02. True/False Indicate whether the statement is true or false.

Fundamental Concepts and Definitions

Lecture #1. Introduction to Classes and Objects

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

What are the characteristics of Object Oriented programming language?

Classes and Objects. Class scope: - private members are only accessible by the class methods.

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

TDDE18 & 726G77. Functions

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Lecture 7. Log into Linux New documents posted to course webpage

III. Classes (Chap. 3)

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

Object Orientated Analysis and Design. Benjamin Kenwright

CS 162, Lecture 25: Exam II Review. 30 May 2018

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Section - Computer Science


Transcription:

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.

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