CHAPTER 5 Class - Basic Concepts

Size: px
Start display at page:

Download "CHAPTER 5 Class - Basic Concepts"

Transcription

1 CHAPTER 5 Class - Basic Concepts Page 1

2 Procedural Languages (or) Traditional Languages (or) Structured Languages: The traditional way of programming (procedural programming) is action-oriented and the unit of the program is the function. In this kind of languages, there is no security for the data used in the program. Example: Pascal, BASIC and C languages. Object Oriented Programming (OOP) The Object-Oriented is the natural way of thinking and of writing computer programs. The unit of OOP is the object. Example: Smalltalk, C++, Java, C# C++: C++ is derived from the C language. Strictly speaking, it is the superset of C. Almost every correct statement in C is also a correct statement in C++, although the reverse is not true. C++ means, C=C+1. The one and only difference here in C++ is Class concept. So, C++ was originally called C with classes. OO Design Analyze the problem and determine what objects are needed. Determine what attributes the objects will need to have. Determine what behaviors (actions) the objects will need to exhibit. Specify how the objects will needs to interact with each other. Examples: Objects: man, animal, book, car, computer etc. Attributes: size, shape, color, weight etc. Behaviors (Actions): read, walk, sleep, move etc. Page 2

3 OOP Features (or) Characteristics 1) Natural way to view the programming process. 2) Encapsulation: Data and functions are combined into a single entity is called Encapsulation. Data (attributes)+functions (behaviors)=abstract Data Type (ADT). Thus the Encapsulation technique closely related to Data Abstraction and Data hiding. 3) Inheritance: The idea of deriving a new one from the existing one. Provides the idea of code reusability Example: cars, trucks, busses and motorcycles are derived from vehicles. 4) Polymorphism: One thing with several distinct forms. Overloading is a kind of polymorphism. Example: function overloading, operator overloading Class Class is a user-defined data type. It is a logical way of grouping data and functions in the same structure. They are declared using the keyword class. This is similar in functionality to that of the C language keyword struct, but with the possibility of including functions as members, instead of only data. Typically there are two steps to implement classes in a program. 1. Define the class. Normally, class is defined before the main() function. 2. Create objects of the class. Mostly, objects are created inside the main() function. Page 3

4 Class Definition General format of a class definition: class class-name access specifier: member1; member2; access specifier: member1; member2; ; class is a keyword followed by the class-name(may be any name given by user). Note: A semicolon is required after the closing brace of the class definition. Access specifiers: Specify the accessibility of the members of the class. These are keywords and should be typed in small letters. There are three kind of access specifiers used in class 1. private: Members (data or function) are accessible only to the function members of the same class or to their friend classes. The private member is inaccessible from outside the class Mostly data members are declared inside this access specifier to provide data hiding. Page 4

5 It is the default access specifier. Hence in the absence of any access specifiers, the members are considered to be private. 2. public: Members (data or functions) are accessible from anywhere the class is visible. (i.e., the program has access the object). Mostly function members are defined inside this access specifier. 3. protected: Same as private and will be discussed later. Example 1(Function members without scope resolution operator) class time private: int hour; int minutes; int seconds; public: void gettime() cout<<"enter the hour"<<endl; cin>>hour; cout<<"enter the minutes"<<endl; cin>>minutes; cout<<"enter the seconds"<<endl; cin>>seconds; ; void printtime() cout<<"the Time is"<<hour<<":"<<minutes<<":"<<seconds<<endl; Page 5

6 Function members can be defined outside the class by using the scope resolution operator (::). Thus the scope resolution operator(::) provides exactly the same scope properties as if it was directly defined within the class. Example 2(Function members with scope resolution operator) class time private: int hour; int minutes; int seconds; public: void gettime(); void printtime(); ; void time::gettime() cout<<"enter the hour"<<endl; cin>>hour; cout<<"enter the minutes"<<endl; cin>>minutes; cout<<"enter the seconds"<<endl; cin>>seconds; void time::printtime() cout<<"the Time is"<<hour<<":"<<minutes<<":"<<seconds<<endl; Note: Both methods having the same effect in the program. Most of the programmers are following the class definition as in the Example 2. Page 6

7 Object of the class: Like the structure variables, the class should have the variables to access its public members. Class variables are called as objects. An Object is said to be an instance (copy or blueprint) of a class. Example: For the above class time, the objects can be created like time t1,t2,*t; time t[10]; Accessing Public Members of a class: Public members of a class are accessed through the member access operators such as 1. The dot operator(.) 2. The arrow operator (->) Using dot (.) operator: Public members of a class are accessed through dot(.) operator, when the class variables(objects) are normal variables. Example Program: #include<iostream> using namespace std; class time private: int hour; int minutes; int seconds; public: void gettime(); void printtime(); ; void time::gettime() cout<<"enter the hour"<<endl; cin>>hour; cout<<"enter the minutes"<<endl; Page 7

8 cin>>minutes; cout<<"enter the seconds"<<endl; cin>>seconds; void time::printtime() cout<<"the Time is"<<hour<<":"<<minutes<<":"<<seconds<<endl; int main() time t1,t2; t1.gettime(); t1.printtime(); t2.gettime(); t2.printtime(); return(0); Output: Enter the hour 10 Enter the minutes 12 Enter the seconds 34 The Time is10:12:34 Enter the hour 6 Enter the minutes 55 Enter the seconds 9 The Time is6:55:9 Page 8

9 Note: Public members are accessible through the objects. Private members are accessible only inside the public members. Accessing private members of a class through objects are violating the concept of data hiding. It is not allowed. t1.hour, t1.minutes and t1.seconds are illegal access. Using arrow (->) operator: Public members of a class are accessed through arrow(->) operator, when the class variables(objects) are pointer variables. Example Program: #include<iostream> using namespace std; class time private: int hour; int minutes; int seconds; public: void gettime(); void printtime(); ; void time::gettime() cout<<"enter the hour"<<endl; cin>>hour; cout<<"enter the minutes"<<endl; cin>>minutes; cout<<"enter the seconds"<<endl; cin>>seconds; Page 9

10 void time::printtime() cout<<"the Time is"<<hour<<":"<<minutes<<":"<<seconds<<endl; int main() time t1,*t2; t2=&t1; t1.gettime(); t2->printtime(); return(0); Output: Enter the hour 8 Enter the minutes 51 Enter the seconds 27 The Time is8:51:27 Initializing Class Objects: (Constructors) Data members cannot be initialized where they are declared in the class body. These should be initialized by the special member function, which is called constructor. Constructor Is a special member function with the same name as the class Initializes the data members of a class object is called automatically when an object is created Has no return type. Page 11

11 Example: For the above class time create two objects t1, t2 then initialize the object t1 with hour=5, minutes=10 and seconds=15. #include<iostream> using namespace std; class time private: int hour; int minutes; int seconds; public: time(); time(int,int,int); void gettime(); void printtime(); ; time::time(int h,int m,int s) hour=h; minutes=m; seconds=s; time::time() void time::gettime() cout<<"enter the hour"<<endl; cin>>hour; Page 11

12 cout<<"enter the minutes"<<endl; cin>>minutes; cout<<"enter the seconds"<<endl; cin>>seconds; void time::printtime() cout<<"the Time is"<<hour<<":"<<minutes<<":"<<seconds<<endl; int main() time t1(5,10,15),t2; t1.printtime(); t2.gettime(); t2.printtime(); return(0); Output: The Time is5:10:15 Enter the hour 9 Enter the minutes 12 Enter the seconds 39 The Time is9:12:39 Using Default Arguments with Constructors We can use the default arguments with constructors, when the objects get initialized with default values. Example: Page 12

13 For the above class time, create five objects t1, t2, t3, t4 and t5. The constructor function have hour=10, minutes=10 and seconds=10 as the default values. Initialize the object t1 with these default values. Initialize the object t2 with hour=9. Initialize the object t3 with hour=6 and minutes=14. Initialize the object t4 with hour=12, minutes=25 and seconds=44. Get the value for t5 with gettime() function and print all the time with printtime() function. #include<iostream> using namespace std; class time private: int hour; int minutes; int seconds; public: time(int=10,int=10,int=10); void gettime(); void printtime(); ; time::time(int h,int m,int s) hour=h; minutes=m; seconds=s; void time::gettime() cout<<"enter the hour"<<endl; cin>>hour; cout<<"enter the minutes"<<endl; Page 13

14 cin>>minutes; cout<<"enter the seconds"<<endl; cin>>seconds; void time::printtime() cout<<"the Time is"<<hour<<":"<<minutes<<":"<<seconds<<endl; int main() time t1,t2(9),t3(6,14),t4(12,25,44),t5; t5.gettime(); t1.printtime(); t2.printtime(); t3.printtime(); t4.printtime(); t5.printtime(); return(0); Output: Enter the hour 12 Enter the minutes 12 Enter the seconds 12 The Time is10:10:10 The Time is9:10:10 The Time is6:14:10 The Time is12:25:44 The Time is12:12:12 Page 14

15 Passing Objects to Functions Objects may be passed as arguments to the functions and also returned from functions. Example: Read two distance d1, d2 in the form of feet and inches by using a class distance. Add these two distances and return the result to another distance d3. Hint: 1 feet=12 inches #include<iostream> using namespace std; class dist private: int feet; float inches; public: dist(); void getdist(); void showdist(); dist adddist(dist); ; dist::dist() feet=0; inches=0.0; void dist::getdist() cout<<"enter the feet"<<endl; cin>>feet; cout<<"enter the inches"<<endl; cin>>inches; Page 15

16 dist dist::adddist(dist d2) dist temp; temp.inches=inches+d2.inches; if(temp.inches>=12.0) temp.inches=temp.inches-12; temp.feet=1; temp.feet=temp.feet+feet+d2.feet; return(temp); void dist::showdist() cout<<feet<<"\'-"<<inches<<"\""<<endl; int main() dist d1,d2,d3; d1.getdist(); d2.getdist(); d3=d1.adddist(d2); cout<<"distance1 is"; d1.showdist(); cout<<"distance2 is"; d2.showdist(); cout<<"distance3 is"; d3.showdist(); return(0); Page 16

17 Output: Enter the feet 34 Enter the inches 11 Enter the feet 56 Enter the inches 10 Distance1 is34'-11" Distance2 is56'-10" Distance3 is91'-9" Exercises: 1. Write a C++ program to get 2 dates and print the dates by using a class date. 2. Write a C++ program to calculate the area and circumference of a circle by using a class circle. 3. Write a C++ program to create two dates by using a class date. Initialize one date with day=4, month=7 and year=1977. Read another date through getdate() function and print both dates by printdate() function. 4. Write a C++ program to create five dates d1, d2, d3, d4 and d5 for the class date. The constructor function have day=10, month=5 and year=1978 as the default values. Initialize the object d1 with these default values. Initialize the object d2 with day=12. Initialize the object d3 with day=25 and month=11. Initialize the object d4 with day=7, month=10 and year=2005. Get the value for d5 with getdate() function and print all the dates with printdate() function. Page 17

18 5. Create a class Rectangle with length and breadth as private data members and also provide the following member functions: getdata(): gets the length and breadth of rectangle. calarea(): return the area of the rectangle. calperi(): return the perimeter of the rectangle. issquare(): return true if the rectangle is square. 6. Write a C++ program to read two lengths l1, l2 in the form of meters and centimeters by using a class length. Add these two lengths and return the result to another length l3. Hint: 1 meter=100 centimeters 7. Write a C++ program to read real and imaginary part of two complex numbers c1 and c2 by using class complex. Add these two complex numbers and return the result to another complex number c3. Page 18

19 CHAPTER 6 Class-Advanced Concepts - Inheritance Page 1

20 Introduction: The idea of deriving a new class from the existing class. Provides the idea of code reusability. Existing class is called as base class (or) super class. Derived class is called as sub class. Derived class inherits the data members and member functions from the previously defined base class. The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on. Example: Base class Student Shape Loan Employee Account Derived classes GraduateStudent UndergraduateStudent Circle Triangle Rectangle CarLoan HomeImprovementLoan MortgageLoan FacultyMember StaffMember CheckingAccount SavingsAccount Types of Inheritance 1. Single Inheritance - One base class and one derived class. Base class Sub class Page 2

21 2. Multiple Inheritance - A class is derived from more than one base classes Base class1 Base class 2 Sub class 3. Multilevel Inheritance - A sub class inherits from a class which inherits from another class. Base class Sub class 1 Sub class 2 4. Hierarchical Inheritance - More than one sub class inherited from a single base class. Base class Sub class 1 Sub class 2 Sub class 3 Note: A class can be derived from more than one class, which means it can inherit data and functions from multiple base classes. Page 3

22 Access Control and Inheritance: A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. We can summarize the different access types according to who can access them in the following way: Access specifier (versus) Limits Public Protected Private Within the Same Class Derived Yes Yes Yes Yes Yes No Classes Outside of Classes Yes No No A derived class inherits all base class functions with the following exceptions: 1. Constructors, destructors and copy constructors of the base class. 2. Overloaded operators of the base class. 3. The friend functions of the base class. Mode of Deriving a class from Base Class: A class can be derived from base class in three modes of access specifiers. The general format of derived class from base is: class derived-class:access-specifier base-class.. ;

23 Page 4

24 Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default. While using different modes of access specifiers, following rules are applied: 1. Public Mode: When deriving a class in a public mode, public members of the base class become public members of the derived class protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class. 2. Protected Mode: When deriving a class in protected mode, public and protected members of the base class become protected members of the derived class. 3. Private Mode: When deriving a class in a private mode, public and protected members of the base class become private members of the derived class. Note: We hardly use protected or private modes of access speficiers, but public mode access specifier is commonly used. Base Class member (versus) Mode of Private Protected Public Derivation Private Inaccessible Private Private Protected Inaccessible Protected Protected Public Inaccessible Protected Public Page 5

25 It is the time to introduce the concept of protected access specifier. protected: Intermediate level of protection between private and public access specifies. Derived-class members can refer to public and protected members of the base class simply by using the member names Note that protected data breaks encapsulation Note: When you derive the sub class from the base class in public mode, specify the base class data members as protected members. The protected members are accessible in derived class. But the private members are not accessible. This is the only difference between the private and protected access specifiers. Single Inheritance: With Public mode of derived class Example Program: Write a C++ program to implement the single inheritance on the classes Shape and Rectangle. Derive the class Rectangle from the class Shape in public mode. The class Shape consists of length and breadth as data members and getdata() as a member function to get the length and breadth. The derived class Rectangle consists of area and perimeter as data members and calculate() as a member function to calculate the area and perimeter, issquare() as a member function to check the rectangle is square or not, display() as a member function to display the information of Rectangle. Page 6

26 Program: #include<iostream> using namespace std; //Base class class shape protected: float length; float breadth; public: void getdata(); ; Shape Rectangle void shape::getdata() cout<<"enter the length of shape"<<endl; cin>>length; cout<<"enter the breadth of shape"<<endl; cin>>breadth; //Derived class class rect:public shape private: float area; float peri; public: void calculate(); void display(); void issquare(); ; Page 7

27 void rect::calculate() area=length*breadth; peri=2*(length+breadth); void rect::issquare() if(length==breadth) cout<<"rectangle is square"<<endl; else cout<<"rectangle is not square"<<endl; void rect::display() cout<<"length of the Rectangle is:"<<length<<endl; cout<<"breadth of the Rectangle is:"<<breadth<<endl; cout<<"area of the Rectangle is:"<<area<<endl; cout<<"perimeter of the Rectangle is:"<<peri<<endl; int main() rect a; a.getdata(); a.calculate(); a.issquare(); a.display(); return(0); Page 8

28 Output: Enter the length of shape 12.3 Enter the breadth of shape 34.5 Rectangle is not square Length of the Rectangle is:12.3 Breadth of the Rectangle is:34.5 Area of the Rectangle is: Perimeter of the Rectangle is:93.6 Hierarchical Inheritance: With Public mode of derived class Example Program: Write a C++ program to implement the hierarchical inheritance on the classes Shape, Rectangle and Triangle. Derive the classes Rectangle and Triangle from the class Shape in public mode. The class Shape consists of side1 and side2 as data members and getdata() as a member function to get these sides. The derived class Rectangle consists of area as a data member and calarea() as a member function to calculate the area, display() as a member function to display the information of Rectangle. The derived class Triangle consists of area as a data member and calarea() as a member function to calculate the area, display() as a member function to display the information of Triangle. Page 9

29 Program: #include<iostream> using namespace std; Shape //Base class class shape protected: float side1; Rectangle Triangle float side2; public: void getdata(); ; void shape::getdata() cout<<"enter the side1 of your shape"<<endl; cin>>side1; cout<<"enter the side2 of your shape"<<endl; cin>>side2; //Derived class 1 class rect:public shape private: float area; public: void calarea(); void display(); ; Page 11

30 void rect::calarea() area=side1*side2; void rect::display() cout<<"length of the Rectangle is:"<<side1<<endl; cout<<"breadth of the Rectangle is:"<<side2<<endl; cout<<"area of the Rectangle is:"<<area<<endl; //Derived class 2 class tri:public shape private: float area; public: void calarea(); void display(); ; void tri::calarea() area=(side1*side2)/2; void tri::display() cout<<"base of the Triangle is:"<<side1<<endl; cout<<"height of the Rectangle is:"<<side2<<endl; cout<<"area of the Triangle is:"<<area<<endl; Page 11

31 int main() rect a; tri b; a.getdata(); a.calarea(); a.display(); b.getdata(); b.calarea(); b.display(); return(0); Output: Enter the side1 of your shape 1223 Enter the side2 of your shape 3323 Length of the Rectangle is:12.3 Breadth of the Rectangle is:33.7 Area of the Rectangle is: Enter the side1 of your shape 3.23 Enter the side2 of your shape 1321 Base of the Triangle is:34.5 Height of the Rectangle is:17.8 Area of the Triangle is: Page 12

32 Multiple Inheritance: With Public mode of derived class Example Program: Write a C++ program to implement the multiple inheritance on the classes Person, Employee and Teacher. Derive the class Teacher from two base classes Person and Employee in public mode. The Base class Person consists of name, age and sex as data members and getperson() as a member function to get these information. Another Base class Employee consists of Employee ID(EID), Basic Pay(BP), Human Resource Allowance(HRA), Dearness Allowance(DA) and Provident Fund(PF) as data members and getemp() as a member function to get the above information, calsalary() as a member function to calculate the Net Pay(NP=BP+HRA+DA-PF). The derived class Teacher consists of Designation, Years of Teaching Experience(YTE), Years of Industrial Experience(YIE), Total Experience(TE) as data members and getteach() as a member function to get these information, caltotexp() as a member function to calculate the Total Experience(TE=YTE+YIE), display() as a member function to display all the information of Teacher. Page 13

33 Program: #include<iostream> using namespace std; //Base class 1 class person Person Employee protected: char name[30]; int age; char sex[5]; public: Teacher void getperson(); ; void person::getperson() cout<<"enter the Name of the Person"<<endl; cin>>name; cout<<"enter the age of the Person"<<endl; cin>>age; cout<<"enter the sex of the Person"<<endl; cin>>sex; //Base Class 2 class employee protected: int eid; float bp,hra,da,pf,np; public: void getemp(); void calsal(); void display(); ; Page 14

34 void employee::getemp() cout<<"enter the Employee ID"<<endl; cin>>eid; cout<<"enter Basic Pay of Employee"<<endl; cin>>bp; cout<<"enter Human Resource Allowance of Employee"<<endl; cin>>hra; cout<<"enter Dearness Allowance of Employee"<<endl; cin>>da; cout<<"enter Provident Fund of Employee"<<endl; cin>>pf; void employee::calsal() np=bp+hra+da-pf; //Derived class class teacher:public person, public employee private: char des[20]; int yte; int yie; int te; public: void getteach(); void caltotexp(); void display(); ; Page 15

35 void teacher::getteach() cout<<"enter the Designation of the Teacher"<<endl; cin>>des; cout<<"enter the Years of Teaching Experience"<<endl; cin>>yte; cout<<"enter the Years of Industrial Experience"<<endl; cin>>yie; void teacher::caltotexp() te=yte+yie; void teacher::display() cout<<"name of the Teacher is:"<<name<<endl; cout<<"age of the Teacher is:"<<age<<endl; cout<<"sex of the Teacher is:"<<sex<<endl; cout<<"employee ID of the Teacher is:"<<eid<<endl; cout<<"basic Pay of the Teacher is:"<<bp<<endl; cout<<"human Resource Allowance of the Teacher is:"<<hra<<endl; cout<<"dearness Allowance of the Teacher is:"<<da<<endl; cout<<"provident Fund of the Teacher is:"<<pf<<endl; cout<<"net Pay of the Teacher is:"<<np<<endl; cout<<"designation of the Teacher is:"<<des<<endl; cout<<"years of Teaching Experience of the Teacher is:"<<yte<<endl; cout<<"years of Industrial Experience of the Teacher is:"<<yie<<endl; cout<<"years of Total Experience of the Teacher is:"<<te<<endl; Page 16

36 int main() teacher t; t.getperson(); t.getemp(); t.calsal(); t.getteach(); t.caltotexp(); t.display(); return(0); Output: Enter the Name of the Person Ahmed Enter the age of the Person 35 Enter the sex of the Person male Enter the Employee ID Enter Basic Pay of Employee Enter Human Resource Allowance of Employee Enter Dearness Allowance of Employee Enter Provident Fund of Employee Enter the Designation of the Teacher Lecturer Enter the Years of Teaching Experience 12 Enter the Years of Industrial Experience 3 Page 17

37 Name of the Teacher is:ahmed Age of the Teacher is:35 Sex of the Teacher is:male Employee ID of the Teacher is: Basic Pay of the Teacher is: Human Resource Allowance of the Teacher is: Dearness Allowance of the Teacher is: Provident Fund of the Teacher is: Net Pay of the Teacher is: Designation of the Teacher is:lecturer Years of Teaching Experience of the Teacher is:12 Years of Industrial Experience of the Teacher is:3 Years of Total Experience of the Teacher is:15 Exercise: 1. In the example program of multiple inheritance, change the name of getperson() function as getdata(), getemp() function as getdata() and getteach() function as getdata(), and then execute the program. What is the change you find in the output of the program? What is this concept is called in OOPs? Clarify the details. Page 18

38 CHAPTER 7 Class-Advanced Concepts - Polymorphism Page 1

39 Polymorphism: In object-oriented programming, polymorphism is a generic term that means 'many forms'. (From the Greek meaning "having multiple forms"). There are two types of polymorphism: 1. Compile time polymorphism Examples: function overloading, operator overloading 2. Run time polymorphism: Examples: virtual functions. Operator Overloading: C++ allows you to redefine how standard operators work when used with class objects. For example, the operator + is used to add two integer or floating point values. This is the nature of + operator. But by overloading the + operator, we can add two objects in C++. Let us consider int a=10; int b=15; int c=a+b; Thus the operator + is used to add two variables of type int or float. Is this possible to add two objects of class? Yes, it s possible by operator overloading concept. Let us consider the following class class complex float r; float img; ; Page 2

40 complex c1,c2,c3; By using operator overloading concept we can add two complex numbers and store the result to another object. i.e; c3=c1+c2; Here for the operator+ function, the right side object c2 is passed as an argument. Note: In C++ some of the operators are cannot be overloaded. They are?:..* :: sizeof The general format of operator overloading function prototype is returntype operatorsymbol(parameter for object on the right side of operator); Note: here operator is a keyword Example: void operator<<( parameter for object on the right side of operator); complex operator+( complex); distance operator-(complex); circle operator/(circle); Example Program: Write a C++ program to read real and imaginary part of two complex numbers c1 and c2 by using class complex. Add these two complex numbers by overloading the + operator and then return the result to another complex number c3. Page 3

41 #include<iostream> using namespace std; class complex private: float r; float img; public: void getdata(); void print(); complex operator+(complex); ; void complex::getdata() cout<<"enter the real part"<<endl; cin>>r; cout<<"enter the imaginary part"<<endl; cin>>img; void complex::print() cout<<r<<"+"<<img<<"j"<<endl; complex complex::operator+(complex z) complex temp; temp.r=r+z.r; temp.img=img+z.img; return(temp); Page 4

42 int main() complex c1,c2,c3; c1.getdata(); c2.getdata(); cout<<"the first complex number is"<<endl; c1.print(); cout<<"the second complex number is"<<endl; c2.print(); c3=c1+c2; cout<<"the result of addition is"<<endl; c3.print(); return(0); Output: Enter the real part 11.2 Enter the imaginary part 4.5 Enter the real part 22.1 Enter the imaginary part 5.6 The first complex number is j The second complex number is j The result of addition is j Page 5

43 Friends of Classes: A friend is a function or class that is not a member of class, but has access to the private and protected members of the class. friend function and friend classes Can access private and protected members of another class friend functions are not member functions of a class Defined outside of class scope Properties of friendship Friendship is granted, not taken Not symmetric (if B a friend of A, A not necessarily a friend of B) Not transitive (if A a friend of B, B a friend of C, A not necessarily a friend of C) friend declarations 1. To declare a friend function Type friend before the function prototype (declaration) in the class that is giving friendship friend returntype functionname(parameterstype); should appear in the class giving friendship 2. To declare a friend class Type friend class classname in the class that is giving friendship if class A is granting friendship to class B, then friend class B; should appear in class A's definition Example Program (Friend Function) Create a class rectangle with length and breadth as private data members and also provide the following member functions: getdata(): gets the length and breadth of rectangle. calarea(): return the area of the rectangle. and write a function issquare() as a friend of class rectangle which gets a rectangle as an argument and return true if the rectangle is square. Page 6

44 Program: #include<iostream> using namespace std; class rect private: float l; float b; float area; public: void getdata(); float calarea(); friend bool issquare(rect); ; void rect::getdata() cout<<"enter the length"<<endl; cin>>l; cout<<"enter the breadth"<<endl; cin>>b; float rect::calarea() area=l*b; return(area); Page 7

45 bool issquare(rect x) if(x.l==x.b) return(true); else return(false); int main() rect r; float res1; bool res2; r.getdata(); res1=r.calarea(); cout<<"the area is"<<res1<<endl; res2=issquare(r); if(res2==1) cout<<"the rectangle is square"<<endl; else cout<<"the rectangle is not square"<<endl; return(0); Page 8

46 Output: Enter the length 12.3 Enter the breadth 5.6 The area is68.88 The rectangle is not square Exercises: 1. Write a C++ program to read real and imaginary part of two complex numbers c1 and c2 by using class complex. Subtract c2 from c1 by overloading the - operator and then return the result to another complex number c3. 2. Write a C++ program to read two distance d1, d2 in the form of feet and inches by using a class distance. Add these two distances by overloading the + operator and return the result to another distance d3. Hint: 1 feet=12 inches Page 9

CHAPTER 6 Class-Advanced Concepts - Inheritance

CHAPTER 6 Class-Advanced Concepts - Inheritance CHAPTER 6 Class-Advanced Concepts - Inheritance Page 1 Introduction: The idea of deriving a new class from the existing class. Provides the idea of code reusability. Existing class is called as base class

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

Programming in C# Inheritance and Polymorphism

Programming in C# Inheritance and Polymorphism Programming in C# Inheritance and Polymorphism C# Classes Classes are used to accomplish: Modularity: Scope for global (static) methods Blueprints for generating objects or instances: Per instance data

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 How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. All Rights Reserved. 1 Inheritance is a form of software reuse in which you create a class that absorbs an existing class s data and behaviors

More information

C++ Classes, Constructor & Object Oriented Programming

C++ Classes, Constructor & Object Oriented Programming C++ Classes, Constructor & Object Oriented Programming Object Oriented Programming Programmer thinks about and defines the attributes and behavior of objects. Often the objects are modeled after real-world

More information

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy CISC181 Introduction to Computer Science Dr. McCoy Lecture 27 December 8, 2009 Object Oriented Programming Classes categorize entities that occur in applications. Class teacher captures commonalities of

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

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Object-Oriented Programming (OOP) Fundamental Principles of OOP Object-Oriented Programming (OOP) O b j e c t O r i e n t e d P r o g r a m m i n g 1 Object-oriented programming is the successor of procedural programming. The problem with procedural programming is

More information

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE PART A UNIT I 1. Differentiate object oriented programming from procedure oriented programming. 2. Define abstraction and encapsulation. 3. Differentiate

More information

Darshan Institute of Engineering & Technology for Diploma Studies

Darshan Institute of Engineering & Technology for Diploma Studies 1. Explain Call by Value vs. Call by Reference Or Write a program to interchange (swap) value of two variables. Call By Value In call by value pass value, when we call the function. And copy this value

More information

Inheritance (Deitel chapter 9)

Inheritance (Deitel chapter 9) Inheritance (Deitel chapter 9) 1 2 Plan Introduction Superclasses and Subclasses protected Members Constructors and Finalizers in Subclasses Software Engineering with Inheritance 3 Introduction Inheritance

More information

C++ & Object Oriented Programming Concepts The procedural programming is the standard approach used in many traditional computer languages such as BASIC, C, FORTRAN and PASCAL. The procedural programming

More information

Babaria Institute of Technology Computer Science and Engineering Department Practical List of Object Oriented Programming with C

Babaria Institute of Technology Computer Science and Engineering Department Practical List of Object Oriented Programming with C Practical -1 Babaria Institute of Technology LEARN CONCEPTS OF OOP 1. Explain Object Oriented Paradigm with figure. 2. Explain basic Concepts of OOP with example a. Class b. Object c. Data Encapsulation

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

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

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I. y UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I Lab Module 7 CLASSES, INHERITANCE AND POLYMORPHISM Department of Media Interactive

More information

Developed By Strawberry

Developed By Strawberry Experiment No. 3 PART A (PART A: TO BE REFFERED BY STUDENTS) A.1 Aim: To study below concepts of classes and objects 1. Array of Objects 2. Objects as a function argument 3. Static Members P1: Define a

More information

Object Oriented Programming(OOP).

Object Oriented Programming(OOP). Object Oriented Programming(OOP). OOP terminology: Class : A class is a way to bind data and its associated function together. It allows the data to be hidden. class Crectangle Data members length; breadth;

More information

Inheritance Motivation

Inheritance Motivation Inheritance Inheritance Motivation Inheritance in Java is achieved through extending classes Inheritance enables: Code re-use Grouping similar code Flexibility to customize Inheritance Concepts Many real-life

More information

COMPUTER PROGRAMMING (ECE 431) TUTORIAL 9

COMPUTER PROGRAMMING (ECE 431) TUTORIAL 9 COMPUTER PROGRAMMING (ECE 431) TUTORIAL 9 1. What is object oriented programming (OOP)? How is it differs from the traditional programming? 2. What is a class? How a class is different from a structure?

More information

CHAPTER 4 Structures

CHAPTER 4 Structures CHAPTER 4 Structures Page 1 Structures: Arrays are one of the most widely used data structures in programming languages. The limitation of arrays, however, is that all the elements must be of the same

More information

Inheritance

Inheritance Inheritance 23-01-2016 Inheritance Inheritance is the capability of one class to acquire properties and characteristics from another class. For using Inheritance concept in our program we must use at least

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

VALLIAMMAI ENGINEERING COLLEGE

VALLIAMMAI ENGINEERING COLLEGE VALLIAMMAI ENGINEERING COLLEGE SRM Nagar, Kattankulathur 603 203 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK B.E. - Electrical and Electronics Engineering IV SEMESTER CS6456 - OBJECT ORIENTED

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

Module 10 Inheritance, Virtual Functions, and Polymorphism

Module 10 Inheritance, Virtual Functions, and Polymorphism Module 10 Inheritance, Virtual Functions, and Polymorphism Table of Contents CRITICAL SKILL 10.1: Inheritance Fundamentals... 2 CRITICAL SKILL 10.2: Base Class Access Control... 7 CRITICAL SKILL 10.3:

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

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory. Pointer C++ Memory Map Once a program is compiled, C++ creates four logically distinct regions of memory: Code Area : Area to hold the compiled program code Data Area : Area to hold global variables Stack

More information

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018) Lesson Plan Name of the Faculty Discipline Semester :Mrs. Reena Rani : Computer Engineering : IV Subject: OBJECT ORIENTED PROGRAMMING USING C++ Lesson Plan Duration :15 weeks (From January, 2018 to April,2018)

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PROGRAMMING Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PARADIGM Object 2 Object 1 Data Data Function Function Object 3 Data Function 2 WHAT IS A MODEL? A model is an abstraction

More information

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in

More information

C++ Quick Guide. Advertisements

C++ Quick Guide. Advertisements C++ Quick Guide Advertisements Previous Page Next Page C++ is a statically typed, compiled, general purpose, case sensitive, free form programming language that supports procedural, object oriented, and

More information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are

More information

Get Unique study materials from

Get Unique study materials from Downloaded from www.rejinpaul.com VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur 603203. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Year & Semester : IV Section : EEE - 1 & 2 Subject Code

More information

Lab 12 Object Oriented Programming Dr. John Abraham

Lab 12 Object Oriented Programming Dr. John Abraham Lab 12 Object Oriented Programming Dr. John Abraham We humans are very good recognizing and working with objects, such as a pen, a dog, or a human being. We learned to categorize them in such a way that

More information

A A B U n i v e r s i t y

A A B U n i v e r s i t y A A B U n i v e r s i t y Faculty of Computer Sciences O b j e c t O r i e n t e d P r o g r a m m i n g Week 10: I n h e r i t a n c e Asst. Prof. Dr. M entor Hamiti mentor.hamiti@universitetiaab.com

More information

Interview Questions of C++

Interview Questions of C++ Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

More information

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

More C++ : Vectors, Classes, Inheritance, Templates Vectors More C++ : Vectors,, Inheritance, Templates vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes defined differently can be resized without explicit

More information

Exercise1. // classes first example. #include <iostream> using namespace std; class Rectangle. int width, height; public: void set_values (int,int);

Exercise1. // classes first example. #include <iostream> using namespace std; class Rectangle. int width, height; public: void set_values (int,int); Exercise1 // classes first example class Rectangle int width, height; void set_values (int,int); int area() return width*height; ; void Rectangle::set_values (int x, int y) width = x; height = y; int main

More information

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

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com More C++ : Vectors, Classes, Inheritance, Templates with content from cplusplus.com, codeguru.com 2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes

More information

Object Oriented Pragramming (22316)

Object Oriented Pragramming (22316) Chapter 1 Principles of Object Oriented Programming (14 Marks) Q1. Give Characteristics of object oriented programming? Or Give features of object oriented programming? Ans: 1. Emphasis (focus) is on data

More information

Object Oriented Programming 2012

Object Oriented Programming 2012 1. Write a program to display the following output using single cout statement. Maths = 90 Physics =77 Chemestry =69 2. Write a program to read two numbers from the keyboard and display the larger value

More information

Object Oriented Programming

Object Oriented Programming OOP Object Oriented Programming Object 2 Object 1 Object 3 For : COP 3330. Object oriented Programming (Using C++) Object 4 http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Objects: State (fields),

More information

INHERITANCE: EXTENDING CLASSES

INHERITANCE: EXTENDING CLASSES INHERITANCE: EXTENDING CLASSES INTRODUCTION TO CODE REUSE In Object Oriented Programming, code reuse is a central feature. In fact, we can reuse the code written in a class in another class by either of

More information

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:

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: Unit IV Pointers and Polymorphism in C++ Concepts of Pointer: A pointer is a variable that holds a memory address of another variable where a value lives. A pointer is declared using the * operator before

More information

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++ Preview Object Oriented Programming with C++ Class Members Inline Functions vs. Regular Functions Constructors & Destructors Initializing class Objects with Constructors C++ Programming Language C Programming

More information

Object Orientated Analysis and Design. Benjamin Kenwright

Object Orientated Analysis and Design. Benjamin Kenwright Notation Part 2 Object Orientated Analysis and Design Benjamin Kenwright Outline Review What do we mean by Notation and UML? Types of UML View Continue UML Diagram Types Conclusion and Discussion Summary

More information

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli COMS W3101 Programming Language: C++ (Fall 2016) ramana@cs.columbia.edu Lecture-2 Overview of C C++ Functions Structures Pointers Design, difference with C Concepts of Object oriented Programming Concept

More information

7. C++ Class and Object

7. C++ Class and Object 7. C++ Class and Object 7.1 Class: The classes are the most important feature of C++ that leads to Object Oriented programming. Class is a user defined data type, which holds its own data members and member

More information

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class In Java 3 Java is an object-oriented

More information

PES Institute of Technology

PES Institute of Technology Second Semester MCA IA Test, 2017 PES Institute of Technology Bangalore South Campus (1 K.M before Electronic City,Bangalore 560100 ) Test-I(Solution Set) Sub: Object Oriented Programming with C++(16MCA22)

More information

MAN4A OBJECT ORIENTED PROGRAMMING WITH C++ Unit I - V

MAN4A OBJECT ORIENTED PROGRAMMING WITH C++ Unit I - V MAN4A OBJECT ORIENTED PROGRAMMING WITH C++ Unit I - V MAN4B Object Oriented Programming with C++ 1 UNIT 1 Syllabus Principles of object oriented programming(oops), object-oriented paradigm. Advantages

More information

Object Oriented Programming. Solved MCQs - Part 2

Object Oriented Programming. Solved MCQs - Part 2 Object Oriented Programming Solved MCQs - Part 2 Object Oriented Programming Solved MCQs - Part 2 It is possible to declare as a friend A member function A global function A class All of the above What

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

Introduction Of Classes ( OOPS )

Introduction Of Classes ( OOPS ) Introduction Of Classes ( OOPS ) Classes (I) A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class.

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

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

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

And Even More and More C++ Fundamentals of Computer Science

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members

More information

Module Operator Overloading and Type Conversion. Table of Contents

Module Operator Overloading and Type Conversion. Table of Contents 1 Module - 33 Operator Overloading and Type Conversion Table of Contents 1. Introduction 2. Operator Overloading 3. this pointer 4. Overloading Unary Operators 5. Overloading Binary Operators 6. Overloading

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

What are the characteristics of Object Oriented programming language?

What are the characteristics of Object Oriented programming language? What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:

More information

Problem: Given a vector of pointers to Shape objects, print the area of each of the shape.

Problem: Given a vector of pointers to Shape objects, print the area of each of the shape. Problem: Given a vector of pointers to Shape objects, print the area of each of the shape. 1 Problem: Given a vector of pointers to Shape objects, sort the shape objects by areas in decreasing order. Std++sort:

More information

CS OBJECT ORIENTED PROGRAMMING

CS OBJECT ORIENTED PROGRAMMING UNIT-4 INHERITANCE AND RUN TIME POLYMORPHISM Inheritance public, private, and protected derivations multiple inheritance virtual base class abstract class composite objects Runtime polymorphism virtual

More information

Data Structures (INE2011)

Data Structures (INE2011) Data Structures (INE2011) Electronics and Communication Engineering Hanyang University Haewoon Nam ( hnam@hanyang.ac.kr ) Lecture 1 1 Data Structures Data? Songs in a smartphone Photos in a camera Files

More information

6.096 Introduction to C++

6.096 Introduction to C++ MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. MASSACHUSETTS INSTITUTE

More information

OOP THROUGH C++(R16) int *x; float *f; char *c;

OOP THROUGH C++(R16) int *x; float *f; char *c; What is pointer and how to declare it? Write the features of pointers? A pointer is a memory variable that stores the address of another variable. Pointer can have any name that is legal for other variables,

More information

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli COMS W3101 Programming Language: C++ (Fall 2015) ramana@cs.columbia.edu Lecture-2 Overview of C continued C character arrays Functions Structures Pointers C++ string class C++ Design, difference with C

More information

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

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT Two Mark Questions UNIT - I 1. DEFINE ENCAPSULATION. Encapsulation is the process of combining data and functions

More information

Department of Computer science and Engineering Sub. Name: Object oriented programming and data structures Sub. Code: EC6301 Sem/Class: III/II-ECE Staff name: M.Kavipriya Two Mark Questions UNIT-1 1. List

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

PROGRAMMING IN C AND C++:

PROGRAMMING IN C AND C++: PROGRAMMING IN C AND C++: Week 1 1. Introductions 2. Using Dos commands, make a directory: C:\users\YearOfJoining\Sectionx\USERNAME\CS101 3. Getting started with Visual C++. 4. Write a program to print

More information

Chapter 9 - Object-Oriented Programming: Inheritance

Chapter 9 - Object-Oriented Programming: Inheritance Chapter 9 - Object-Oriented Programming: Inheritance 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship between Superclasses and Subclasses 9.5 Case Study: Three-Level

More information

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli COMS W3101 Programming Language: C++ (Fall 2015) ramana@cs.columbia.edu Lecture-2 Overview of C continued C character arrays Functions Structures Pointers C++ string class C++ Design, difference with C

More information

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. OOPs Concepts 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. Type Casting Let us discuss them in detail: 1. Data Hiding: Every

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

CS24 Week 3 Lecture 1

CS24 Week 3 Lecture 1 CS24 Week 3 Lecture 1 Kyle Dewey Overview Some minor C++ points ADT Review Object-oriented Programming C++ Classes Constructors Destructors More minor Points (if time) Key Minor Points const Motivation

More information

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

END TERM EXAMINATION

END TERM EXAMINATION END TERM EXAMINATION THIRD SEMESTER [BCA] DECEMBER 2007 Paper Code: BCA 209 Subject: Object Oriented Programming Time: 3 hours Maximum Marks: 75 Note: Attempt all questions. Internal choice is indicated.

More information

PROGRAMMING IN C++ COURSE CONTENT

PROGRAMMING IN C++ COURSE CONTENT PROGRAMMING IN C++ 1 COURSE CONTENT UNIT I PRINCIPLES OF OBJECT ORIENTED PROGRAMMING 2 1.1 Procedure oriented Programming 1.2 Object oriented programming paradigm 1.3 Basic concepts of Object Oriented

More information

Solved Exercises from exams: Midterm(1)

Solved Exercises from exams: Midterm(1) Solved Exercises from exams: Midterm(1) 1431-1432 1. What is the output of the following program: #include void fun(int *b) *b = *b+10; main() int a=50; fun(&a); cout

More information

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND Introduction to Classes 13.2 The Class Unit 4 Chapter 13 CS 2308 Fall 2016 Jill Seaman 1 l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains

More information

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++ No. of Printed Pages : 3 I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination 05723. June, 2015 BCS-031 : PROGRAMMING IN C ++ Time : 3 hours Maximum Marks : 100 (Weightage 75%)

More information

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

Chapter 15: Inheritance, Polymorphism, and Virtual Functions Chapter 15: Inheritance, Polymorphism, and Virtual Functions 15.1 What Is Inheritance? What Is Inheritance? Provides a way to create a new class from an existing class The new class is a specialized version

More information

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci) Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci) Sung Hee Park Department of Mathematics and Computer Science Virginia State University September 18, 2012 The Object-Oriented Paradigm

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

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform Ch 13: Introduction to Classes 13.1 Procedural Programming! Data is stored in variables CS 2308 Spring 2015 Jill Seaman - Perhaps using arrays and structs.! Program is a collection of functions that perform

More information

Object oriented programming Concepts

Object oriented programming Concepts Object oriented programming Concepts Naresh Proddaturi 09/10/2012 Naresh Proddaturi 1 Problems with Procedural language Data is accessible to all functions It views a program as a series of steps to be

More information

CS304 Object Oriented Programming

CS304 Object Oriented Programming 1 CS304 Object Oriented Programming 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes?

More information

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS JAN 28,2011 MC100401285 Moaaz.pk@gmail.com Mc100401285@gmail.com PSMD01 FINALTERM EXAMINATION 14 Feb, 2011 CS304- Object Oriented

More information

Sri Vidya College of Engineering & Technology

Sri Vidya College of Engineering & Technology UNIT I INTRODUCTION TO OOP AND FUNDAMENTALS OF JAVA 1. Define OOP. Part A Object-Oriented Programming (OOP) is a methodology or paradigm to design a program using classes and objects. It simplifies the

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

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

1. Write two major differences between Object-oriented programming and procedural programming?

1. Write two major differences between Object-oriented programming and procedural programming? 1. Write two major differences between Object-oriented programming and procedural programming? A procedural program is written as a list of instructions, telling the computer, step-by-step, what to do:

More information

Syllabus for Bachelor of Technology. Computer Engineering. Subject Code: 01CE1303. B.Tech. Year - II

Syllabus for Bachelor of Technology. Computer Engineering. Subject Code: 01CE1303. B.Tech. Year - II Subject Code: 01CE1303 Subject Name: Object Oriented Design and Programming B.Tech. Year - II Objective: The objectives of the course are to have students identify and practice the object-oriented programming

More information

Lecture 5: Inheritance

Lecture 5: Inheritance McGill University Computer Science Department COMP 322 : Introduction to C++ Winter 2009 Lecture 5: Inheritance Sami Zhioua March 11 th, 2009 1 Inheritance Inheritance is a form of software reusability

More information

C++ (classes) Hwansoo Han

C++ (classes) Hwansoo Han C++ (classes) Hwansoo Han Inheritance Relation among classes shape, rectangle, triangle, circle, shape rectangle triangle circle 2 Base Class: shape Members of a class Methods : rotate(), move(), Shape(),

More information

Object Oriented Programming

Object Oriented Programming F.Y. B.Sc.(IT) : Sem. II Object Oriented Programming Time : 2½ Hrs.] Prelim Question Paper Solution [Marks : 75 Q.1 Attempt the following (any THREE) [15] Q.1(a) Explain encapsulation? [5] (A) The wrapping

More information