C : Friend And Inheritance - Ryerson University

2y ago
22 Views
3 Downloads
390.61 KB
13 Pages
Last View : 22d ago
Last Download : 2m ago
Upload by : Annika Witter
Transcription

C : Friend and InheritanceFriend functionsPrivate and protected members of a class cannot be accessed from outside thesame class in which they are declared. However, this rule does not affectfriends. A function that is not a member of a class but has access to the class'sprivate and protected members. They are normal external functions that are given special accessprivileges. Friend function is declared by the class that is granting access. Thefriend declaration can be placed anywhere in the class declaration. It isnot affected by the access control keywords (public, private andprotected.)1

#include iostream using namespace std;class myclass {int num;public:myclass(int x) {num x;}friend int isneg(myclass ob);};int isneg(myclass ob)//friend function{return (ob.num 0) ? 1 : 0;}int main(){myclass a(-1), b(2);cout isneg(a) ' ' isneg(b);cout endl;return 0;}2

Friend classesWe can define a class as friend of another one, granting that second classaccess to the protected and private members of the first one.#include iostream void CRectangle::convert (CSquare a) {using namespace std;width a.side;class CSquare {height a.side;}private:int side;public:void set side (int a){side a;}friend class CRectangle;int main () {CSquare sqr;CRectangle rect;sqr.set side(4);rect.convert(sqr);};cout rect.area();class CRectangle {return 0;}int width, height;public:int area (){return (width * height);}void convert (CSquare a);}3

Inheritance between classes allows to create classes which are derived from other classes, so that theyautomatically include some of its "parent's" members, plus its own.The class CPolygon would contain members that are common for both types ofpolygon. In our case: width and height.4

class A : public B{// Class A now inherits the members of Class B// with no change in the “access specifier” for}// the inherited membersclass A : protected B{// Class A now inherits the members of Class B// with public members “promoted” to protected}// protected members “promoted” to privateclass A : private B{// Class A now inherits the members of Class B// with public and protected members}// “promoted” to private5

Type of InheritanceVisibilitymodifier vateprotectedpublic The type of inheritance defines the minimum access level for themembers of derived class that are inherited from the base class With public inheritance, the derived class follow the same accesspermission as in the base class With protected inheritance, only the public members inherited from thebase class can be accessed in the derived class as protected members With private inheritance, none of the members of base class is accessibleby the derived class6

motherdaughtersonclass mother{protected:int x, y;public:void set(int a, int b);private:int z;}class son : protected mother{private:double b;public:void foo ( );}void son :: foo ( ){x y 20; // error, not a public memberset(5, 10);cout “value of b ” b endl;z 100; // error, not a public member}son can access only 1 of the 4 inherited member7

What is inherited from the base class?In principle, a derived class inherits every member of a base class except: its constructor and its destructorits friendsAlthough the constructors and destructors of the base class are not inheritedthemselves, its default constructor (i.e., its constructor with no parameters)and its destructor are always called when a new object of a derived class iscreated or destroyed.8

If you want that an overloaded constructor is called when a new derived objectis created, you can specify it in each constructor definition of the derived class:derived constructor name (parameters) : base constructor name (parameters) {.}class mother {class son : public mother {public:public:mother ()son (int a) : mother (a){ cout "mother: no parameters\n"; }mother (int a){ cout "son: int parameter\n\n"; }};{ cout "mother: int parameter\n"; }};int main () {class daughter : public mother {public:daughter (int a)daughter cynthia (0);son daniel(0);return 0;{ cout "daughter: int parameter\n\n"; }}; }9

Multiple inheritance (C only)In C it is perfectly possible that a class inherits members from more than oneclass. Deriving a class from more than one direct base class is called multipleinheritance.This is done by simply separating the different base classes with commas in thederived class declaration.class A { /* . */ };class B { /* . */ };class C { /* . */ };class X : public A, public B, public C { /* . */ };10

class CPolygon {protected:int width, height;public:void set values (int a, int b){ width a; height b;}};class COutput {public:void output (int i);};void COutput::output (int i) {cout i endl;}11

class CRectangle: public CPolygon, public COutput {public:int area () { return (width * height); }};class CTriangle: public CPolygon, public COutput {public:int area () { return (width * height / 2); }};int main () {CRectangle rect;CTriangle trgl;rect.set values (4,5);trgl.set values (4,5);rect.output (rect.area());trgl.output (trgl.area());}12

The main problem appears when data fields or methods with same names are present inboth super classes.class Truc {public:int chose() {}};class Machin {public:int chose() {}};This can not compile:chose.cc: In function „int main()‟:chose.cc:16: error: request for member „chose‟ is ambiguouschose.cc:8: error: candidates are: int Machin::chose()chose.cc:3: error: int Truc::chose()class Bidule : public Truc, Machin {};int main() {Bidule x;x.chose();}Truc:: x.chose(); orMachin:: x.chosen();13

its constructor and its destructor its friends Although the constructors and destructors of the base class are not inherited themselves, its default constructor (i.e., its constructor with no parameters) and its destructor are always called when

Related Documents:

CCS Help Desk 416.979.5000 ext. 6806 help@ryerson.ca CCS Reception LIB-B99 350 Victoria St., Toronto Page 5 Using XMediusFAX 3. On your first use of SendFax you will be prompted for your User Settings.Select My User and enter your my.ryerson username appended to @ryerson.ca i.e. jsmith@ryerson.ca.Your initialFile Size: 856KB

Alexander Ferworn, CD, PhD Professor Department of Computer Science Faculty of Science Ryerson University 350 Victoria St. Toronto, Ontario, Canada M5B 2K3 (416) 979-5000 x 6968 aferworn@ryerson.ca . 1 Ryerson Office of the Vice President-Research and Innovation holds a Memorandum of Understanding (2007) with

Java keyword extends to denote inheritance. class Manager extends Employee { added methods and fields} C NOTE: Inheritance is similar in Java and C . Java uses the extends keyword instead of the : token. All inheritance in Java is public inheritance; there is no analog to the C fea-tures of private and protected inheritance.

Organic Synthesis as Artificial Intelligence Planning Arman Masoumi 1, Mikhail Soutchanski , and Andrea Marrella2 1 Ryerson University, Toronto, Ontario, Canada arman.masoumi@ryerson.ca, mes@scs.ryerson.ca 2 Sapienza Universita di Roma, Rome, Italy marrella@dis.uniroma1.it Abstract.

RYERSON UNIVERSITY JUNE 19-22, 2014 SPECIAL THANKS TO: Gerd Hauck, Dean, Faculty of Communication and Design, Ryerson Donald J. Gillies, Professor Emeritus, School of Image Arts, Ryerson The Canadian Communication Association ACKNOWLEDGEMENTS: Thom Gencarelli, Sheena Hyndman, Paul Soukup, Ainsley Moore, Corey Anton, Valerie Peterson,

Mar 09, 2020 · 9th Biology – Patterns of Inheritance and Human Genetics March 23-27 12 III. Wednesday, March 25 Unit – Ch 12: Inheritance Patterns and Human Genetics Lesson 3: Chromosomes and Inheritance (Part 3) Objectives: Be able to do this by the end of this lesson. 1. Differentiate between chromosome mutations and gene mutations. 2.

Phenotypes and Genotypes . The inheritance pattern of one trait doesn’t usually influence the inheritance of any other trait. Three Ideas Mendel Used for Explaining This Pattern of Inheritance 1. Each parent puts into every sperm or egg it makes a single set of instructions

Complex Inheritance and Human Heredity Inferring Genotypes ! Knowing physical traits can determine what genes an individual is most likely to have. Predicting Disorders ! Record keeping helps scientists use pedigree analysis to study inheritance patterns, determine phenotypes, and ascertain genotypes. Basic Patterns of Human Inheritance