Introduction to Classes

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Road Map Introduction to object oriented programming. Classes
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
CS0007: Introduction to Computer Programming Introduction to Arrays.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
1 Classes and Objects in Java Basics of Classes in Java.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Learners Support Publications Classes and Objects.
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.
 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.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Defining a ClasstMyn1 Defining a Class A class is a template that defines the form of an object. It specifies both the data and the code that will operate.
What is an Object? Real world objects are things that have: 1) state 2) behavior Example: your dog: 1) state – name, color, breed, sits?, barks?, wages.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Introduction to Object-Oriented Programming Lesson 2.
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.
Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Procedural and Object-Oriented Programming
Topic: Classes and Objects
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
User-Written Functions
Chapter 13: Overloading and Templates
Introducing Classes By: Pavan D.M.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Programming with ANSI C ++
Java Primer 1: Types, Classes and Operators
FUNCTIONS In C++.
Functions, locals, parameters, and separate compilation
Classes and Objects in Java
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Chapter 3: Using Methods, Classes, and Objects
Classes and Objects in Java
Classes and Objects in Java
Chapter 5 Classes.
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Modern JavaScript Develop And Design
JavaScript: Functions.
Object-Oriented Programming: Classes and Objects
Memberwise Assignment / Initialization
Lecture 4-7 Classes and Objects
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Constructor Overloading
Introducing Classes By: Pavan D.M.
Introduction to Classes
CS5253 Workshop I Lecturer: Dr. Lusheng Wang
Unit-1 Introduction to Java
Classes and Objects.
Java – Inheritance.
Classes Lecture 7 from Chapter /1/11.
Tonga Institute of Higher Education
JAVA 22 February 2019 DEPARTMENT OF CSE.
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
Methods.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
OOP With Java/ course1 Sundus Abid-Almuttalib
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
SPL – PS3 C++ Classes.
String Objects & its Methods
class Box { double width; double height; double depth; }
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Introduction to Classes Prof.Pavan D.M Asst.Prof. CSE Dept.

General Forms of Class a class is a template for an object, and an object is an instance of a class. A Class contains 2 parts Code and Data. Very simple class may contain either code or data. A class is declared by use of the class keyword. A simplified general form of a class definition is shown here: class classname { type instance-variable1; type instance-variable2; ------------------------ type instance-variableN; type methodname1(parameter-list) { // body of method } type methodname2(parameter-list) { // ... type methodnameN(parameter-list) {

Contd… The data, or variables, defined within a class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class. the instance variables are accessed by the methods defined for that class. Variables defined within a class are called instance variables because each object of the class contains its own copy of these variables. Thus, the data for one object is separate and unique from the data for another.

A Simple Class class Box { Here is a class called Box that defines three instance variables: width, height, and depth. class Box { double width; double height; double depth; } a class defines a new type of data. In this case, the new data type is called Box. It is important to remember that a class declaration only creates a template; it does not create an actual object. To actually create a Box object, you will use a statement like the following: Box mybox = new Box(); // create a Box object called mybox After this statement executes, mybox will be an instance of Box. Each time you create an instance of a class, you are creating an object that contains its own copy of each instance variable defined by the class.

mybox = new Box(); // allocate a Box object Contd.. Thus, every Box object will contain its own copies of the instance variables width, height, and depth. To access these variables, you will use the dot (.) operator. The dot operator links the name of the object with the name of an instance variable. For example, to assign the width variable of mybox the value 100 mybox.width = 100; Declaring Objects: when you create a class, you are creating a new data type. You can use this type to declare objects of that type. However, obtaining objects of a class is a two-step process. 1) you must declare a variable of the class type. Box mybox; 2) you must acquire an actual, physical copy of the object and assign it to that variable. You can do this using the new operator. mybox = new Box(); // allocate a Box object Combining these 2 statements we can write it in a line. Box mybox=new Box( ); What is job of new operator?

Contd.. The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it. It has this general form: class-var = new classname( ); The class name followed by parentheses specifies the constructor for the class. It is important to understand that new allocates memory for an object during run time.

Assigning Object Reference Variables Box b1 = new Box(); Box b2 = b1; b2 is being assigned a reference to a copy of the object referred to by b1. b1 and b2 will both refer to the same object. The assignment of b1 to b2 did not allocate any memory or copy any part of the original object. It simply makes b2 refer to the same object as does b1. Thus, any changes made to the object through b2 will affect the object to which b1 is referring, since they are the same object.

Contd.. Even though b1 and b2 both refer to same object they are not linked in any other way. Box b1 = new Box(); Box b2 = b1; // ... b1 = null; Here, b1 has been set to null, but b2 still points to the original object. METHODS: This is the general form of a method: type name(parameter-list) { // body of method } Adding a Method to the Box Class: We can add any number of methods inside the class. Ex: Let us add a method to Box class – volume( )

Program