The Notion of a Class and Some Other Key Ideas (contd.) Questions:

Size: px
Start display at page:

Download "The Notion of a Class and Some Other Key Ideas (contd.) Questions:"

Transcription

1 The Notion of a Class and Some Other Key Ideas (contd.) Questions: 1

2 1. WHO IS BIGGER? MR. BIGGER OR MR. BIGGER S LITTLE BABY? Which is bigger? A class or a class s little baby (meaning its subclass)? 2

3 2. When you extend a class in C++ in order to define a subclass, the syntax for the subclass header must have what elements in it? 3

4 3. When you extend a class in Java in order to define a subclass, the syntax for the subclass header must have what keyword in it? 4

5 4. What is one of the first things that a subclass constructor must do in both C++ and Java? 5

6 5. How does a C++ subclass constructor invoke its superclass constructor? 6

7 6. How does a Java subclass constructor invoke its superclass constructor? 7

8 7. Is it necessary for a subclass constructor to invoke its superclass constructor? 8

9 Packages in Java (Section 3.9) The Java platform consists of packages of related classes and interfaces. For example, all the classes that deal with I/O are in thejava.io package; the utility classes are all grouped in the java.util package; etc. 9

10 import java.util.*; public class Test { public static void main( String[] args ) { int[] arr = new int[ ]; for ( int i=0; i< ; i++ ) arr[i] = (int) ( * Math.random() ); long starttime = System.currentTimeMillis(); Arrays.sort( arr ); long difftime = System.currentTimeMillis() - starttime; System.out.println( "\n\nsort time in milliseconds: " + difftime ); 10

11 In order to create your own packages, the first executable statement in your class definition must be statement package packagename; 11

12 Namespaces in C++ (Section 3.10) The modularity that one gives to Java software through the mechanism of packages can be given to C++ software through the mechanism of namespaces. The basic syntax for encapsulating code in a namespace is namespace ModuleName { // code All of the code in a given namespace can be in one file, or distributed over multiple files. If more than one file is involved, the code in each file must be encapsulated in the manner shown above. 12

13 A namespace creates a scope. The identifiers declared in a given namespace cannot be accessed directly outside that namespace without the scope operator ::. For example, if we create namespace module by namespace Module1 { void foo() { cout << "Module1 foo() invoked" << endl; To access the function foo(), we must invoke it by Module1::foo(); 13

14 Since it can get to be tedious to always have to precede the name of a function by its module name, C++ allows us to employ the using directive, as in int main() { using namespace Module1; foo(); 14

15 #include <string> #include <iostream> using namespace std; //(A) namespace Module1 { void foo(); // this is only a declaration // definition will come later namespace Module2 { void foo() { cout << "Module2 foo() invoked" << endl; namespace Module3 { using namespace Module1; // has foo //(B) using namespace Module2; // also has foo, but //(C) // no problem at this point void bar() { cout << "Module3 bar() invoked" << endl; namespace Module4 { void foo() { cout << "Module4 foo() invoked" << endl; namespace Module5 { void bar() { cout << "Module5 bar() invoked" << endl; // foo of Module1 defined outside the namespace Module1. Must // therefore use namespace-qualified name for foo: 15

16 void Module1::foo() { cout << "Module1 foo() invoked" << endl; // The global foo: void foo() { cout << "top level foo() invoked" << endl; //Addition to Module5: namespace Module5 { void hum() { cout << "Module5 hum() invoked" << endl; int main() { //This statement invokes global foo() foo(); Module1::foo(); Module2::foo(); //(D) //(E) //(F) //The following statement, if uncommented, results //in compiler error because Module1 and Module2 //both have foo() // Module3::foo(); //(G) Module3::bar(); //(H) using namespace Module4; 16

17 //The following statement, if uncommented, results //in compiler error because foo() of Module4 //conflicts with the global foo() // foo(); //(I) //But the following statement is okay since it uses //the scope operator for invoking the global foo() ::foo(); using namespace Module5; bar(); hum(); return 0; //(J) //(K) //(L) 17

18 The program produces the following output: top level foo() invoked Module1 foo() invoked Module2 foo() invoked Module1 foo() invoked Module3 bar() invoked Module5 bar() invoked Module5 hum() invoked 18

19 Using Declaration Versus Using Directive While a using directive makes all its names merely available to the current scope, a using declaration actually declares a specific namespace name in the current scope. 19

20 //Namespaces2.cc namespace Module1 { class X {; class Y {; namespace Module2 { class X {; class Y {; int main() { using Module1::X; X x1; // using Module2::X; // ERROR, name conflict X x2; return 0; 20

21 Which Namespace Owns Names Imported from Another Namespace? If one namespace is imported into another named namespace, the member names of the former can be thought of as belonging to the latter. 21

22 //Namespaces3.cc #include <iostream> using namespace std; namespace Module1 { class X {; namespace Module2 { void foo(){ cout << "foo of Module2 invoked" << endl; void bar(){ cout << "bar of Module2 invoked" << endl; namespace Module3 { using namespace Module1; typedef X Y; using Module2::foo; class Z {; int main() { Module3::X x; Module3::Y y; 22

23 Module3::foo(); // Module3::bar(); // ERROR. No bar in Module3. return 0; 23

24 Using Declarations and Directives Have Scope Just like any other declaration, using directives and declarations have block scope. A block is a section of code delimited by curly brackets. This fact can be used to localize the visibility of the names introduced into a program by either a using directive or a using declaration. 24

25 //Namespaces.cc #include <iostream> using namespace std; namespace Module1 { typedef int Type; Type foo( Type arg ) { return arg; namespace Module2 { typedef double Type; Type foo( Type arg ) { return arg; int main() { { using namespace Module1; Type x = 100; { // int cout << foo( x ) << endl; // 100 using namespace Module2; Type x = 3.14; // double 25

26 { { cout << foo( x ) << endl; // 3.14 using Module1::foo; cout << foo( 100 ) << endl; // 100 using Module2::foo; cout << foo( 3.14) << endl; // 3.14 return 0; 26

27 Nesting Namespaces and Namespace Aliases It is possible to nest namespaces to any depth. If an inner namespace contains a name that is identical to an outer namespace, the inner name hides the outer name. Also, the namespace-qualified name of an inner namespace starts with the name of the outermost namespace, followed by the scope operator, followed by the name of the next-to-the-outermost namespace, followed by the scope operator, etc., until reaching the inner namespace. 27

28 // NamespaceNested.cc #include <iostream> #include <string> using namespace std; namespace N1 { typedef int Type; namespace N2 { typedef int* Type; namespace N3 { typedef string Type; namespace N4 { typedef string* Type; int main() { using namespace N1; Type x = 10; // Type is int cout << x << endl; // 10 28

29 N1::N2::Type p = &x; // Type is int* cout << *p << endl; // 10 N1::N2::N3::Type str( "hello" ); cout << str << endl; N1::N2::N3::N4::Type q = &str; cout << *q << endl; namespace N_FOUR = N1::N2::N3::N4; N_FOUR::Type ptr = &str; cout << *ptr << endl; // Type is string // "hello" // Type is string* // "hello" // namespace alias // "hello" return 0; 29

30 Unnamed Namespaces When defining a namespace, you are allowed to omit the name of the namespace: namespace { int buffer; class X; void foo(); typedef string* T; The compiler internally generates a unique name for such a namespace. Furthermore, a using directive is automatically assumed for an unnamed namespace. 30

31 So, in effect, an unnamed namespace declaration is equivalent to namespace ---UNIQUE_NAME--- { int buffer; class X; void foo(); typedef string* T; using namespace ---UNIQUE_NAME---; 31

32 Names introduced into the current scope through an unnamed namespace possess internal linkage the same linkage as for a global name that is declared to be static. When you declare a global name in file A to be static, its scope is limited to file A. It cannot be linked to from a file B by the use of extern declaration in file B. That allows you to use the same name in file B for a different purpose without fear of creating a name clash with file A. An unnamed namespace is meant to be a cleaner way of achieving the same effect. The use of the global static in C++ programs for achieving internal linkage for a name is being deprecated. 32

33 Koenig Lookup for Unqualified Function Names So far we have seen three different ways in which the namespace definition of a name can be accessed outside the namespace. We can either use a namespace-qualified name, or resort to a using declaration, or to a using directive in the manner shown earlier. For unqualified function names, there is yet another way that allows the system to access the namespace definition of a function name through Koenig lookup. 33

34 //Koenig.cc #include <iostream> using namespace std; namespace Module1 { class X {; void foo( X xobj ) { cout << "Module1 s foo(x) invoked"; namespace Module2 { void foo( Module1::X xobj ) { cout << "X s foo(x) invoked"; void foo( int i ) { cout << "global foo(int) invoked"; int main() { Module1::X xob; foo( 1 ); foo( xob ); return 0; // global foo(int) invoked // Module1 s foo(x) invoked 34

35 Access Control for Class Members (Section 3.11) Every member of a class has associated with it an access control property. In C++, a member can be private, protected, or public. In addition to these three, Java also allows for the access control property of a member to be package. When the access control modifier is left unspecified for a class member in Java, it is of type package. 35

36 Access Control in C++: All members of a C++ class are private unless declared explicitly to be otherwise. Public members of a class are accessible to all other classes and functions. Members that are private to a class can be accessed by only those functions that are defined specifically for that class. The access control modifier protected is used for a member if we wish for that member to be accessible to only the subclasses of that class. (A protected member of a class acts like a public member for the subclasses derived from that class, but like a private member for the rest of the program.) 36

37 The private members of a class are also accessible to the friends of that class. 37

38 //Friend.cc #include <iostream> class Y; //(A) class X { int m; int n; public: X( int mm, int nn ) { m = mm; n = nn; friend Y; friend void print( X* ); ; //(B) //(C) class Y { X* x; int t; public: Y( X* xobj ) { x = xobj; t = x->m + x->n; //(D) int get_t() { return t; ; void print( X* ptr ) { cout << ptr->m << " " << ptr->n << endl; //(E) 38

39 int main() { X* ptr = new X(100, 200); Y y( ptr ); cout << y.get_t() << endl; // 300 print( ptr ); // return 0; 39

40 A friend declaration may be placed in any section of a class, private, protected, or public. It carries the same meaning regardless of where it appears. 40

41 Access Control in Java: In addition to the modifiers public, private, and protected, Java has one more: package. The modifiers public and private carry the same meaning in Java as they do in C++. The modifier protected also carries the same meaning, except that such members are like public members in the same package. In other words, a protected class member will be accessible inside all classes in the same package, but to only the subclasses in other package. 41

42 When no access modifier is specified in Java, that means the member is of type package. Such members are no different from public members within the same package. But they act like private members with respect to other packages. 42

43 Abstract Classes and Interfaces (Section 3.12) Abstract classes are very important to object-oriented programming. While the notion of an abstract class is common to both C++ and Java, the latter also supports a variant thereof interfaces. A Java interface is an abstract class that is not allowed to contain any implementation code at all for any of the member functions. An interface is also not allowed any data members that can be given values on a per object basis. 43

44 Why abstract classes are useful: An abstract class can lend organization to the other classes in a class hierarchy. An abstract class can represent a specialized behavior, which when mixed with the other classes gives us classes with that behavior. Abstract classes can help build up an implementation incrementally. 44

45 An abstract class can help with knowledge organization in an OO program by serving as a root class of a hierarchy of other concrete classes and thus pull together what might otherwise be disparate bits of knowledge encapsulated in the subclasses. Shape Circle Rectangle... 45

46 46

47 C++: class Shape { public: virtual double area( ) = 0; virtual double circumference() = 0; //... ; //(A) //(B) JAVA: abstract class Shape { abstract public double area( ); abstract public double circumference(); //... 47

48 interface Collection { public boolean add( Object o ); public boolean remove( Object o ); // other methods The methods declared in an interface are always public, implicitly so if their access privilege is not so stated. 48

49 Comparing Objects (Section 3.13) The basic issues in object comparison relate to what can be compared; and how to compare. class Apple { class Orange { Taste taste; Taste taste; Size size; Size size; Weight wt; Weight wt; //... //... ; ; 49

50 There are two kinds of comparisons that one can make for class type objects: We may wish to know whether or not two objects are identical on the basis of equal values for one or more of the data members. The result of such a comparison is either true or false. Or, we may wish to know whether one object is smaller than, equal to, or greater than another object, again on the basis of the values for one or more data members of the objects involved. 50

51 In C++, the first kind of comparison is yielded typically by the == operator. The programmer has to overload this operator for a given class. The second type of comparison in C++ is implemented by defining an appropriate comparison function that returns the three values needed. 51

52 Our discussion so far on object comparison has been centered primarily on a comparison of two objects on the basis of their content, meaning on the basis of the values of one or more the data members of the objects. Java adds an additional twist to this it allows object comparisons on the basis of equality of reference. Two objects are equal on the basis of equality of reference if they are the same object in the memory. 52

53 In Java, comparison of two objects on the basis of equality of reference is carried out by the == operator. And a comparison on the basis of content can be carried out by a programmersupplied definition for the equals() method that every class in Java inherits from the root class Object. 53

54 class X { int p; X( int m ) { p = m; //... X x1 = new X( 10 ); X x2 = x1; x1 == x2; // true X x1 = new X( 10 ); X x2 = new X( 10 ); x1 == x2; // false If you do not supply your own override definition forequals thatxinherits from Object, comparisons using equals behave in exactly the same manner as comparisons using ==: 54

55 X x1 = new X( 10 ); X x2 = x1; x1.equals( x2 ); X x3 = new X( 10 ); x1.equals( x3 ); // true // false 55

56 //EqualityTest.java class X { int p; int q; X( int m, int n ) { p = m; q = n; boolean equals( X other ) { return p == other.p; class Test { public static void main( String[] args ) { X x1 = new X( 10, 100 ); // x1 and X x2 = new X( 10, ); // x2 look very differen if ( x1.equals( x2 ) ) // but they are equal System.out.println( "x1 and x2 are equal" ); 56

57 The Java platform also uses the notion of natural ordering for comparing class type objects. The objects of a Java class exhibit natural ordering if the class has implemented the java.lang.comparable interface. Such a class must provide an implementation for the compareto method referred to as the class s natural comparison method that can then be used by the algorithms and the data structures for comparing data objects. The compareto method must return a negative integer, a zero, or a positive integer if the object on which it is invoked is less than, equal to, or greater than the argument object. 57

58 Many of the system supplied classes in Java possess natural ordering. These include String, Integer, Float, Double, Date, File and many others. For the String class, the natural order is lexicographic; it is chronological for the Date class; lexicographic on the pathname for the File class, etc. 58

59 Template Classes A templatized C++ program can work with different types of data types. For example, a templatized C++ linked-list can be used to hold elements of type int, double, char,etc. class X{ T datum; public: // constructor, etc. ; // type T not known in advance 59

60 template <class T> class X { T datum; public: X( T dat ) : datum( dat ) { T getdatum(){ return datum; ; 60

61 #include <string> #include <iostream> template <class T> class X { T datum; public: X( T dat ) : datum( dat ) { T getdatum(){ return datum; ; int main() { int x = 100; X<int> xobj_1( x ); double d = 1.234; X<double> xobj_2( d ); string str = "hello"; X<string> xobj_3( str ); string ret1 = xobj_3.getdatum(); cout << ret1 << endl; // output: hello // int ret2 = xobj_3.getdatum(); // Error 61

Extending Classes (contd.) (Chapter 15) Questions:

Extending Classes (contd.) (Chapter 15) Questions: Extending Classes (contd.) (Chapter 15) Questions: 1 1. The following C++ program compiles without any problems. When run, it even prints out the hello called for in line (B) of main. But subsequently

More information

Extending Classes (contd.) (Chapter 15) Questions:

Extending Classes (contd.) (Chapter 15) Questions: Extending Classes (contd.) (Chapter 15) Questions: 1 Virtual Functions in C++ Employee /\ / \ ---- Manager 2 Case 1: class Employee { string firstname, lastname; //... Employee( string fnam, string lnam

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

More information

STRUCTURING OF PROGRAM

STRUCTURING OF PROGRAM Unit III MULTIPLE CHOICE QUESTIONS 1. Which of the following is the functionality of Data Abstraction? (a) Reduce Complexity (c) Parallelism Unit III 3.1 (b) Binds together code and data (d) None of the

More information

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value Paytm Programming Sample paper: 1) A copy constructor is called a. when an object is returned by value b. when an object is passed by value as an argument c. when compiler generates a temporary object

More information

CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

Object Reference and Memory Allocation. Questions:

Object Reference and Memory Allocation. Questions: Object Reference and Memory Allocation Questions: 1 1. What is the difference between the following declarations? const T* p; T* const p = new T(..constructor args..); 2 2. Is the following C++ syntax

More information

C++ Scope Resolution Operator ::

C++ Scope Resolution Operator :: C++ Scope Resolution Operator :: C++The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global

More information

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

Ch02. True/False Indicate whether the statement is true or false. Ch02 True/False Indicate whether the statement is true or false. 1. The base class inherits all its properties from the derived class. 2. Inheritance is an is-a relationship. 3. In single inheritance,

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 6 Example Activity Diagram 1 Outline Chapter 6 Topics 6.6 C++ Standard Library Header Files 6.14 Inline Functions 6.16 Default Arguments 6.17 Unary Scope Resolution Operator

More information

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

More information

Data Abstraction. Hwansoo Han

Data Abstraction. Hwansoo Han Data Abstraction Hwansoo Han Data Abstraction Data abstraction s roots can be found in Simula67 An abstract data type (ADT) is defined In terms of the operations that it supports (i.e., that can be performed

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy CSCI 334: Principles of Programming Languages Lecture 18: C/C++ Homework help session will be tomorrow from 7-9pm in Schow 030A instead of on Thursday. Instructor: Dan Barowy HW6 and HW7 solutions We only

More information

Array. Prepared By - Rifat Shahriyar

Array. Prepared By - Rifat Shahriyar Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types

More information

Classes, The Rest of the Story

Classes, The Rest of the Story Classes, The Rest of the Story 1 1. Is the following syntax legal in Java: class MyException extends Exception { class Test { static void f( ) throws MyException { throw MyException(); // (A) public static

More information

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007 ECE 462 Midterm Exam 1 10:30-11:20AM, September 21, 2007 1 Template Classes and the STL Library 1.1 Container Classes Which statement is correct? Answer: B A. An element can be inserted anywhere in a stack.

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

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

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year Object Oriented Programming Assistant Lecture Omar Al Khayat 2 nd Year Syllabus Overview of C++ Program Principles of object oriented programming including classes Introduction to Object-Oriented Paradigm:Structures

More information

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 6 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 21, 2013 Question 1 What is the output of the following C++ program? #include #include using namespace

More information

CS260 Intro to Java & Android 03.Java Language Basics

CS260 Intro to Java & Android 03.Java Language Basics 03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Lecture 3: Introduction to C++ (Continue) Examples using declarations that eliminate the need to repeat the std:: prefix 1 Examples using namespace std; enables a program to use

More information

W3101: Programming Languages C++ Ramana Isukapalli

W3101: Programming Languages C++ Ramana Isukapalli Lecture-6 Operator overloading Namespaces Standard template library vector List Map Set Casting in C++ Operator Overloading Operator overloading On two objects of the same class, can we perform typical

More information

25. Interfaces. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

25. Interfaces. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 25. Interfaces Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Definition The Comparable Interface Interfaces vs. Abstract Classes Creating Custom Interfaces References Definition Definition Sometimes

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

What about Object-Oriented Languages?

What about Object-Oriented Languages? What about Object-Oriented Languages? What is an OOL? A language that supports object-oriented programming How does an OOL differ from an ALL? (ALGOL-Like Language) Data-centric name scopes for values

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

Computer Programming : C++

Computer Programming : C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

More information

9 Working with the Java Class Library

9 Working with the Java Class Library 9 Working with the Java Class Library 1 Objectives At the end of the lesson, the student should be able to: Explain object-oriented programming and some of its concepts Differentiate between classes and

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

2 nd Week Lecture Notes

2 nd Week Lecture Notes 2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous

More information

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR 603203 DEPARTMENT OF COMPUTER SCIENCE & APPLICATIONS QUESTION BANK (2017-2018) Course / Branch : M.Sc CST Semester / Year : EVEN / II Subject Name

More information

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages Preliminaries II 1 Agenda Objects and classes Encapsulation and information hiding Documentation Packages Inheritance Polymorphism Implementation of inheritance in Java Abstract classes Interfaces Generics

More information

Chapter 1: Object-Oriented Programming Using C++

Chapter 1: Object-Oriented Programming Using C++ Chapter 1: Object-Oriented Programming Using C++ Objectives Looking ahead in this chapter, we ll consider: Abstract Data Types Encapsulation Inheritance Pointers Polymorphism Data Structures and Algorithms

More information

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING THROUGH C++ Practical: OOPS THROUGH C++ Subject Code: 1618407 PROGRAM NO.1 Programming exercise on executing a Basic C++

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

Java: introduction to object-oriented features

Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

Bruce Merry. IOI Training Dec 2013

Bruce Merry. IOI Training Dec 2013 IOI Training Dec 2013 Outline 1 2 3 Outline 1 2 3 You can check that something is true using assert: #include int main() { assert(1 == 2); } Output: test_assert: test_assert.cpp:4: int main():

More information

OBJECT ORİENTATİON ENCAPSULATİON

OBJECT ORİENTATİON ENCAPSULATİON OBJECT ORİENTATİON Software development can be seen as a modeling activity. The first step in the software development is the modeling of the problem we are trying to solve and building the conceptual

More information

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון Overriding עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap A method in a child class overrides a method in the parent class if it has the same name and type signature: Parent void method(int,float)

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009 From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3 Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009 C++ Values, References, and Pointers 1 C++ Values, References, and Pointers 2

More information

26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini 26. Interfaces Java Fall 2009 Instructor: Dr. Masoud Yaghini Outline Definition The Comparable Interface Interfaces vs. Abstract Classes Creating Custom Interfaces References Definition Definition Single

More information

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

More information

Introduction to C++ Friends, Nesting, Static Members, and Templates Topic #7

Introduction to C++ Friends, Nesting, Static Members, and Templates Topic #7 Introduction to C++ Friends, Nesting, Static Members, and Templates Topic #7 CS202 7-1 Relationship of Objects Friends, Nesting Static Members Template Functions and Classes Reusing Code Template Specializations

More information

10. Functions (Part 2)

10. Functions (Part 2) 10.1 Overloaded functions 10. Functions (Part 2) In C++, two different functions can have the same name if their parameters are different; either because they have a different number of parameters, or

More information

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

More information

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

CS 162, Lecture 25: Exam II Review. 30 May 2018 CS 162, Lecture 25: Exam II Review 30 May 2018 True or False Pointers to a base class may be assigned the address of a derived class object. In C++ polymorphism is very difficult to achieve unless you

More information

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2. Comparison of Programming Paradigms 3. Basic Object Oriented Programming

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Storage Classes Scope Rules Functions with Empty Parameter Lists Inline Functions References and Reference Parameters Default Arguments Unary Scope Resolution Operator Function

More information

Class, Variable, Constructor, Object, Method Questions

Class, Variable, Constructor, Object, Method Questions Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx

More information

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

Chapter 10 Classes Continued. Fundamentals of Java

Chapter 10 Classes Continued. Fundamentals of Java Chapter 10 Classes Continued Objectives Know when it is appropriate to include class (static) variables and methods in a class. Understand the role of Java interfaces in a software system and define an

More information

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() { Scoping, Static Variables, Overloading, Packages In this lecture, we will examine in more detail the notion of scope for variables. We ve already indicated that variables only exist within the block they

More information

A Short Summary of Javali

A Short Summary of Javali A Short Summary of Javali October 15, 2015 1 Introduction Javali is a simple language based on ideas found in languages like C++ or Java. Its purpose is to serve as the source language for a simple compiler

More information

index.pdf January 21,

index.pdf January 21, index.pdf January 21, 2013 1 ITI 1121. Introduction to Computing II Circle Let s complete the implementation of the class Circle. Marcel Turcotte School of Electrical Engineering and Computer Science Version

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes

More information

CSCE3193: Programming Paradigms

CSCE3193: Programming Paradigms CSCE3193: Programming Paradigms Nilanjan Banerjee University of Arkansas Fayetteville, AR nilanb@uark.edu http://www.csce.uark.edu/~nilanb/3193/s10/ Programming Paradigms 1 Java Packages Application programmer

More information

Recharge (int, int, int); //constructor declared void disply();

Recharge (int, int, int); //constructor declared void disply(); Constructor and destructors in C++ Constructor Constructor is a special member function of the class which is invoked automatically when new object is created. The purpose of constructor is to initialize

More information

Comments are almost like C++

Comments are almost like C++ UMBC CMSC 331 Java Comments are almost like C++ The javadoc program generates HTML API documentation from the javadoc style comments in your code. /* This kind of comment can span multiple lines */ //

More information

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017 Programming Language Concepts Object-Oriented Programming Janyl Jumadinova 28 February, 2017 Three Properties of Object-Oriented Languages: Encapsulation Inheritance Dynamic method binding (polymorphism)

More information

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli Identify and overcome the difficulties encountered by students when learning how to program List and explain the software development roles played by students List and explain the phases of the tight spiral

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

Points To Remember for SCJP

Points To Remember for SCJP Points To Remember for SCJP www.techfaq360.com The datatype in a switch statement must be convertible to int, i.e., only byte, short, char and int can be used in a switch statement, and the range of the

More information

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank 1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A. integer B 1.427E3 B. double D "Oct" C. character B -63.29 D. string F #Hashtag

More information

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

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Objectives Classes & Objects ( 9.2). UML ( 9.2). Constructors ( 9.3). How to declare a class & create an object ( 9.4). Separate a class declaration from a class implementation

More information

Inheritance and Interfaces

Inheritance and Interfaces Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical

More information

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

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Variables. Data Types.

Variables. Data Types. Variables. Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting

More information

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods. Inheritance Inheritance is the act of deriving a new class from an existing one. Inheritance allows us to extend the functionality of the object. The new class automatically contains some or all methods

More information

4. Structure of a C++ program

4. Structure of a C++ program 4.1 Basic Structure 4. Structure of a C++ program The best way to learn a programming language is by writing programs. Typically, the first program beginners write is a program called "Hello World", which

More information

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS Chapter 1 : Chapter-wise Java Multiple Choice Questions and Answers Interview MCQs Java Programming questions and answers with explanation for interview, competitive examination and entrance test. Fully

More information

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8 Today... Java basics S. Bowers 1 of 8 Java main method (cont.) In Java, main looks like this: public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Q: How

More information

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy CAAM 420 Fall 2012 Lecture 29 Duncan Eddy November 7, 2012 Table of Contents 1 Templating in C++ 3 1.1 Motivation.............................................. 3 1.2 Templating Functions........................................

More information

Programming Language Concepts: Lecture 2

Programming Language Concepts: Lecture 2 Programming Language Concepts: Lecture 2 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 2, 19 January 2009 Classes and

More information

Comp-304 : Object-Oriented Design What does it mean to be Object Oriented?

Comp-304 : Object-Oriented Design What does it mean to be Object Oriented? Comp-304 : Object-Oriented Design What does it mean to be Object Oriented? What does it mean to be OO? What are the characteristics of Object Oriented programs (later: OO design)? What does Object Oriented

More information

Data Structures using OOP C++ Lecture 3

Data Structures using OOP C++ Lecture 3 References: th 1. E Balagurusamy, Object Oriented Programming with C++, 4 edition, McGraw-Hill 2008. 2. Robert L. Kruse and Alexander J. Ryba, Data Structures and Program Design in C++, Prentice-Hall 2000.

More information

Declarations and Access Control SCJP tips

Declarations and Access Control  SCJP tips Declarations and Access Control www.techfaq360.com SCJP tips Write code that declares, constructs, and initializes arrays of any base type using any of the permitted forms both for declaration and for

More information

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

COMP322 - Introduction to C++ Lecture 02 - Basics of C++ COMP322 - Introduction to C++ Lecture 02 - Basics of C++ School of Computer Science 16 January 2012 C++ basics - Arithmetic operators Where possible, C++ will automatically convert among the basic types.

More information

a. a * c - 10 = b. a % b + (a * d) + 7 =

a. a * c - 10 = b. a % b + (a * d) + 7 = Exam #2 CISC1110, MW 10:35-12:40pm Fall 2011 Name 1 Evaluate each expression according to C++ rules (8 pts) Given: Integers a = 3, b = 2, c = 5, and float d = 40 a a * c - 10 = b a % b + (a * d) + 7 =

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information