File tree Expand file tree Collapse file tree 12 files changed +304
-0
lines changed Expand file tree Collapse file tree 12 files changed +304
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+
4+ void updateByReference (int &x)
5+ {
6+ cout << &x << " : " << x << endl;
7+ x += 10 ;
8+ }
9+
10+ void updateByAddress (int *x)
11+ {
12+ cout << x << " : " << *x << endl;
13+ *x += 10 ;
14+ }
15+
16+ int main ()
17+ {
18+ int a = 5 ;
19+ updateByReference (a);
20+ cout << " a after updateByReference: " << a << endl;
21+ updateByAddress (&a);
22+ cout << " a after updateByAddress: " << a << endl;
23+ return 0 ;
24+ }
Original file line number Diff line number Diff line change 1+
2+ //package practice;
3+ import java .util .*;
4+
5+ public class p10 {
6+ void sort (int [] args ) {
7+ int i , j , temp ;
8+ for (i = 0 ; i < args .length - 1 ; i ++) {
9+ for (j = 0 ; j < args .length - i - 1 ; j ++) {
10+ if (args [j ] > args [j + 1 ]) {
11+ temp = args [j ];
12+ args [j ] = args [j + 1 ];
13+ args [j + 1 ] = temp ;
14+ }
15+ }
16+ }
17+ }
18+
19+ void display (int [] args ) {
20+ int i ;
21+ for (i = 0 ; i < args .length ; i ++) {
22+ System .out .print (args [i ] + " " );
23+ }
24+ System .out .println ();
25+ }
26+
27+ public static void main (String [] args ) {
28+ p10 ob = new p10 ();
29+ int i ;
30+ int [] arr = new int [5 ];
31+ for (i = 0 ; i < 5 ; i ++) {
32+ arr [i ] = Integer .valueOf (args [i ]);
33+ }
34+
35+ ob .display (arr );
36+ ob .sort (arr );
37+ System .out .println ("Sorted args:" );
38+ ob .display (arr );
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+
4+ class Test
5+ {
6+ public:
7+ int i;
8+
9+ Test (int idx)
10+ {
11+ i = idx;
12+ cout << " Object Created: " << i << endl;
13+ }
14+
15+ ~Test ()
16+ {
17+ cout << " Object Destroyed: " << i << endl;
18+ }
19+ };
20+
21+ int main ()
22+ {
23+ Test t1 (1 );
24+ Test t2 (2 );
25+ return 0 ;
26+ }
Original file line number Diff line number Diff line change 1+ class Department {
2+ Department () {
3+ System .out .println ("CSE" );
4+ }
5+
6+ Department (String name ) {
7+ this ();
8+ System .out .println ("CSE: String Constructor" + name );
9+ }
10+ }
11+
12+ public class p12 extends Department {
13+ p12 () {
14+ System .out .println ("Third year " );
15+ }
16+
17+ p12 (String name ) {
18+ this ();
19+ System .out .println ("Third year: String Constructor" + name );
20+ }
21+
22+ public static void main (String [] args ) {
23+ new p12 ("Section A+B+C" );
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+
4+ void printVal (void *ptr)
5+ {
6+ cout << " Value = " << *(int *)ptr << endl;
7+ }
8+
9+ int main ()
10+ {
11+ int a = 7 ;
12+ printVal ((int *)&a);
13+ return 0 ;
14+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+
4+ class test
5+ {
6+ public:
7+ test () { cout << " Cons" << endl; }
8+ ~test () { cout << " Dest" << endl; }
9+ };
10+
11+ int main ()
12+ {
13+ for (int i = 0 ; i < 2 ; i++)
14+ test obj;
15+ return 0 ;
16+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+
4+ class MyClass
5+ {
6+ private:
7+ static int count; // static variable to track number of objects
8+
9+ public:
10+ // Constructor
11+ MyClass ()
12+ {
13+ count++;
14+ cout << " Object created. Total alive: " << count << endl;
15+ }
16+
17+ // Destructor
18+ ~MyClass ()
19+ {
20+ count--;
21+ cout << " Object destroyed. Total alive: " << count << endl;
22+ }
23+
24+ // Static function to access count
25+ static int getCount ()
26+ {
27+ return count;
28+ }
29+ };
30+
31+ // Initialize static variable outside the class
32+ int MyClass::count = 0 ;
33+
34+ int main ()
35+ {
36+ MyClass obj1;
37+ {
38+ MyClass obj2, obj3;
39+ cout << " Currently alive: " << MyClass::getCount () << endl;
40+ } // obj2 and obj3 go out of scope here
41+
42+ cout << " Currently alive after block: " << MyClass::getCount () << endl;
43+
44+ return 0 ;
45+ }
Original file line number Diff line number Diff line change 1+ public class p5 {
2+ public static void main (String [] args ) {
3+ int i ;
4+ System .out .println ("Command Line Args:" );
5+ for (i = 0 ; i < args .length ; i ++) {
6+ System .out .print (args [i ] + " " );
7+ }
8+
9+ System .out .println ();
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ //package practice;
2+
3+ public class p6 {
4+ double getArea (int r ) {
5+ return (22.0 / 7.0 ) * r * r ;
6+ }
7+
8+ double getArea (int l , int b ) {
9+ return l * b ;
10+ }
11+
12+ public static void main (String [] args ) {
13+ p6 ob = new p6 ();
14+ System .out .println ("Area of Circle = " + ob .getArea (7 ));
15+ System .out .println ("Area of Rectangle = " + ob .getArea (3 , 4 ));
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+
4+ class Animal {
5+ public:
6+ virtual void sound () { // virtual method
7+ cout << " Animal makes a sound" << endl;
8+ }
9+ };
10+
11+ class Animal2 {
12+ public:
13+ void sound () { // non-virtual method
14+ cout << " Animal makes a sound in 2" << endl;
15+ }
16+ };
17+
18+ class Dog : public Animal {
19+ public:
20+ void sound () override {
21+ cout << " Dog barks" << endl;
22+ }
23+ };
24+
25+ class Dog2 : public Animal2 {
26+ public:
27+ void sound () {
28+ cout << " Dog barks 2" << endl;
29+ }
30+ };
31+
32+ int main () {
33+ Animal* a;
34+ Animal2* b;
35+ Dog d;
36+ Dog2 d2;
37+ a = &d;
38+ a->sound (); // Resolved at runtime
39+ b = &d2;
40+ b->sound ();
41+ return 0 ;
42+ }
You can’t perform that action at this time.
0 commit comments