-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopps using pointers.cpp
More file actions
83 lines (63 loc) · 1.32 KB
/
opps using pointers.cpp
File metadata and controls
83 lines (63 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// code using the pointers with objects in a oops
#include<iostream>
using namespace std;
class ABC
{
public:
int a=50;
};
int main()
{
ABC obj1;
ABC *ptr;
ptr=&obj1;
cout<<obj1.a;
cout<<ptr->a;
return 0;
}
// write cpp program using the concept of this pointer
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
// Public data members
string name;
float marks;
// Constructor using 'this' to distinguish members from parameters
Student(string name, float marks) {
this->name = name;
this->marks = marks;
}
void display() {
// Using this pointer explicitly to show it points to the current instance
cout << "Student: " << this->name << " | Marks: " << this->marks << "%" << endl;
}
};
int main() {
// 1. Create object using constructor
Student s1("Sudeep", 85.0);
s1.display();
// 2. Accessing and modifying public members directly from main
s1.name = "Sudeep Uppar";
s1.marks = 90.5;
cout << "\nAfter direct modification:" << endl;
s1.display();
return 0;
}
// same another program
//using the concept this pointer
#include<iostream>
using namespace std;
class student
{
int a,b;
public:
void input(int a,int b){
this->a=a+b;
this->b=a-b;
}
void output(){
cout<<
}
}