0% found this document useful (0 votes)
4 views3 pages

C++ Class Concepts and Examples

Uploaded by

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

C++ Class Concepts and Examples

Uploaded by

naacc3.klnce
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Start and stop are default for all programs

1a) Static data member


Steps:
1. Declare a class as item
2. Declare the static member fn. Count and an integer variable number.
3. In public section create the integer as a number and assign the value for number=a and
increment the count.
4. Declare the fn. Getcount and print the count value.
5. Then initialize the static fn. as int item::count.
6. Initialize the 3 integer variable and display output.
(fn.=function).
1b) static member fn.
[Link] the class as item
[Link] the static member fn. count and an integer code.
[Link] public,initialize the fn. setcode and increment count.
[Link] fn. showcode to print code.
[Link] initialize the static fn. int test :: count.
[Link] 3 variables in main and display output.
1c) Default arguments
1.A fn. printline is declared with default arguments.
[Link] fn. printline is defined so as to print character.
[Link] fn. printline is called inside main
3.1 with less number of arguments compared to the printline prototype to access
the default arguments.
3.2 with equal number of arguments compared to the printline prototype to
override the default arguments

1d) Function overloading


1. Define a function with overloading using same function name but with different number
and type of arguments.
1.1 The fn. Show() is overloaded with integer ,float and character datatype.
[Link] fn. Show() is defined to print the output for different datatype.
[Link] the main the fn.
The show() is called with different type of arguments to invoke the overloaded functions.

2a) Friend function


[Link] class name as xyz.
[Link] an integer variable say x,as [Link] public define a fn. and assign x=i.
[Link] friend fn. as max with xyz and abc as arguments.
[Link] another class with name abc in that declare a private variable as ‘a’. In public declare a
[Link] and assign a=i.
[Link] max fn use if condition as (m.x>=n.a) then print x,else print a.
[Link] main fn. create an object for abc then access setvalue fn. similarly,create another object for
xyz fn. and access the setvalue fn.
Then print which is maximum.
2b) / returning obj.

[Link] the class name as complex.


2. Declare float variable say x and y in private.
3. In public, create a fn. Input() with arguments float real,float img.
Assign x=real and y=img.
4. friend fn. is created as sum() to calculate the sum.
4. In fn. void show display x and y in that performs some tasks like c3.x=c1.x+c2.x and
c3.y=c1.y+c2.y and return (c3).
[Link] main, declare objects A,B and C. This objects is used to access the member fn. and
variable in class .
2c) Call by value and address
[Link] call by value fn with argument as int a, increment a.
[Link] call by address fnwith argument as pointer variable,increment a.
[Link], in call by reference using address operator (int&a),increment a.
[Link] main,
Call the functions with different arguments passing methods and display the results.

3a) Array of objects


[Link] the class name as student.
[Link] private,initialize character and integer datatypes.
[Link] public,fn. get and display is declared.
[Link] the class ,initialize the static fn as void student :: get(),in that get roll
no. and name.
[Link] another ststic member fn. as void student ::display, in that print roll no an
Name.
[Link] main, assign int I and an object s[2] for the class [Link] for loop for accessing
member fn.
3b) Pointer to object
[Link] class name as student.
[Link] public, initialize the variable roll no and name.
[Link] print fn. print roll no and name.
4. In main,declare pointer objects say s1,s2 for student. Assign s1=newstudent
s2=newstdent(mry space allocation)
[Link] (*s1).rno=1 and using strcpyfn access the name. Similarly, for s2 and print both.
3c) Local classes
[Link] main, initialize [Link] as an integer and get input from user.
declare a fn. test and pass rno as argument - test(rno).
[Link] test [Link] is declared as student. In public,initialize rollno and name.
[Link] get fn, get name and and assign rollno and rno received from main.
4. print() [Link] defined to print the values
5. create an object for the class locally within the function and invoke the get and print functions
to view the results.
4a)Nested class public
[Link] the class name as out with public variable y.
2. In public of class outside, another class name inside is declared in public with integer varianle
x and andfn.get1 and display ().
3. In get function get x from user and in display fn. print that integer x.
4. create an object for the inside class int1 within the public section of Outside class.
5. define get() for outside class to receive value for y from user.
6. define function add() to add the public variable fo both the classes.
4. In add [Link] another variable c, so as to perform c=int1.x+y then print c.
5. In main, the outside class object and inside class objects are called to access the functions and
display the results.

4b)Nested class private


1. Declare class name as add1 and in the private of add1 another class add2 is declared.
2. In public of add2, initialize integer (a) and a fn. get1 to receive the value of a.
3. In public, of add1 , initialize variable b create an object for add2 say s1.
4. In public, of add1 initialize a function to receive the value of b
5. define another function display () to create the object for class add2 and calculate the sum of
the two class variables and display results.
5. In main, create object for class add1 and invoke the display() to calculate the sum and display
the results.

You might also like