0% found this document useful (0 votes)
11 views27 pages

Class and Friend

The document provides an overview of key concepts in C++ classes, including constant objects, member functions, composition, friend functions and classes, the use of the 'this' pointer, static members, and dynamic memory management. It includes code examples to illustrate these concepts, such as creating and using classes, accessing static members, and dynamically allocating memory for objects. The document serves as a comprehensive guide for understanding advanced class functionalities in C++.

Uploaded by

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

Class and Friend

The document provides an overview of key concepts in C++ classes, including constant objects, member functions, composition, friend functions and classes, the use of the 'this' pointer, static members, and dynamic memory management. It includes code examples to illustrate these concepts, such as creating and using classes, accessing static members, and dynamically allocating memory for objects. The document serves as a comprehensive guide for understanding advanced class functionalities in C++.

Uploaded by

aa.raxmanovv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Classes

A Deeper Look
OBJECTIVES
1. To specify const(constant) objects and const
member functions.
2. Composition: objects as member of classes
3. To use friend functions and friend classes.
4. To use the this pointer.
5. To use static data members and member
functions.
6. To create and destroy objects dynamically with
operators new and delete, respectively.
Constant Objects and Constant Member
Function
➢ const member functions may be invoked
for const and non-const objects.
➢ non-const member functions can only be invoked for
non-const objects.
➢ If a non-const member function is invoked on
a const object, it is a compiler error.
➢ const objects may only be used for const parameters.
➢ const methods cannot invoke non-const methods.
1. #include<iostream>
2. using namespace std;
3. class classone {
4. private:
5. int a;
6. public:
7. classone(int x) {
8. a = x;
9. }
10. void get(){
11. cout << "enter a:" << endl;
12. cin >> a;
13. }
14. void show()const {
15. cout << "value of a:=" << a << endl;
16. }
17. };
18. int main() {
19. classone c1(5);
20. c1.get();
21. c1.show();
22. const classone c2(30);
23. c2.show();
24. return 0;
25. }

OUTPUT
Composition: Objects as Members of Classes

1. Sometimes referred to as a has-a relationship

2. A class can have objects of other classes as


members
1. //Composition
2. #include<iostream>
3. using namespace std;
4. class Birthday {
5. private:
6. int month;
7. int date;
8. int year;
9. public:
10. Birthday(int m, int d, int y) {
11. month = m;
12. date = d;
13. year = y;
14. }
15. void print() {
16. cout << month << "/" << date << "/" << year << endl;
17. }
18. };
19. class human {
20. string name;
21. Birthday dob;
22. public:
23. human(string m, Birthday b) {
24. name=m;
25. dob=b;}
26. void printvalue() {
27. cout << name << endl;
28. dob.print();
29. }
30. };
31. int main() {
32. Birthday b(01, 01, 1995);
33. human h1("Sara", b);
34. h1.printvalue();
35. return 0;}
friend Functions and friend Classes
➢ friendfunction of a class
➢ Defined outside that class’s scope
➢Not a member function of that class

➢ Yet has the right to access the non-


public and public members of that class
friend Functions and friend Classes
➢ To declare a function as a friend of a class:
➢ Provide the function prototype in the class
definition preceded by keyword friend
➢ To declare a class as a friend of a class:
➢ Place a declaration of the form
➢ friend class ClassTwo;
in the definition of class ClassOne
➢ All member functions of class ClassTwo are
friends of class ClassOne
1. //example of a friend function
2. #include<iostream>
3. using namespace std;
4. class Box {
5. private:
6. int lenght;
7. int height;
8. int breadth;
9. int volume;
10. public:
11. void set(int a, int b, int c) {
12. lenght=a;
13. height=b;
14. breadth=c;
15. volume = a * b * c;
16. }
17. friend void Display(Box box);
11
18. };
19. void Display(Box box) {
20. cout << box.volume << endl;
21. }
22. int main() {
23. Box box;
24. box.set(3, 4, 5);
25. Display(box);
26. return 0;
27. }
1. //example of a friend class
2. #include<iostream>
3. using namespace std;
4. class box {
5. private:
6. int lenght;
7. int height;
8. int breadth;
9. int volume;
10. public:
11. void set(int a, int b, int c) {
12. lenght=a;
13. height=b;
14. breadth=c;
15. volume = a * b * c;
16. }
17. friend class rectangle;
18. };
19. class rectangle{
20. public:
21. void display(box box) {
22. cout << box.volume << endl;
23. }
24. };
25. int main() {
26. box box;
27. box.set(3, 4, 5);
28. rectangle r;
29. r.display(box);
30. return 0;
31. }
Using the this Pointer
1. Member functions know which object’s data members to
manipulate
a. Every object has access to its own address through a
pointer called this
b. An object’s this pointer is not part of the object itself
c. The this pointer is passed (by the compiler) as an implicit
2. Objects use the this pointer implicitly or explicitly
a. Implicitly when accessing members directly
b. Explicitly when using keyword this
c. this pointer can be used only with member function
a. Friend function can’t use this pointer
1. #include<iostream>
2. using namespace std;
3. class classone {
4. int a;
5. int b;
6. public:
7. classone() {
8. a = 5;
9. b = 10;}
10. void setvalue(int a, int b) {
11. cout << a << endl;
12. cout << this->b << endl;}
13. void display() {
14. cout << a << endl;
15. cout << b << endl;
16. }
17. };
18. int main() {
19. classone c1, c2;
20. c1.setvalue(15, 15);
21. c1.display();
22. c2.setvalue(2, 2);
23. c2.display();
24. return 0;
25. }
static Class Members
1. Only one copy of a variable shared by all objects
of a class
a. “Class-wide” information

b. A property of the class shared by all instances,


not a property of a specific object of the class
2. Declaration begins with keyword static
3. Visible only within the class, but its lifetime is the
entire program.
4. It is initialized to zero when the first object is
created
1. #include<iostream>
2. using namespace std;
3. class classone {
4. private:
5. int a;
6. static int count;
7. public:
8. void getdata(int x) {
9. a = x;
10. count++;
11. }
12. void getcount() {
13. cout << count << endl;
14. }
15. };
16. int classone::count; // Define static member outside the class
17. int main() {
18. classone c1, c2, c3;
19. c1.getdata(5);
20. c2.getdata(6);
21. c1.getcount();
22. return 0;
23. }
static Member function
1. A member function independent of any
particular object of the class . It can be called
even if no objects of the class exist
2. Accessed using only the class name and the
scope resolution operator ::
3. Can only access static data member, other static
member functions.
4. A static member function does not have a this
pointer
5. When a static member function is called, there
might not be any objects of its class in memory
1. #include<iostream>
2. using namespace std;
3. class classone {
4. private:
5. int a;
6. static int count;
7. public:
8. void getdata(int x) {
9. a = x;
10. cout << "a---:" << a << endl;
11. count++;
12. }
13. static void getcount() {
14. cout << "count--:";
15. cout << count << endl;
16. }
17. };
18. int classone::count; // Define static member outside the class
19. int main() {
20. classone c1, c2, c3;
21. c1.getdata(5);
22. c2.getdata(7);
23. classone::getcount(); // Calling static function without an
object
24. return 0;
25. }
Dynamic Memory Management with Operators
new and delete

1. Allocates (i.e., reserves) storage of the proper


size for an object at execution time.
2. Calls a constructor to initialize the object.
3. Returns a pointer of the type specified to the
right of new .
4. Can be used to dynamically allocate any
fundamental type (such as int or double) or any
class type.
➢ Operator delete

1. Destroys a dynamically allocated object


2. Calls the destructor for the object
3. De-allocates (i.e., releases) memory from
the free store
4. The memory can then be reused by the
system to allocate other objects
Initializing an object allocated by new

1. Initializer for a newly created fundamental-type variable


Example
double *ptr = new double( 3.14159 );

2. Specify a comma-separated list of arguments to the


constructor of an object
Example
Time *timePtr = new Time( 12, 45, 0 );
1. #include <iostream>
2. using namespace std;
3. class Student {
4. public:
5. string name;
6. Student(string n) { name = n; }
7. void display() { cout << "Student Name: " << name << endl; }
8. };
9. int main() {
10. Student* students = new Student[3]{ Student("Alice"), Student("Bob"),
Student("Charlie") };
11. for (int i = 0; i < 3; i++) {
12. students[i].display();
13. }
14. delete[] students; // Free the array of objects
15. return 0;}

You might also like