The best revenge is massive SUCCESS
--Your friends at LectureNotes

Note for Object Oriented Programming Using Cpp - OOP By Papithasri K

  • Object Oriented Programming Using Cpp - OOP
  • Note
  • 7 Topics
  • 2 Offline Downloads
  • Uploaded 1 year ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-2

080230004 - Object Oriented Programming www.Vidyarthiplus.com Data Encapsulation Data Encapsulation combines data and functions into a single unit called Class. When using Data Encapsulation, data is not accessed directly; it is only accessible through the functions present inside the class. Data Encapsulation enables the important concept of data hiding possible. Polymorphism Polymorphism allows routines to use variables of different types at different times. An operator or function can be given different meanings or functions. Polymorphism refers to a single function or multi-functioning operator performing in different ways. Overloading Overloading is one type of Polymorphism. It allows an object to have different meanings, depending on its context. When an exiting operator or function begins to operate on new data type, or class, it is understood to be overloaded. Reusability This term refers to the ability for multiple programmers to use the same written and debugged existing class of data. This is a time saving device and adds code efficiency to the language. Additionally, the programmer can incorporate new features to the existing class, further developing the application and allowing users to achieve increased performance. Introduction to C++ Variable, Constants and Data types in C++ Variables A variable is the storage location in memory that is stored by its value. A variable is identified or denoted by a variable name. The variable name is a sequence of one or more letters, digits or underscore, for example: character _ Rules for defining variable name: example character _. denote variable name. programming language. C++ is a case-sensitive language. Variable names written in capital letters differ from variable names with the same name but written in small letters. For example, the variable name EXFORSYS differs from the variable name exforsys. Data Types Below is a list of the most commonly used Data Types in C++ programming language Using variable names and data type, we shall now learn how to declare variables. Declaring Variables: In order for a variable to be used in C++ programming language, the variable must first be declared. The syntax for declaring variable names is data type variable name; The date type can be int or float or any of the data types listed above. A variable name is given based on the rules for defining variable name (refer above rules). www.Vidyarthiplus.com Dept of IT SVHEC 2

Text from page-3

080230004 - Object Oriented Programming www.Vidyarthiplus.com Example: int a; This declares a variable name a of type int. If there exists more than one variable of the same type, such variables can be represented by separating variable names using comma. For instance int x,y,z ; This declares 3 variables x, y and z all of data type int. The data type using integers (int, short int, long int) are further assigned a value of signed or unsigned. Signed integers signify positive and negative number value. Unsigned integers signify only positive numbers or zero. For example it is declared as unsigned short int a; signed int z; By default, unspecified integers signify a signed integer. For example: int a; is declared a signed integer It is possible to initialize values to variables: data type variable name = value; Example: int a=0; int b=5; Constants Constants have fixed value. Constants, like variables, contain data type. Integer constants are represented as decimal notation, octal notation, and hexadecimal notation. Decimal notation is represented with a number. Octal notation is represented with the number preceded by a zero character. A hexadecimal number is preceded with the characters 0x. Example 80 represent decimal 0115 represent octal 0x167 represent hexadecimal By default, the integer constant is represented with a number. The unsigned integer constant is represented with an appended character u. The long integer constant is represented with character l. Example: 78 represent int 85u present unsigned int 78l represent long Floating point constants are numbers with decimal point and/or exponent. Example 2.1567 4.02e24 These examples are valid floating point constants. Floating point constants can be represented with f for floating and l for double precision floating www.Vidyarthiplus.com Dept of IT SVHEC 3

Text from page-4

080230004 - Object Oriented Programming www.Vidyarthiplus.com point numbers. Character constants have single character presented between single quotes. Example ‗c‘ ‗a‘ are all character constants. Strings are sequences of characters signifying string constants. These sequence of characters are represented between double quotes. Example: ―Exforsys Training‖ is an example of string constant. Referencing variables The & operator is used to reference an object. When using this operator on an object, you are provided with a pointer to that object. This new pointer can be used as a parameter or be assigned to a variable. C++ Objects and Classes 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 object. A class is an extended concept similar to that of structure in C programming language, this class describes the data properties alone. 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. Features of Class: Classes contain member data and member functions. As a unit, the collection of member data and member functions is an object. Therefore, this unit of objects makes up a class. How to write a Class: In Structure in C programming language, a structure is specified with a name. The C++ programming language extends this concept. A class is specified with a name after the keyword class. The starting flower brace symbol, {is placed at the beginning of the code. Following the flower brace symbol, the body of the class is defined with the member functions data. Then the class is closed with a flower brace symbol} and concluded with a colon;. class exforsys { member data; member functions; …………… }; There are different access specifiers for defining the data and functions present inside a class. Access specifiers: 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: www.Vidyarthiplus.com Dept of IT SVHEC 4

Text from page-5

080230004 - Object Oriented Programming www.Vidyarthiplus.com private public protected private member within a class denotes that only members of the same class have accessibility. The private member is inaccessible from outside the class. Public members are accessible from outside the class. private and public access. If member functions defined in a class are protected, they cannot be accessed from outside the class but can be accessed from the derived class. When defining access specifiers, the programmer must use the keywords: private, public or protected when needed, followed by a semicolon and then define the data and member functions under it. class exforsys { private: int x,y; public: void sum() { ……… ……… } }; In the code above, the member x and y are defined as private access specifiers. The member function sum is defined as a public access specifier. General Syntax of a class: General structure for defining a class is: class classname { acess specifier: data member; member functions; acess specifier: data member; member functions; }; 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. class exforsys { int x,y; public: void sum() { ……… ……… } }; www.Vidyarthiplus.com Dept of IT SVHEC 5

Lecture Notes