Developed By Strawberry

Similar documents
Developed By Strawberry

Developed By Strawberry

Developed By Strawberry

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Developed By Strawberry

Constructor - example

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

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

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Data Structures using OOP C++ Lecture 3


Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };

CONSTRUCTOR AND DESTRUCTOR

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

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

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I.

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Short Notes of CS201

University of Toronto

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

CS201 - Introduction to Programming Glossary By

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Cpt S 122 Data Structures. Introduction to C++ Part II

CSC1322 Object-Oriented Programming Concepts

Constructors for classes

PESIT Bangalore South Campus

B.Sc. (Hons.) Computer Science I B.Sc. (Hons.) Electronics. (i) Runtime polymorphism and compile time polymorphism

Algorithms & Data Structures

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

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

Object Oriented Programming

CS 247: Software Engineering Principles. ADT Design

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Module Operator Overloading and Type Conversion. Table of Contents

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

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

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

EECS168 Exam 3 Review

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

For Teacher's Use Only Q No Total Q No Q No

Sai Nath University. Assignment For MCA 2nd Sem.

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Introduction Of Classes ( OOPS )

Instantiation of Template class

Get Unique study materials from

C++ Programming Fundamentals

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

COMPUTER APPLICATIONS

More C++ : Vectors, Classes, Inheritance, Templates

C++ 8. Constructors and Destructors

Java Primer 1: Types, Classes and Operators

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

C++ Important Questions with Answers

Template Issue Resolutions from the Stockholm Meeting

Lab 4 - Lazy Deletion in BSTs

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS304 Object Oriented Programming Final Term

III. Classes (Chap. 3)

Sai Nath University. Assignment For BCA 3 RD Sem.

END TERM EXAMINATION

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Media Computation. Lecture 16.1, December 8, 2008 Steve Harrison

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Declarations and Access Control SCJP tips

2. COURSE DESIGNATION: 3. COURSE DESCRIPTIONS:

CGS 2405 Advanced Programming with C++ Course Justification

TEMPLATE IN C++ Function Templates

CIS Intro to Programming in C#

Programming 2. Object Oriented Programming. Daniel POP

INHERITANCE PART 2. Constructors and Destructors under. Multiple Inheritance. Common Programming Errors. CSC 330 OO Software Design 1

Tokens, Expressions and Control Structures

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

CMSC 132: Object-Oriented Programming II

C11: Garbage Collection and Constructors

Computer Programming

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Chapter 4 Defining Classes I

COMPUTER SCIENCE (083)

A First Program - Greeting.cpp

Fundamentals of Programming. Lecture 19 Hamed Rasifard

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Inheritance, and Polymorphism.

Savitribai Phule Pune University, Pune Second Year of Computer Engineering (2015 Course) : Object Oriented Programming Lab Teaching Scheme

Note: The buy help from the TA for points will apply on this exam as well, so please read that carefully.

Computer Programming

Computer Programming: C++

Transcription:

Experiment No. 4 PART A (PART A: TO BE REFFERED BY STUDENTS) A.1. Aim: To understand the following concept of constructors. 1. To create constructor. 2. To understand concept of no argument constructor. 3. To understand the concept of default constructor. 4. To understand the concept of parameterized constructor. P1: Create a class called time that has separate integer member data for hours, minutes, and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. Another member function should display it, in HH:MM:SS format. The final member function should add two objects of type time passed as arguments. A main() program should create two initialized time objects and one that isn t initialized. Then it should add the two initialized values together, leaving the result in the third time variable. Finally it should display the value of this third variable. Make appropriate member functions. A.2. Prerequisite: Sr.No Concepts 1. Constructors A.3. Outcome: A.4. Theory: Constructors: After successful completion of this experiment students will be able to A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of the data members of the class. A constructor is declared and defined as follows: //class with a constructor class xyz int m, n;

public: xyz(void); // constructor declared ; xyz :: xyz(void) // constructor defined m=0; n=0; When a class contains a constructor it is guaranteed that an object created by the class will be initialized automatically. For example, the declaration xyz obj1; // object obj1 created not only creates the object obj1 of type xyz but also initializes its data members m and n to zero. A constructor that accepts no parameters is called the default constructor. The default constructor for class A is A::A(). If no such constructor is defined, then the compiler supplies a default constructor. Therefore, a statement such as A a; Invokes the default constructor of the compiler to create the object a. The constructor functions have the following characteristics: They should be declared in public section They are invoked automatically when the objects are created. They do not have return types not even void, and therefore, they cannot return values. They cannot be inherited, though a derived class can call the base class constructor. Like other C++ functions, they can have default arguments. Constructors cannot be virtual We cannot refer to their addresses An object with a constructor cannot be used as a member of a union. They make implicit calls to the operators new and delete when memory allocation is required. Parameterized constructor: The constructors that can take arguments are called parameterized constructors. The constructor xyz() may be modified to take arguments as shown below: class xyz int m, n; public:

xyz(int x, int y);.. ; xyz :: xyz(int x, int y) m=x; n=y; // parameterized constructor When a constructor has been parameterized, we must pass the initial values as arguments to the constructor function when an object is declared. This can be done in two ways: By calling the constructor explicitly. xyz obj1 = xyz(0,100); // explicit call By calling the constructor implicitly. xyz obj1(0,100); // implicit call Multiple constructors in a class: C++ permits the use of multiple constructors in the same class. For example, we could define a class as follows: class xyz int m, n; public: xyz() // constructor 1 m=0; n=0; xyz(int x, int y) // constructor 2 m=x; n=y; xyz( xyz &i) // constructor 3 m=i.m; n=i.n; ;

This declares three constructors for a xyz object. The first constructor receives no arguments, the second receives two integer arguments and the third receives one xyz object as an argument. For example, the declaration xyz obj1; would automatically invoke the first constructor and set both m and n of onj1 to zero. The statement xyz obj2(20,40); would call the second constructor and set both m and n of obj2 to 20 and 40 respectively. Finally the statement xyz obj3(obj2); would invoke the third constructor which copies the values of obj2 into obj3. In other words, it sets the value of every data element of obj3 to the values of the corresponding data element of obj2. Such a constructor is called the copy constructor. When more than one constructor function is defined in a class, we say that the constructor is overloaded. Constructor with default arguments: It is possible to define constructors with default arguments. For example, the constructor xyz() can be declared as follows: xyz( int x, int y=0); the default value of the argument y is zero. Then, the statement xyz obj(5); Assigns the value 5 to m and 0 to n (by default).

PART B (PART B: TO BE COMPLETED BY STUDENTS) (Students must submit the soft copy as per following segments within two hours of the practical. The soft copy must be uploaded on the Blackboard or emailed to the concerned lab in charge faculties at the end of the practical in case the there is no Black board access available) Roll No. N008 Name: AKSHAY BANDA Program: MBA TECh CS Division: C Semester: 2 Batch : C1 Date of Experiment: 28/1/15 Date of Submission: 28/1/15 Grade : B.1. Software Code written by student: (Paste your C++ code completed during the 2 hours of practical in the lab here) 1. #include<iostream> using namespace std; class time private: int h,m,s; public: time() h=0; m=0; s=0; time(int a, int b, int c) h=a; m=b; s=c; time add(time a, time b) time c; c.h=a.h+b.h; c.m=a.m+b.m; c.s=a.s+b.s; if(c.s>=60)

c.m=m+(c.s/60); c.s=c.s%60; if(c.m>=60) c.h=c.h+(c.m/60); c.m=c.m%60; return c; void display() cout<<"time is "<<h<<":"<<m<<":"<<s; ; int main() int h,m,s; cout<<"\nenter hours :"; cin>>h; cout<<"\nenter minutes : "; cin>>m; cout<<"\nenter seconds : "; cin>>s; time x(h,m,s); cout<<"\nenter hours :"; cin>>h; cout<<"\nenter minutes : "; cin>>m; cout<<"\nenter seconds : "; cin>>s; time y(h,m,s); time z; z=z.add(x,y); z.display(); B.2. Input and Output:

(Paste your program input and output in following format. If there is error then paste the specific error in the output part. In case of error with due permission of the faculty extension can be given to submit the error free code with output in due course of time. Students will be graded accordingly.) 1. Enter hours :7 Enter minutes : 7 Enter seconds : 7 Enter hours :18 Enter minutes : 18 Enter seconds : 18 Time is 25:25:25 Process returned 0 (0x0) execution time : 4.188 s Press any key to continue. B.3. Conclusion: (Students must write the conclusion as per the attainment of individual outcome listed above and learning/observation noted in section B.1) I learned to create constructor, understand concept of no argument constructor, concept of default constructor, concept of parameterized constructor.