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

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

C++ Classes & Data Abstraction
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
ASP.NET Programming with C# and SQL Server First Edition
Chapter 15: Operator Overloading
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Review of C++ Programming Part II Sheng-Fang Huang.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Learners Support Publications Classes and Objects.
C++ Review (3) Structs, Classes, Data Abstraction.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
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.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
1 CSC241: Object Oriented Programming Lecture No 02.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
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.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
CONSTRUCTOR AND DESTRUCTORS
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
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.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Learners Support Publications Constructors and Destructors.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Constructors and Destructors
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
Static data members Constructors and Destructors
By Muhammad Waris Zargar
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.
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Constructor & Destructor
Chapter 15: Overloading and Templates
group work #hifiTeam
Object Oriented Analysis and Design
Chapter 3 Introduction to Classes, Objects Methods and Strings
Andy Wang Object Oriented Programming in C++ COP 3330
Classes & Objects: Examples
Learning Objectives Classes Constructors Principles of OOP
Constructors and Destructors
Classes and Objects.
Class.
NAME 436.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Andy Wang Object Oriented Programming in C++ COP 3330
Types of Computer Languages
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Constructors & Destructors
More C++ Classes Systems Programming.
Presentation transcript:

 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 of the objects. For example a class person can be used to define the characteristics and functions of a person. Each object of a class is known as an instance of its class. For example Ali, mobeen,nasir are three instances of a class person. Classes

 A class is declared in the same way as a structure is declared. The keyword class is used to declare a class. A class declaration specifies the variables and functions that are common to all objects of that class. The variables declared in a class are known as member variables or data members. The function declared in a class are called member functions. The syntax of declaring a class is as follows: Class identifier { Body of class }; Declaring a class

 The command that are used to specifies the access level of class members are known as access specifiers. There are two types of access specifiers: 1) Private access specifier 2) Public access specifier Access specifier

 It is used to restrict the class member within the class. The data member are normally declared with private access specifier. It is because the data of an object is more sensitive. For example  int a;  char c;  flaot x; Private access specifier

 The public access specifiers is used to allow the user to access a class member within the class as well as outside the class. Any member of the class declared with public access specifier can be accessed from anywhere in the program. The member function are normally declared with public access specifier. It is because the users access function of an object from outside the class. For example  show();  input(); Public access specifier

 A class is simply a model or prototype for creating object. It is like a new data type that contains both data and functions. An object is created in the same way as other variables are created. When an object of class is created, the space for all data members defined in the class is allocated in the memory according to their data type. An object is also known as instance. The process of creating an object of a class is also called instantiation. The syntax of creating an object of a class is as follows: class_name object_name; Creating objects

 An object of particular class contains all data member as well as member functions defined in that class. The data members contain the value related to the object. The member functions are used to manipulate the data members. The member functions can be executed only after creating an object. The syntax of executing member functions is as follows: object-name.function(); Executing member functions

 The member functions of class a can also be defined outside the class. The declaration of member functions is specified within the class and function definition is specified outside the class. The scope resolution operator :: is used in function declarator if the function is defined outside the class. The syntax of defining member functions outside the class is as follows: Return_type class_name :: function_name(parameters) { function body } Defining member functions outside the class

 A type of member function that is automatically executed when an object of that class is created is known as constructor. the constructor has no return type and has same name that of class name. The constructor can work as a normal function but it cannot return any value.it is normally defined in classes to initialize data member. The syntax of declaring constructors is as follows: name() { Constructor body } Constructors

 The method of passing parameter to a constructor is same as passing parameter to normal function. The only difference is that the parameter are passed to the constructor when the object is declared. The parameter are written in parenthesis along with the object name in declaration statement. The syntax of passing parameters to constructors is as follows: type object ­_name (parameter); Passing parameter to constructors

 The process of declaring multiple constructors with same name but different parameters is known as constructor overloading. The constructor with same name must differ in one of the following ways:  Number of parameters  Type of parameter  Sequence of parameters Constructor overloading

 A type of constructor that is used to initialize an object with another object of the same type is known as default copy constructor. Its name is “default copy constructor” because it is available by default in all classes. The user does not need to write this constructor. It accepts a single object of the same type as parameter. The parameter for default copy constructor can be given in parenthesis or using assignment operator. The syntax of using default copy constructor is as follows: Class _name object _name (parameter); OR Class _name object _name=parameter; Default copy constructor

 A type of member function that is automatically executed when an object of that class is destroyed is known as destructor. The destructor has no return type and its name is same as class name. The destructor cannot return any value. It also cannot accept any parameter. The destructor name is preceded by tilde sign~. ~name () { Destructor body } Destructors

 Object can also be passed as parameters to member functions. The method of passing object to a function as parameter is same as passing other simple variables. Objects as Function Parameters