Write a program that converts a data of a class with inch and feet user defined to inches float. Write a program to read and display Cartesian Coordinate using stream operators. Write a program to read and display complex numbers using stream operator overloading and addition and subtraction of complex numbers using operator overloading.
After reading the data, write it to a file then display the content of the file in the proper format on your output screen. Write a program to display n characters by using default argument for all parameters. Assume that the function takes two parameters one character to be printed and other number of characters to be printed.
Write a program to check whether the number is prime or not using object class concept. Remember to return enumerated true or false the function where you check the number. Write a program to display the output in pyramid form as follows. There's no signup, and no start or end dates.
Knowledge is your reward. Use OCW to guide your own life-long learning, or to teach others. We don't offer credit or certification for using OCW. Made for sharing. Download files for later. Send to friends and colleagues.
Modify, remix, and reuse just remember to cite OCW as the source. Object-Oriented Programming. Course Home Syllabus Instructor Insights. Python Tutorial. Unit 1: Software Engineering. The exit code is used by some operating systems and may be used by calling programs. By convention, an exit code of 0 means that the program finished normally and any other value means an error happened.
The selective Structure: switch. The syntax of the switch instruction is a bit peculiar. Its objective is to check several possible constant values for an expression, something similar to what we did at the beginning of this section with the linking of several if and else if sentences.
It works in the following way: switch evaluates expression and checks if it is equivalent to constant1, if it is, it executes block of instructions 1 until it finds the break keyword, then the program will jump to the end of the switch selective.
If expression was not equal to constant1 it will check if expression is equivalent to constant2. If it is, it will execute block of instructions 2 until it finds the break keyword. Finally, if the value of expression has not matched any of the previously specified constants you may specify as many case sentences as values you want to check , the program will execute the instructions included in the default: section, if this one exists, since it is optional.
I have commented before that the syntax of the switch instruction is a bit peculiar. Notice the inclusion of the break instructions at the end of each block. For example:. If you need to check ranges or values that are not constants use a concatenation of if and else if sentences. The :: scope resolution operator is used to get hidden names due to variable scopes so that you can still use them.
The scope resolution operator can be used as both unary and binary. You can use the unary scope operator if a namespace scope or global scope name is hidden by a particular declaration of an equivalent name during a block or class.
If a class member name is hidden, you can use it by prefixing it with its class name and the class scope operator. Output: A's x is 10 B's x is 20 5 For namespace If a class having the same name exists inside two namespace we can use the namespace name with the scope resolution operator to refer that class without any conflicts.
Here, cout and endl belong to the std namespace. What are memory management operators? These two memory management operators are used for allocating and freeing memory blocks in efficient and convenient ways. This operator can be used to create object of any type. They are used to store similar type of elements as in the data type must be the same for all elements. They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type.
Why do we need arrays? We can use normal variables v1, v2, v3,.. The idea of an array is to represent many instances in one variable. It can be done by specifying its type and size, by initializing it or both. Array declaration by specifying size.
Random access of elements using array index. Use of less line of code as it creates a single array of multiple elements. Easy access to all the elements. Traversal through the array becomes easy using a single loop. Sorting becomes easy as it can be accomplished by writing less line of code. Allows a fixed number of elements to be entered which is decided at the time of declaration.
Unlike a linked list, an array in C is not dynamic. Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation. Array index starts with 0 and goes till size of array minus 1. Output In C, it is not compiler error to initialize an array with more elements than the specified size. For example, the below program compiles fine and shows just Warning. Warnings: prog.
If we save the above program as a. The elements are stored at contiguous memory locations Example:. Output Size of integer in this compiler is 4 Address arr[0] is 0x7ffe75c Address arr[1] is 0x7ffe75c Address arr[2] is 0x7ffe75c For example, you want to store Student details like student name, student roll num, student age. You have two ways to do it, one way is to create different variables for each data, but the downfall of this approach is that if you want to store the details of. Similarly I can set and get the values of other data members of the structure for every student.
Lets see a complete example to put this up all together:. Example: 1. Rectangle int w, int h 6. Open navigation menu. Close suggestions Search Search. User Settings. Skip carousel. Carousel Previous. Carousel Next. What is Scribd? Explore Ebooks.
Bestsellers Editors' Picks All Ebooks. Explore Audiobooks. Bestsellers Editors' Picks All audiobooks. Explore Magazines. Editors' Picks All magazines. Explore Podcasts All podcasts. Difficulty Beginner Intermediate Advanced. Explore Documents. Unit 1-OOP. Uploaded by Kamini Salunkhe. Document Information click to expand document information Description: Object oriented programming class notes unit 1. Did you find this document useful? Is this content inappropriate?
Report this Document. Description: Object oriented programming class notes unit 1. Flag for inappropriate content. Related titles. Carousel Previous Carousel Next. Jump to Page. Search inside document. Unit 1 Principles of OOP Object-oriented programming OOP is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields often known as attributes or properties , and code, in the form of procedures often known as methods.
Object oriented. The actions that can be performed by objects becomes functions of the class and is referred to as Methods. No memory is allocated when a class is created. Memory is allocated only when an object is created, i. Inheritance: Inheritance is the process of forming a new class from an existing class or base class.
The base class is also known as parent class or super class, The new class that is formed is called derived class. Derived class is also known as a child class or sub class. Inheritance helps in reducing the overall code size of the program, which is an important concept in object-oriented programming. Data Abstraction: Data Abstraction increases the power of programming language by creating user defined data types.
Data Abstraction also represents the needed information in the program without presenting the details. 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.
This time saving feature optimizes code, helps in gaining secured applications and facilitates easier maintenance on the application. Programming Concepts: Encapsulation It is a mechanism that associates the code and the data it manipulates into a single unit to and keeps them safe from external interference and misuse. An instance of an object is known as object which represents a real world entity. Data Abstraction A data abstraction is a simplified view of an object that includes only features one is interested in while hides away the unnecessary details.
In programming languages, a data abstraction becomes an abstract data type or a user-defined type. In OOP, it is implemented as a class. Terminology: "the child or derived class inherits or is derived from the parent or base class". It is a lot useful since it can group classes and their functions together. Polymorphism means that the same thing can exist in two forms. This is an important characteristic of true object oriented design - which means that one could develop good OO design with data abstraction and inheritance, but the real power of object oriented design seems to surface when polymorphism is used.
Multiple Inheritance The mechanism by which a class is derived from more than one base class is known as multiple inheritance. Instances of classes with multiple inheritance have instance variables for each of the inherited base classes.
Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section. The private member is inaccessible from outside the class. Public members are accessible from outside the class. A protected access specifier is a stage between 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.
Data Members : Data members include members that are declared with any of the fundamental types, as well as other types, including pointer, reference, array types, bit fields, and user-defined types. You can declare a data member the same way as a variable, except that explicit initializers are not allowed inside the class definition.
However, a const static data member of integral or enumeration type may have an explicit initializer. A class X cannot have a member that is of type X, but it can contain pointers to X, references to X, and static objects of X. Member functions of X can take arguments of type X and have a return type of X. Only one copy of the static member is shared by all objects of a class in a program. When you declare an object of a class having a static member, the static member is not part of the class object.
You access a static member by qualifying the class name using the :: scope resolution operator. They make the programs more modular and easy to read and manage. The execution of the program starts from the function main. The name of the function should begin with the alphabet or underscore. The parameter list consists of variables separated with comma along with their data types. The parameter list could be empty which means the function do not contain any parameters. The parameter list should contain both data type and name of the variable.
For example, int factorial int n, float j is the function header of the function factorial. The return type is of integer which means function should return data of type integer. The parameter list contains two variables n and j of type integer and float respectively. The body of the function performs the computations. Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier.
These are called friends of a class. You can declare a member function as static; this is called a static member function. A member function that is not declared as static is called a nonstatic member function. The definition of a member function is within the scope of its enclosing class.
The body of a member function is analyzed after the class declaration so that members of that class can be used in the member function body, even if the member function definition appears before the declaration of that member in the class member list. When the function add is called in the following example, the data variables a, b, and c can be used in the body of add.
You still may omit variable names in the prototypes. The following will show an example of this. Public and private don't apply to them. To declare a class method constant, put the keyword const after the parentheses but before the semicolon.
The declaration of the constant member functionSomeFunction takes no arguments and returns void. It looks like this: void SomeFunction const; Access or functions are often declared as constant functions by using the const modifier Declare member functions to be const whenever they should not change the object Volatile Functions: The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.
If your objects are used in a multithreaded environment or they can be accessed asynchronously say by a signal handler , they should be declared volatile. A volatile object can call only volatile member functions safely. A class member is either a property or a method. A static member of a class is a member whose value is the same for every object instantiated.
This means that if one object changes the value of the static member, this change will be reflected in another object instantiated from the class.
The change or the resulting value will be the same in all the instantiated objects. You can also access a static member using the class name without instantiation. You can have a static member along side other members in your class. Static Property A static property is also called a static data member.
Declaring a Static Property You declare a static property just as you declare any other attribute, but you precede the declaration expression with the keyword, static and a space. The syntax is: static Type Ident; Despite this simple feature, you have to learn how to use the static member. You do not use it in the straightforward way. This class has just one member, which is the static data member. You initialize the static member outside the class description as shown above. You begin with the return type of the static property.
This is followed by a space and then the name of the class. After that you have the scope operator, then the identifier of the static property. Then you have the assignment operator and the value. You instantiate an object form the class that has the static member in the normal way.
You access the static property of an instantiated object in the normal way. The second line in the main function illustrates this. However, changing the value as this line has done means changing the value for the class description and any instantiated object and any object that is still to be instantiated. The third line in the main function displays the static property value.
It uses the class name; it did not use the object name. To use the class name to access the static attribute, you begin with the class name. This is followed by the scope operator and then the identifier of the static property. This shows how you can access a static attribute with the class name directly and without using an object; this is like accessing the property in the class description. The static member is a kind of global object. A class is an extended concept similar to that of structure in C programming language, this class describes the data properties alone.
Classes are not objects, but they are used to instantiate objects. Creation of Objects: Once the class is created, one or more objects can be created from the class as objects are instance of the class.
Juts as we declare a variable of data type int as: int x; Objects are also declared as: class name followed by object name; exforsys e1; This declares e1 to be an object of class exforsys. References, by their very nature, cannot change what they refer to. Pointers, on the other hand, have two ways that you can use them: you can change the data pointed to, or change the pointer itself. Consequently, there are two ways of declaring a const pointer: one that prevents you from changing what is pointed to, and one that prevents you from changing the data pointed to.
Notice, by the way, that this pointer had to be initialized when it was declared: since the pointer itself is const, we can't change what it points to later on! Them's the rules. Generally, the first type of pointer, where the data is immutable, is what I'll refer to as a "const pointer" in part because it's the kind that comes up more often, so we should have a natural way of describing it. Such a class is called a "nested class. Just as you would manage any other class so can you exercise control on a nested class.
For example, you can declare all necessary variables or methods in the nested class or in the nesting class. When you create one class inside of another, there is no special programmatic relationship between both classes: just because a class is nested doesn't mean that the nested class has immediate access to the members of the nesting class.
They are two different classes and they can be used separately. To access a nested class outside of the nesting class, you must qualify the name of the nested class anywhere you want to use it.
0コメント