Object oriented programming

Similar documents
Објектно ориентирано програмирање

Object oriented programming

Object oriented programming

Object oriented programming

Object oriented programming

Object oriented programming

Inheritance, and Polymorphism.

Object oriented programming

Object oriented programming

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Exercise 1.1 Hello world

Lab Instructor : Jean Lai

Linked List using a Sentinel

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Programming Language. Functions. Eng. Anis Nazer First Semester

C++ basics Getting started with, and Data Types.

Ch 6. Functions. Example: function calls function

Summary of basic C++-commands

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

AN OVERVIEW OF C++ 1

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Programming in C++: Assignment Week 8

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

CMSC 202 Midterm Exam 1 Fall 2015

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

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

CSCE 110 PROGRAMMING FUNDAMENTALS

C++ Final Exam 2017/2018

C C C C++ 2 ( ) C C++ 4 C C

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

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

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

Short Notes of CS201

do { statements } while (condition);

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

CSCE Practice Midterm. Data Types

CS201 - Introduction to Programming Glossary By

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

More Tutorial on C++:

Problem Solving: Storyboards for User Interaction

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

Unit 6. Thinking with Functions

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Input And Output of C++

Name. CPTR246 Spring '17 (100 total points) Exam 2

Intermediate Programming, Spring 2017*

Due Date: See Blackboard

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Introduction Of Classes ( OOPS )

2 2

CSCE 110 PROGRAMMING FUNDAMENTALS

THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering. Computer Programming (ENG236) - Homework 4

C:\Temp\Templates. Download This PDF From The Web Site

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

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Constructor - example

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

Data Structures (INE2011)

CS 115 Exam 3, Spring 2010

C++ For Science and Engineering Lecture 12

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Do not turn to the next page until the start of the exam.

Use the dot operator to access a member of a specific object.

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

LAB 4.1 Relational Operators and the if Statement

CSE030 Fall 2012 Final Exam Friday, December 14, PM

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

Module 7 b. -Namespaces -Exceptions handling

1. The term STL stands for?

A SHORT COURSE ON C++

Today USING POINTERS. Functions: parameters and arguments. Todaywewilllookattopicsrelatingtotheuseofpointers

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CSC1322 Object-Oriented Programming Concepts

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

CS

Classes and Objects. Instructor: 小黑

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

Program template-smart-pointers-again.cc

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Question 1 Consider the following structure used to keep employee records:

Review of Important Topics in CS1600. Functions Arrays C-strings

Fast Introduction to Object Oriented Programming and C++

Getting started with C++ (Part 2)

Lecture 7. Log into Linux New documents posted to course webpage

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Introduction to Programming using C++

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

1. In C++, reserved words are the same as predefined identifiers. a. True

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

Program template-smart-pointers.cc

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Transcription:

Exercises 3 Version 1.0, 24 February, 2017

Table of Contents 1. Classes...................................................................... 1 1.1. Defining class.......................................................... 1 1.2. Creating (instantiating) objects............................................ 1 1.3. Objects lifecycle......................................................... 2 2. Problems.................................................................... 2 2.1. Triangle................................................................ 2 2.2. Employee............................................................... 3 2.3. E-mail.................................................................. 4 3. Source code of the examples and problems..................................... 6

1. Classes 1.1. Defining class Syntax class class_name { /* variables and methods not visible outside of class */ public : /* variables and methods visible outside of class */ ; Example oop_av3_ex1.cpp class Cuboid { int a, b, c; public: // Default constructor Cuboid(){ cout << "Default constructor" << endl; // Constructor with arguments Cuboid(int aa, int bb, int cc) { a = aa; b = bb; c = cc; cout << "Constructor" << endl; ~Cuboid() { cout << "Destructor" << endl; // Method for computing area int area() { return 2 * (a * b + a * c + b * c); // Method for computing volume int volume() { return a * b * c; ; 1.2. Creating (instantiating) objects Syntax class_name object_name; Example oop_av3_ex1.cpp Cuboid c(10, 15, 20); 1. Classes 1

1.3. Objects lifecycle Constructor - special method of each class which is called always when an object of the class is created (instantiated) Destructor - special method of each class which is called to free the memory that the instantiated object allocated 2. Problems 2.1. Triangle Write a class for geometric figure triangle. In the class implement methods for computing the area and perimeter of the triangle. Then, write a main function where you will instantiate one object of this class with values read from SI. Call the methods of this object for computing area and perimeter. Solution oop_av31_en.cpp #include <iostream> #include <cmath> using namespace std; class Triangle { int a, b, c; public: // Constructor Triangle(int x, int y, int z) { a = x; b = y; c = z; // Destructor ~Triangle() { int permeter() { return a + b + c; float area() { float s = (a + b + c) / 2; return sqrt(s * (s - a) * (s - b) * (s - c)); ; int main() { int a, b, c; cin >> a >> b >> c; Triangle t(a, b, c); cout << "Area: " << t.area() << endl; cout << "Permeter: " << t.permeter() << endl; return 0; 2 1.3. Objects lifecycle

2.2. Employee Write a class that will represent an employee with following attributes: name salary working position (employee, manager or owner). Write a main function where from SI you will read data for N employees, and then print a list of employees sorted in descending order by the salary. Solution oop_av32_en.cpp #include <iostream> #include <string.h> using namespace std; enum position { employee, manager, owner ; class Person { char name[100]; int salary; position pos; public: // Constructors Person() { Person(char *name, int salary, position pos) { strcpy(this->name, name); this->salary = salary; this->pos = pos; // Destruktor ~Person() { ; void setname(char const *name) { strcpy(this->name, name); void setsalary(int const salary) { this->salary = salary; void setposition(position p) { pos = p; char *getname() const { return name; int getsalary() const { return salary; position getposition() const { return pos; void sort(person a[], int n) { int i, j; Person p; 2.2. Employee 3

for (i = 0; i < n - 1; i++) for (j = i; j < n; j++) if (a[i].getsalary() < a[j].getsalary()) { p = a[j]; a[j] = a[i]; a[i] = p; int main() { Person employees[100]; float salary; int n, pos; char name[100]; cin >> n; for (int i = 0; i < n; i++) { cin >> name; cin >> salary; cin >> pos; employees[i].setname(name); employees[i].setsalary(salary); employees[i].setposition((position) pos); sort(employees, n); for (int i = 0; i < n; i++) { cout << i + 1 << ". " << employees[i].getname() << "\t" << employees[i].getsalary() << "\t" << employees[i].getposition() << endl; return 0; 2.3. E-mail Write a class that defines an e-mail message. The class should implement method for showing the contents of the message on the screen. Then write a main function where the parameters of the message are read from SI and an object of this class is instantiated. Then call the method for printing the message. The validity of the e-mail address should be checked with existence of the @ character. Solution oop_av33_en.cpp #include <iostream> #include <cstring> using namespace std; class EMail { enum { AddrLen = 100, SubLen = 200, BodyLen = 1000 ; char to[addrlen]; char from[addrlen]; char subject[sublen]; char body[bodylen]; public: EMail(char *to, char *from, char *subject, char *body) { strncpy(this->to, to, AddrLen - 1); strncpy(this->from, from, AddrLen - 1); strncpy(this->subject, subject, SubLen - 1); strncpy(this->body, body, BodyLen - 1); 4 2.3. E-mail

; to[addrlen - 1] = subject[sublen - 1] = 0; from[addrlen - 1] = subject[sublen - 1] = body[bodylen - 1] = 0; ~EMail() { void setto(char const *n) { strncpy(to, n, AddrLen - 1); to[addrlen - 1] = 0; void setfrom(char const *n) { strncpy(from, n, AddrLen - 1); from[addrlen - 1] = 0; void setsubject(char const *n) { strncpy(subject, n, SubLen - 1); subject[sublen - 1] = 0; void setbody(char const *n) { strncpy(body, n, BodyLen - 1); body[bodylen - 1] = 0; const char* getto() { return to; const char* getfrom() { return from; const char* getsubject() { return subject; const char* getbody() { return body; void print() { cout << "To: " << to << endl; cout << "From: " << from << endl; cout << "Subject: " << subject << endl; cout << "Body: " << body << endl; bool checkemail(char* address); int main() { char to[100], from[100], subject[200], body[1000]; cout << "To: " << endl; cin >> to; if (checkemail(to)) { cout << "From: " << endl; cin >> from; cout << "Subject: " << endl; cin >> subject; cout << "Body: " << endl; cin >> body; EMail poraka(to, from, subject, body); cout << "Sent:" << endl; poraka.print(); else { cout << "Invalid email address!" << endl; return 0; bool checkemail(char *address) { int count = 0; while (*address!= 0) if ('@' == *address++) count++; return (1 == count); 2.3. E-mail 5

3. Source code of the examples and problems https://github.com/finki-mk/sp/ Source code ZIP 6 3. Source code of the examples and problems