Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Exercise 2.
Copyright  Hannu Laine C++-programming Part 7 Hannu Laine Static members Different uses of const specifier.
C++ Classes & Data Abstraction
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
True or false A variable of type char can hold the value 301. ( F )
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Constructors & Destructors Review CS 308 – Data Structures.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Functions Modules in C++ are called functions and classes
1 C++ Structures Starting to think about objects...
PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1. Structures Structures : Aggregate data types built using elements of other types struct Time { int hour;
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
LAB#1 : Arrays & Functions. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays Arrays.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
C++ Review (3) Structs, Classes, Data Abstraction.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 1Winter Quarter C++: I/O and Classes Lecture 25.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
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.
Class Inheritance Dr. Leon Jololian. Dr.Jololian2 class Person { private: string name; int age; public: Person(string na, int ag); Person(string na);
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
Chapter Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
CS1201: Programming Language 2 Classes and objects.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
Lecture #6 Classes and Objects.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
1 Class 19 Chapter 13 – Creating a class definition.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
Pointer to an Object Can define a pointer to an object:
Chapter 5 Classes.
CS1201: Programming Language 2
group work #hifiTeam
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Pointers & Functions.
Starting to think about objects...
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
CLASSES AND OBJECTS.
Introduction to Classes and Objects
CS1201: Programming Language 2
Anatomy of a class Part II
Pointers & Functions.
Object Oriented Programming (OOP) Lecture No. 11
CS31 Discussion 1D Winter18: week 6
Objects as Function Arguments
OOP objects and Classes Dr. Ahmed Hashim Mohammed A Simple Class
Object Oriented Programming (OOP) Lecture No. 12
Presentation transcript:

Classes

 A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data members) Object

 A class definition begins with the keyword class.  The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { …. }; Class body (data member + methods) Any valid identifier

 private and public  the default is private.  Private :  Accessible only to member functions of class  Private members and methods are for internal use only.  Public:  can be accessed outside the class directly.

class class_name { private: … public: … }; Public members or methods private members or methods

◦ Public function member ◦ Has the same name as the class ◦ Has no return value (not even void) ◦ Called and executed each time a new instance of the class is created ◦ Used to set initial values for data members

◦ Has the same name as the class but with (~) ◦ Has no return value, neither arguments ◦ Called and executed each time an object is destroyed. ◦ Used to cleanup the memory after an object is destroyed

class Student { private: int stnum; public: Student(int s); void setstnum(int sn); int getstnum(); ~Student(); }; Student::Student( int s) { stnum=s; } Student::~Student() { cout << "Object has been destroyed" ; } void Student::setstnum(int sn) { stnum = sn; } int Student::getstnum() { return stnum; }

 A class is a type, and an object of this class is just a variable.variable  You can have many objects of the same class. Int x ;Student S1 ;

 Through an object of the class using (.)  Through a pointer to the class using (->) Student *S1ptr; S1ptr-> setstnum(1234); Student S1; S1. setstnum(1234);

#include using namespace std; class person { private: string name; int age; public: person(); person(string, int ); void set(string,int); string getname(); int getage(); };

person::person() { name="NO Name"; age=0; } person::person(string pn,int pa) { name=pn; age=pa; }

void person::set(string n, int a) { name=n; age=a; } string person::getname() { return name; } int person::getage() { return age; }

int main() { person a; person b("Fahad",24); cout<<"Persons information : "<<endl; cout << a.getname() << ": " << a.getage() <<endl; cout << b.getname() << ": " << b.getage() << endl; cout<<"*****************************************"<<endl; a.set("Ahmad",30); b.set("Khaled", 20); cout<<"Persons information after modification : "<<endl; cout << a.getname() << ": " << a.getage() <<endl; cout << b.getname() << ": " << b.getage() << endl; return 0; }

Define a class Rectangle which contains:  Data members: length and width.  Member functions: ◦ A constructor with two parameters that assigns values to the data members of the created object. ◦ Function area() to compute the area of the rectangle. ◦ Function getdata( ) to prompt the user to enter the length and width for a rectangle. ◦ Function showdata( ) to display length, width and area of a rectangle