KEMBAR78
C++ - Unit 2 | PDF
0% found this document useful (0 votes)
6 views21 pages

C++ - Unit 2

The document provides an overview of classes and objects in C++, explaining the concept of user-defined data types, member data, and member functions. It details access specifiers (private, protected, public), object creation, member function definitions, and the use of static members and friend functions. Additionally, it includes examples to illustrate these concepts and their implementation in C++ programming.

Uploaded by

jaynthir01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
6 views21 pages

C++ - Unit 2

The document provides an overview of classes and objects in C++, explaining the concept of user-defined data types, member data, and member functions. It details access specifiers (private, protected, public), object creation, member function definitions, and the use of static members and friend functions. Additionally, it includes examples to illustrate these concepts and their implementation in C++ programming.

Uploaded by

jaynthir01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 21
Classes and Objects ai One Class is 2 user defined data type. It contains member data and member functions. As ® unit, the collection of member data and member functions is an object. Therefore, this unit of objects makes up aclass, 6.1.1 Specifying « class A class is @ user defined data type. The mechanism that allows you to comibine data and the function in a single unit is called a class. A class vanable is called object or instance. Classes are generally declared using the keyword class. ‘The general format of the class is as follows: class class_name { private: members1; - protected: members2; public: members3; Me Where class_name is a valid identifier for the class. The body of the Geclaration can “contain members, that. can be either data or function declarations, The members of a class are classified into three categories: Private, public, and. protected. Private, protected, and public are reserved words and are called member access specifiers. 8.2 Access Specifiers / Visibility Modifiers/ Access Control Access specifiers are used to identify access rights for the data and member functions of the class. There are three main types of access specifiers in C+ programming language. The data and functions in a class can be defined by any one of the following visibility modifiers. (Private members bf a class are accessible only from within other members Of the same class. You cannot access if Outside of the class a in C- i Teen Pate, Protected members ae scsi om member oft ee cy also from members of their derived classes. were the obj blie members are accessible from anywhere i Soar all members of a class declared with the oe ra bay Private access for all its members. Therefore, any member that is ec ‘before ne other class specifier automatically has private access, class item { int number; float cost; Public: int z; void getdata(int a, float b); void putdata (void) ; Me vote ove example, the member dat (yrabe) number and cos i cepa ember becaie default sone of member is private Wher delet ar panned et are public members. The function is 8 prototype. Only ictions can the pri dete access the private member 8.3 Creating objects class is Class_name object Narie; Example 5 of item al; This declares al tobe an object of class item, 84 Accessing Class members --Once-an object ofa class has been c access its cone This is achieved Wy using the gat be 4 Provision to (dot, The syntax for acesing members (Da and Fen Ate pean “a) Accessing data member / NOY TES ie objectname . datamember; - b) Accessing member functions objectname . menberfunction(Actuay Example “Stay #include ‘Classes and Objects 8.3 class dat { private: int sdata; public: void setdat( int a) { sdata =a; } void show( ) { cout << "\nSet data is " << sdata; } he void main() { dat %,yi x.setdat (1000); a y-setdat (1245); / x.show(); fo y-show(); } The output of the above program is shown here. Set Data is:1000 Set Data is:1245 In the above class example the "private" object "sdata" is used only within the function. But the functions "setdat", "show" are used in the main function since they are "public". k 8.5 Defining Member Functions \ The data members of a class must be declared within the body of the ~ #include class sum { private: int A, B, Total; Public: void getdata () { cout<,”\n enter the value of A and Bas cin>>A>>B; void display () { total = A+B; cout<<”\n the sum of A and B=”< #include class sum { int A, B, Total; public: void getdata (); void display (); e void sum:: getdata () { cout<<” \n enter the value of A and B”; cin>>A>>B; } void sum:: display () { total =A+B; cout<<”\n the sum of A and B="< class squarenumber { int number; public: long square(); void getnumber (); void display(): és i in C+ and Data ——o void squarenumber: : getnumber () { cout<<”Enter an integer number: ”; cin>>number; } long squarenumber: ?square() { number=number*number; ) return (number) ; ve } void Squarenumber: idisplay()_ { cout<<”Square of the number: "<< } int main() { Squarenumber sqr; Sqr.getnumber Oe Sdr.display() ? return 0; #include class creditmoney { int m; void input (); public: void credit (); void display(); id creditmoney: : input () cout<<”Enter the value of mi" cin>>m; ) void creditmoney: :credit () i input (); if (m=0 && m<=100) m=mt+5; else m=m+20; } void creditmoney: :display() { cout<<”After credit given the.value. of m : is:”< | class friendcl { private: int a,b; public: friend int sum (. friendcl x) ; “void set( int’w, int q); be void friendcl:: ( a= ws set( int w, int a | b=q; Paeee rey 8.1 REC Ot int sum (friendcl x) { return x.a + x.b; } int main() { friendel r; T.set (6,7); << The output ofthe shove program is shown here. The sum of the values is::13 nia go aU the clas “oun” sa fend fncton “friendcl". So, this can use even the private Variables "ab" of the class “friendcl". The function “sum” j ye ARE between two classes, ie "sum" and "friendel" can be established. ‘ Example 2 pi #include siostream.h> int AddEriend(int x) class frdfun { int private data; friend int AddFriend(int x); public: frdfun() { Private data = 5; Me int AddFriend(int x) { frdfun varl; return varl.private data + x? and Obj 8.13 int main() { ” cout << "Added Result: "<< AddFriend(4)< iy ; inline int sqr(int num) Jee ‘i : ; 1 { i q return num*num; } void main() { e float n; cout <<”Enter a number:”; } cin>>n; cout<<"Its Square="<>dollar; cout<< "Rupees : //Function call » needollartors (dollar) ; return 07 ' we 7.8 hearin tions have to be called by Recursion isthe prover Soe eo ‘leulating the factorial of n of amselves i is uel for many tak TI Sarg “a number (nl) the maliémager Pers FO! example, to ol formuls would be: 4) * (n-2) # (3) eee 2 hee pens more concretely, 5! (Factorial of 5) would be: 7.9 ee J Namespace std; factorial (long a) £ (a> 1) return (a * factorial (a-1)); return (1); main () long number; cout << "Enter the number: "; cin >> number; : E << number << "! = ". <<. factorial (number) ; _ return 0; ction Overloading function is overloaded when same given to different function. Ne io fimelions the same name will differ at least in one of the The data type of parameters / arguments Using different number of parameters/arguments function with same name can be defined in a same program as given Se ee een mone \-*” Function with no argument Function with one argument iple iostream.h> Programming in C+ and Duty 710 d sum(); : void sum(int a,int b); int main() \ sum() ; sum(10,20) ; return 0; void sum() \ int a,b; Cout<>a; cout<>b; Cout<<"\n Result from sum() function i, } S2"<>il>>i2; cout<<"ENTER TWO REAL VALUE: "<>f£1>>£2; cout<<"ENTER THE TWO REAL PARTS OF THE COMPLEX NUMBERS : "<>cl.rp>>c2.rp; cout<<"ENTER THE TWO Imaginary PARTS OF COMPLEX NUMBERS: "<>cl.ip>>c2.ip; cout<<"\NINTEGER SUM="< class overtarg, { int a; long b; 2 kaa, Public; int Double (int number) { "ee est * setae ‘“Snumber<< is “< Function defined below the main program.must have:a prototype or declaration _before” calling” the function. Whereas function declared. fe prototype. Inline function created without prototype reduces the memory and it is used only for small function. Inline function c cannot | have - Switch, 1, ‘goto, static variable and recursion, ~ Default argument be specified in the prototype. When a function with default argument is called without the argument then that argument takes the default value. Constant argument can also have default value. Default value cannot be changed only inside the function; but any value can be passed to constant argument. Functions without argument and with only one argument as default argument will-lead-to-ambiguity- “(uncleary for iit the programmer must avoid such kind of functi

You might also like