Advanced Programming (C++)

From C to C++

Ahmad Yoosofan

University of Kashan

http://yoosofan.github.io/course/cpp.html

Methods of Learning a Programming Languange

5 items

Converting C code to C++

  1. Rename .c file to .cpp
  2. <stdio.h> to <cstdio>
    • stdio.h → cstdio
    • math.h → cmath
    • stdlib.h → cstdlib
    • string.h → csting
    • ctype.h → cctype

code1.c

code1.cpp

1 #include<stdio.h>
2 int main(){
3   int s1, s2, s3, sum;
4   printf("Enter mark\n");
5   scanf("%d", &s1 );
6   printf("Enter mark\n");
7   scanf("%d", &s2 );
8   printf("Enter mark\n");
9   scanf("%d", &s3 );
10   sum = s1 + s2 + s3;
11   printf("%d\n", sum);
12   return 0;
13 }
1 #include<cstdio>
2 using namespace std;
3 int main(){
4   int s1, s2, s3, sum;
5   printf("Enter mark\n");
6   scanf("%d", &s1);
7   printf("Enter mark\n");
8   scanf("%d", &s2);
9   printf("Enter mark\n");
10   scanf("%d", &s3);
11   sum = s1 + s2 + s3;
12   printf("%d\n", sum);
13   return 0;
14 }

c2cpp01_stdlib.c

c2cpp01_stdlib.cpp

1 #include<stdio.h>
2 #include<math.h>
3 #include<stdlib.h>
4 
5 int main(){
6   int i=1,a=2,b,c=3;
7   b = i>a ? a>c ? c:a : i>c?c:i;
8   printf("%d\t",b);
9   printf("%d\n", i>a ? a>c?c:a : i>c?c:i);
10   b=1;a=2;c=1;
11   printf("%d\t",b+=a+=c);
12   printf("%d,%d,%d\n",b,a,c);
13   printf("%d\n",printf("%d\n",b));
14   printf("%lf\n",sin(0.12));
15   printf("%d\n",abs(-12));
16   printf("%lf\n",fabs(-0.12));
17   printf("%lf\n",pow(2,4));
18   printf("%lf\n",sqrt(2));
19   printf("%lf\n",pow(2,0.5));
20   i=2;a=3;b=4;
21   i= a,b; /*   */
22   printf("%d\t",i);
23   return 0;
24 }
1 #include<cstdio>
2 #include<cmath>
3 #include<cstdlib>
4 using namespace std;
5 int main(){
6   int i=1,a=2,b,c=3;
7   b = i>a ? a>c ? c:a : i>c?c:i;
8   printf("%d\t",b);
9   printf("%d\n", i>a ? a>c?c:a : i>c?c:i);
10   b=1;a=2;c=1;
11   printf("%d\t",b+=a+=c);
12   printf("%d,%d,%d\n",b,a,c);
13   printf("%d\n",printf("%d\n",b));
14   printf("%lf\n",sin(0.12));
15   printf("%d\n",abs(-12));
16   printf("%lf\n",fabs(-0.12));
17   printf("%lf\n",pow(2,4));
18   printf("%lf\n",sqrt(2));
19   printf("%lf\n",pow(2,0.5));
20   i=2;a=3;b=4;
21   i= a,b; /*   */
22   printf("%d\n",i);
23 }

c2cpp02_functions.c

c2cpp02_functions.cpp

1 #include<stdio.h> /* ./a.out >a1.txt */
2 int min(int, int); int input(void);
3 void myf(void);void printTriangle(void);
4 void printSquare(void); void menu(void);
5 int main(){
6   menu();
7   return 0;
8 }
9 void menu(void){ int n;
10   do{
11     printf("Enter your choice. \
12              Enter 4 for end\n");
13     n=input();
14     if(n==1)  myf();
15     else if(n==2)printTriangle();
16     else if(n==3)printSquare();
17     else if(n!=4)printf("wrong number\n");
18   }while(n!=4);
19 }
20 void printSquare(void){
21   int n,i,j; n=input();
22   for(i=0;i<n;i++){
23     for(j=0;j<n;j++)   printf("*");
24     printf("\n");
25   }
26 }
27 void printTriangle(void){
1 #include<cstdio> /* ./a.out >a1.txt */
2 using namespace std;
3 int min(int, int); int input(void);
4 void myf(void);void printTriangle(void);
5 void printSquare(void); void menu(void);
6 int main(){
7   menu()
8   return 0;
9 }
10 void menu(void){int n;
11   do{
12     printf("Enter your choice. \
13              Enter 4 for end\n");
14     n=input(); 
15     if(n==1)  myf();
16     else if(n==2)printTriangle();
17     else if(n==3)printSquare();
18     else if(n!=4)printf("wrong number\n");
19   }while(n!=4);
20 }
21 void printSquare(void){
22   int n,i,j; n=input();
23   for(i=0;i<n;i++){
24     for(j=0;j<n;j++)   printf("*");
25     printf("\n");
26   }
27 }

c2cpp02_functions.c

c2cpp02_functions.cpp

27 void printTriangle(void){
28   int n,i,j; n=input();
29   for(i=0;i<n;i++){
30     for(j=0;j<=i;j++) printf("*");
31     printf("\n");
32   }
33 }
34 int min(int a, int b){return a<b ? a:b;}
35 int input(void){int a;
36   do{
37     printf("Enter a>0: ");
38     scanf("%d",&a);
39   }while(a<=0);
40   return a;
41 }
42 void myf(void){
43   int a=input(), b=input();
44   printf("min %d\n",min(a,b));
45 }
27 }
28 void printTriangle(void){
29   int n,i,j; n=input();
30   for(i=0;i<n;i++){
31     for(j=0;j<=i;j++) printf("*");
32     printf("\n");
33   }
34 }int min(int a, int b){return a<b ? a : b;}
35 int input(void){int a;
36   do{
37     printf("Enter a>0: ");
38     scanf("%d",&a);
39   }while(a<=0);
40   return a;
41 }void myf(void){
42   int a=input(), b=input();
43   printf("min %d\n",min(a,b));
44 }

No return 0 and void

1 #include<stdio.h>
2 //c2cpp02_functions_style.cpp
3 int min(int, int); 
4 int input();
5 void myf();
6 void printTriangle();
7 void printSquare();
8 
9 int main(){
10   int n = 0;
11   do{
12     printf("Enter your choice. \
13              Enter 4 for end\n");
14     n = input();
15     if(n == 1)  
16       myf();
17     else if(n == 2)
18       printTriangle();
19     else if(n == 3)
20       printSquare();
21     else if(n != 4)
22       printf("wrong number\n");
23   }while(n != 4);
24 }
25 void printSquare(void){
26   int n, i = 0, j; 
27   n = input();
28   for(i = 0; i < n; i++){
29     for(j = 0; j < n; j++)   
30       printf("*");
31     printf("\n");
32   }
33 }
34 
35 void printTriangle(){
36   int n, i, j; 
37   n = input();
38   for(i = 0; i < n; i++){
39     for(j = 0; j <= i; j++) 
40       printf("*");
41     printf("\n");
42   }
43 }
44 
45 int min(int a, int b)
46 {return a < b ? a : b;}

Online Compilers and tools

Compilers

Compile and run

cd folder_of_code
g++ code1.cpp
./a.out
cd folder_of_code
g++ code1.cpp -o code1.o
./code1.o

Editors

git

git config --global user.name "نام و نام خانوادگی شما"
git config --global user.email "رایانامه‌ی شما"
git clone https://github.com/yoosofan/slide.git
cd slide
git pull origin main

IDE

Standard Library

C++

Initialization and Assignment

1 #include<stdio.h>
2 /*#include<iostream>
3 using namespace std;*/
4 int main(){
5   int s1 = 2;
6   // int sum = s1+4;
7   printf("Enter mark\n");
8   scanf("%d",& s1 );
9   /*cout << "sss" ;*/
10   int s2;
11   printf("Enter mark\n");
12   scanf("%d",& s2 );
13   int s3;
14   printf("Enter mark\n");
15   scanf("%d",& s3 );
16   // static int sum= s1 + s2 + s3;  // error
17   const int m1=2, m2=3, m3=4;
18   static int sum= m1 + m2 + m3; 
19   // Assignment
20   printf("%d\n",sum);
21 }
1 #include<cstdio>
2 using namespace std;
3 int main(){
4   int s1;
5   printf("Enter mark\n");
6   scanf("%d",& s1 );
7   int s2;
8   printf("Enter mark\n");
9   scanf("%d",& s2 );
10   int s3;
11   printf("Enter mark\n");
12   scanf("%d",& s3 );
13   int sum;
14   sum = s1 + s2 + s3; 
15   // Assignment
16   printf("%d\n",sum);
17 }

const instead of define

1 #include<stdio.h>
2 /*#include<iostream>
3 using namespace std;*/
4 int f1(const int m1);
5 # define m33 50
6 const int global_variable_m44 = 50;
7 int main(){
8   int s1 = 2;
9   int arr[50];
10   int i;
11   for(i=0; i< 50; i++)
12     arr[i] = i*2;
13   int arr2[m33];
14   for(i=0; i< m33; i++)
15     arr2[i] = i*2;
16   int arr3[global_variable_m44];
17   for(i=0; i< global_variable_m44; i++)
18     arr2[i] = i*2;
19   // int sum = s1+4;
20   printf("Enter mark\n");
20   scanf("%d",& s1 );
21   /*cout << "sss" ;*/
22   int s2;
23   printf("Enter mark\n");
24   scanf("%d",& s2 );
25   int s3;
26   printf("Enter mark\n");
27   scanf("%d",& s3 );
28   // static int sum= s1 + s2 + s3;  // error
29   const int m1=2, m2=3, m3=4;
30   static int sum= m1 + m2 + m3; 
31   // Assignment
32   printf("%d\n",sum);
33   printf("%d\n",f1(m2));
34 }
35 int f1(const int m1){
36   static int sum = 5;
37   sum = 5;
38   return sum++ + m1;
39 }

Cin & cout

1 #include<iostream>
2 using namespace std;
3 int main(){
4   int s1;
5   cout << "Enter mark\n";
6   cin >> s1 ;
7   int s2;
8   cout << "Enter mark\n";
9   cin >> s2;
10   cout << "Enter mark\n";
11   int s3;
12   cin >> s3 ;
13   int sum = s1 + s2 + s3;
14   // initialization
15   cout << sum;
16 }
1 #include<iostream>
2 int main(){
3   int s1;
4   std::cout << "Enter mark\n";
5   std::cin >> s1 ;
6   int s2;
7   std::cout << "Enter mark\n";
8   std::cin >> s2;
9   std::cout << "Enter mark\n";
10   int s3;
11   std::cin >> s3 ;
12   int sum = s1 + s2 + s3;
13   // initialization
14   std::cout << sum;
15 }

Struct name C

Struct name C++

2 struct firstStruct{  };
3 struct secondStruct{ };
4 struct thirdStruct{ };
5 struct firstStruct f1(void);
6 void f2(struct firstStruct);
7 int main(){
8   struct firstStruct myfs;
9   myfs = f1();
10   f2(myfs);
11   return 0;
12 }
13 struct firstStruct  f1(void){
14   struct firstStruct fs1;
15   struct secondStruct ss1;
16   struct thirdStruct ts1;
17   return fs1;
18 }
19 void f2(struct firstStruct m1){
20   printf("%p\n", &m1);
21 }
2 struct firstStruct{  };
3 struct secondStruct{ };
4 struct thirdStruct{ };
5 firstStruct f1(void);
6 void f2(firstStruct);
7 int main(){
8   firstStruct myfs;
9   myfs = f1();
10   f2(myfs);
11 }
12 firstStruct f1(){
13   firstStruct fs1;
14   secondStruct ss1;
15   thirdStruct ts1;
16   return fs1;
17 }
18 void f2(firstStruct m1){
19   cout << &m1 ;
20   cout << "\n";
21 }
2 struct date{ int year, month, day; };
3 struct student{
4   char name[50]; 
5   char stdNumber[15]; 
6   struct date birthDate;
7 };
8 struct date input_date(void);
9 struct student input_student(void);
10 void print_date(struct date d1);
11 void print_student(struct student st);
12 int main(){
13   struct student st1[50];
14   int n, i=0;
15   printf("Enter number of \
16           students(0<n<50) ");
17   scanf("%d",&n);
18   if(n>0 && n<50)
19     do{
20       st1[i] = input_student();
21       print_student(st1[i]);
22       i++;
23     }while(i<n);
24   else printf("0<n<50, %d",n);
25 }
26 struct date input_date(void){
27   struct date d1;
3 struct date{ int year, month, day; };
4 struct student{
5   char name[50]; 
6   char stdNumber[15]; 
7   date birthDate;
8 };
9 date input_date(void);
10 student input_student(void);
11 void print_date(date d1);
12 void print_student(student st);
13 int main(){
14   student st1[50];
15   int n, i=0;
16   cout << "Enter number of students(0<n<50) ";
17   cin >> n;
18   if(n>0 && n<50)
19     do{
20       st1[i] = input_student();
21       print_student(st1[i]);
22       i++;
23     }while(i<n);
24   else  { cout << "0<n<50 "; cout << n;}
25 }
26 date input_date(void){
27   date d1;
28   cout << "Enter year:{1350..1390} "; 
28   printf("Enter year:{1350..1390} "); 
29   scanf("%d",&d1.year);
30   printf("Enter month{1..12}:"); 
31   scanf("%d",&d1.month);
32   printf("Enter day{1..31}:"); 
33   scanf("%d",&d1.day);
34   return d1;
35 }
36 void print_date(struct date d1){
37   printf("year=%d,\tmonth=%d,\tday=%d\n",
38     d1.year,d1.month,d1.day);
39 }
40 struct student input_student(void){
41   struct student st;
42   printf("Enter name :");
43   scanf("%s",st.name);
44   printf("Enter studnet number :");
45   scanf("%s",st.stdNumber);
46   printf("Enter birth date :"); 
47   st.birthDate = input_date();
48   return st;
49 }
50 void print_student(struct student st){
51   printf("name=%s\n",st.name);
52   printf("number=%s\n",st.stdNumber);
53   print_date(st.birthDate);
54 }
29   cin >> d1.year;
30   cout << "Enter month{1..12}:"; 
31   cin >> d1.month;
32   cout << "Enter day{1..31}:"; 
33   cin >> d1.day;
34   return d1;
35 }
36 void print_date(date d1){
37   cout << "year = ";  cout << d1.year;
38   cout << "\tmonth = "; cout << d1.month;
39   cout << "\tday = "; 
40   cout << d1.day; cout << "\n";
41 }
42 student input_student(void){
43   student st;
44   cout << "Enter name :";
45   cin >> st.name;
46   cout << "Enter studnet number :";
47   cin >> st.stdNumber;
48   cout << "Enter birth date :"; 
49   st.birthDate = input_date();
50   return st;
51 }
52 void print_student(student st){
53   cout << "name = "; cout << st.name;
54   cout << "\n"; cout << "number = ";
55   cout << st.stdNumber; cout << "\n";
56   print_date(st.birthDate);
57 }

struct c

struct cpp

1   printf("Enter year:{1350..1390} "); 
2   scanf("%d",&d1.year);
3   printf("Enter month{1..12}:"); 
4   scanf("%d",&d1.month);
5   printf("Enter day{1..31}:"); 
6   scanf("%d",&d1.day);
7   return d1;
8 }
9 void print_date(struct date d1){
10   printf("year=%d,\tmonth=%d,\tday=%d\n",
11     d1.year,d1.month,d1.day);
12 }
13 struct student input_student(void){
14   struct student st;
15   printf("Enter name :");
16   scanf("%s",st.name);
17   printf("Enter studnet number :");
18   scanf("%s",st.stdNumber);
19   printf("Enter birth date :"); 
20   st.birthDate = input_date();
21   return st;
22 }
23 void print_student(struct student st){
24   printf("name=%s\n",st.name);
25   printf("number=%s\n",st.stdNumber);
26   print_date(st.birthDate);
27 }
1   cout << "Enter year:{1350..1390} "; 
2   cin >> d1.year;
3   cout << "Enter month{1..12}:"; 
4   cin >> d1.month;
5   cout << "Enter day{1..31}:"; 
6   cin >> d1.day;
7   return d1;
8 }
9 void print_date(date d1){
10   cout << "year = ";  cout << d1.year;
11   cout << "\tmonth = "; cout << d1.month;
12   cout << "\tday = "; 
13   cout << d1.day; cout << "\n";
14 }
15 student input_student(void){
16   student st;
17   cout << "Enter name :";
18   cin >> st.name;
19   cout << "Enter studnet number :";
20   cin >> st.stdNumber;
21   cout << "Enter birth date :"; 
22   st.birthDate = input_date();
23   return st;
24 }
25 void print_student(student st){
26   cout << "name = "; cout << st.name;
27   cout << "\n"; cout << "number = ";
28   cout << st.stdNumber; cout << "\n";
29   print_date(st.birthDate);
30 }

simple cout (print_date)

multiple output (print_date)

1 #include<iostream>
2 using namespace std;
3 struct date{ int year, month, day; };
4 date input_date(void){
5   date d1;
6   cout << "Enter year:{1350..1390} "; 
7   cin >> d1.year;
8   cout << "Enter month{1..12}:"; 
9   cin >> d1.month;
10   cout << "Enter day{1..31}:"; 
11   cin >> d1.day;
12   return d1;
13 }
14 void print_date(date d1){
15   cout << "year = ";  cout << d1.year;
16   cout << "\tmonth = "; cout << d1.month;
17   cout << "\tday = "; cout << d1.day; cout << "\n";
18 }
19 int main(){
20   date birthDate = input_date();
21   print_date(birthDate);
22   return 0;
23 }
1 #include<iostream>
2 using namespace std;
3 struct date{ int year, month, day; };
4 date input_date(void){
5   date d1;
6   cout << "Enter year:{1350..1390} " ;
7   cin >> d1.year;
8   cout << "Enter month{1..12}:"; 
9   cin >> d1.month;
10   cout << "Enter day{1..31}:"; 
11   cin >> d1.day;
12   return d1;
13 }
14 void print_date(date d1){
15   cout << "year = " << d1.year;
16   cout << "\tmonth = " << d1.month;
17   cout << "\tday = " << d1.day << "\n";
18 }
19 int main(){
20   date birthDate = input_date();
21   print_date(birthDate);
22   return 0;
23 }

simple cout (print_date)

multiple output (print_date)

1 #include<iostream>
2 using namespace std;
3 struct date{ int year, month, day; };
4 date input_date(void){
5   date d1;
6   cout << "Enter year:{1350..1390} " ;
7   cin >> d1.year;
8   cout << "Enter month{1..12}:"; 
9   cin >> d1.month;
10   cout << "Enter day{1..31}:"; 
11   cin >> d1.day;
12   return d1;
13 }
14 void print_date(date d1){
15   cout << "year = " << d1.year;
16   cout << "\tmonth = " << d1.month;
17   cout << "\tday = " << d1.day << "\n";
18 }
19 int main(){
20   date birthDate = input_date();
21   print_date(birthDate);
22   return 0;
23 }
1 #include<iostream>
2 using namespace std;
3 struct date{ int year, month, day; };
4 date input_date(void){
5   date d1;
6   cout << "Enter year:{1350..1390} " ;
7   cin >> d1.year;
8   cout << "Enter month{1..12}:"; 
9   cin >> d1.month;
10   cout << "Enter day{1..31}:"; 
11   cin >> d1.day;
12   return d1;
13 }
14 void print_date(date d1){
15   cout << "year = " << d1.year 
16        << "\tmonth = " << d1.month
17        << "\tday = " << d1.day << "\n";
18 }
19 int main(){
20   date birthDate = input_date();
21   print_date(birthDate);
22 }

\n

endl

1 #include<iostream>
2 using namespace std;
3 struct date{ int year, month, day; };
4 date input_date(void){
5   date d1;
6   cout << "Enter year:{1350..1390} " ;
7   cin >> d1.year;
8   cout << "Enter month{1..12}:"; 
9   cin >> d1.month;
10   cout << "Enter day{1..31}:"; 
11   cin >> d1.day;
12   return d1;
13 }
14 void print_date(date d1){
15   cout << "year = " << d1.year 
16        << "\tmonth = " << d1.month
17        << "\tday = " << d1.day << "\n";
18 }
19 int main(){
20   date birthDate = input_date();
21   print_date(birthDate);
22 }
1 #include<iostream>
2 using namespace std;
3 struct date{ int year, month, day; };
4 date input_date(void){
5   date d1;
6   cout << "Enter year:{1350..1390} " ;
7   cin >> d1.year;
8   cout << "Enter month{1..12}:"; 
9   cin >> d1.month;
10   cout << "Enter day{1..31}:"; 
11   cin >> d1.day;
12   return d1;
13 }
14 void print_date(date d1){
15   cout << "year = " << d1.year
16        << "\tmonth = " << d1.month
17        << "\tday = " << d1.day << endl;
18 }
19 int main(){
20   date birthDate = input_date();
21   print_date(birthDate);
22   return 0;
23 }

different name

function signature

1 #include<iostream>
2 using namespace std;
3 struct date{ int year, month, day; };
4 date input_date(void);
5 void print_date(date d1);
6 void print(int k){cout << k << endl;}
7 int input(void)
8 {int n; cout <<"Enter n "; cin >> n; return n;}
9 int main(){
10   date d1;
11   d1 = input_date();
12   print_date(d1);
13 }
14 date input_date(void){
15   date d1;
16   cout << "Enter year:{1350..1390} "; 
17   cin >> d1.year;
18   cout << "Enter month{1..12}:"; 
19   cin >> d1.month;
20   cout << "Enter day{1..31}:"; 
21   cin >> d1.day;
22   return d1;
23 }
24 void print_date(date d1){
25   cout << "year = "  << d1.year;
26   cout << "\tmonth = " << d1.month;
27   cout << "\tday = " << d1.day << endl;
28 }
1 #include<iostream>
2 using namespace std;
3 struct date{ int year, month, day; };
4 date input_date(void);
5 void print(date d1);
6 void print(int k){cout << k << endl;}
7 int input(void)
8 {int n; cout <<"Enter n "; cin >> n; return n;}
9 int main(){
10   date d1;
11   d1 = input_date();
12   print(d1);
13 }
14 date input_date(void){
15   date d1;
16   cout << "Enter year:{1350..1390} "; 
17   cin >> d1.year;
18   cout << "Enter month{1..12}:"; 
19   cin >> d1.month;
20   cout << "Enter day{1..31}:"; 
21   cin >> d1.day;
22   return d1;
23 }
24 void print(date d1){
25   cout << "year = "  << d1.year;
26   cout << "\tmonth = " << d1.month;
27   cout << "\tday = " << d1.day << endl;
28 }

Same signature

Error

1 #include<iostream>
2 using namespace std;
3 struct date{ int year, month, day; };
4 date input(void);
5 void print(date d1);
6 void print(int k){cout << k << endl;}
7 int input(void)
8 {int n; cout <<"Enter n "; cin >> n; return n;}
9 int main(){
10   date d1;
11   d1 = input();
12   print(d1);
13 }
14 date input(void){
15   date d1;
16   cout << "Enter year:{1350..1390} "; 
17   cin >> d1.year;
18   cout << "Enter month{1..12}:"; 
19   cin >> d1.month;
20   cout << "Enter day{1..31}:"; 
21   cin >> d1.day;
22   return d1;
23 }
24 void print(date d1){
25   cout << "year = "  << d1.year;
26   cout << "\tmonth = " << d1.month;
27   cout << "\tday = " << d1.day << endl;
28 }
1 7:5: error: ambiguating new declaration of ‘int input()’
2     7 | int input(void)
3       |     ^~~~~
4 :4:6: note: old declaration ‘date input()’
5     4 | date input(void);
6       |      ^~~~~
7 Compilation failed.

function default values

void point(int x = 3, int y = 4);

point(1,2); // calls point(1,2)
point(1);   // calls point(1,4)
point();    // calls point(3,4)
int sum(int x, int y, int z=0, int w=0)
{
    return (x + y + z + w);
}

/* Driver program to test above function*/
int main()
{
    cout << sum(10, 15) << endl;
    cout << sum(10, 15, 25) << endl;
    cout << sum(10, 15, 25, 30) << endl;
}

Errors Default value for parameters(I)

1 #include<iostream>
2 using namespace std;
3 
4 int sum(int x, int y, int z=0, int w){
5     return (x + y + z + w);
6 }
7 
8 int main(){
9   cout << sum(10, 15) << endl;
10   cout << sum(10, 15, 25) << endl;
11   cout << sum(10, 15, 25, 30) << endl;
12 }
   4:36: error: default argument missing for parameter 4 of ‘int sum(int, int, int, int)’
   4 | int sum(int x, int y, int z=0, int w){
   |                                ~~~~^
   4:27: note: ...following parameter 3 which has a default argument
   4 | int sum(int x, int y, int z=0, int w){
   |                       ~~~~^~~

Errors Default value for parameters(II)

1 #include<iostream>
2 using namespace std;
3 int sum(int x, int y, int z=0, int w=0){
4   return (x + y + z + w);
5 }
6 int sum(int x, int y){
7   return (x + y);
8 }
9 int main(){
10   cout << sum(10, 15) << endl;
11   cout << sum(10, 15, 25) << endl;
12   cout << sum(10, 15, 25, 30) << endl;
13 }
 1  10:21: error: call of overloaded ‘sum(int, int)’ is ambiguous
 2     10 |   cout << sum(10, 15) << endl;
 3        |                     ^
 4  3:5: note: candidate: ‘int sum(int, int, int, int)’
 5      3 | int sum(int x, int y, int z=0, int w=0){
 6        |     ^~~
 7  6:5: note: candidate: ‘int sum(int, int)’
 8      6 | int sum(int x, int y){
 9        |     ^~~

Obeject Oriented, Encapsulation(I)

3 struct student{
4   char name[20];
5   int  id;
6   char address[50];
7 };
8 student input_student(){
9   student s1;
10   cout << "Enter name:";
11   cin >> s1.name;
12   cout << "id:";
13   cin >> s1.id;
14   cout << "address:";
15   cin >> s1.address;
16   return s1;
17 }
18 void print(student st1){
19   cout << "name:\t\t"
20     << st1.name << endl;
21   cout << "id:\t\t"
22     << st1.id << endl;
23   cout << "address:\t\t"
24     << st1.address << endl;
25 }
26 int main(){
27   student st1;
28   st1 = input_student();
29   print(st1);
30   student st2;
31   st2 = input_student();
32   print(st2);
33 }

 

3 struct student{
4   char name[20];
5   int  id;
6   char address[50];
7 
8   void input(){
9     cout << "Enter name:";
10     cin >> name;
11     cout << "id:";
12     cin >> id;
13     cout << "address:";
14     cin >> address;
15   }
16 
17   void print(){
18     cout << "name:\t\t"
19       << name << endl;
20     cout << "id:\t\t"
21       << id << endl;
22     cout << "address:\t\t"
23       << address << endl;
24   }
25 };
26 int main(){
27   student std;
28   std.input();
29   std.print();
30   student st2;
31   st2.input();
32   st2.print();
33 }

Encapsulation(II)

3 struct date{ int year, month, day; };
4 date input(){
5   date d1;
6   cout << "Year: "; 
7   cin >> d1.year;
8   cout << "Month: "; 
9   cin >> d1.month;
10   cout << "Day: "; 
11   cin >> d1.day;
12   return d1;
13 }
14 void print(date d1){
15   cout << d1.year << '/' << d1.month 
16     << '/' << d1.day << endl;
17 }
18 int main(){
19   date d1;
20   d1 = input();
21   print(d1);
22   date d2;
23   d2 = input();
24   print(d2);
25 }
3 struct date{ 
4   int year, month, day; 
5   void input(){
6     cout << "Year: "; 
7     cin >> year;
8     cout << "Month: "; 
9     cin >> month;
10     cout << "Day: "; 
11     cin >> day;
12   }
13   void print(){
14     cout << year << '/' << month 
15       << '/' << day << endl;
16   }
17 };
18 int main(){
19   date d1;
20   d1.input();
21   d1.print();
22   date d2;
23   d2.input();
24   d2.print();
25 }

Encapsulation(III)

3 struct point {double x, y;};
4 point inputPoint() {
5   point p1;
6   cout << "Enter the X value: ";
7   cin >> p1.x;
8   cout << "Enter the Y value: ";
9   cin >> p1.y;
10   return p1;
11 }
12 void print(point p1) {
13   cout << "X value: "
14     << p1.x << endl;
15   cout << "Y value: "
16     << p1.y << endl;
17 }
18 point move(point p1,
19     double dx, double dy) {
20   p1.x += dx;
21   p1.y += dy;
22   return p1;
23 }
24 int main(){
25   point p1 = inputPoint();
26   print(p1);
27 }
3 struct point{
4   double x, y;
5 
6   void input() {
7     cout << "Enter the X value: ";
8     cin >> x;
9     cout << "Enter the Y value: ";
10     cin >> y;
11   }
12   void print() {
13     cout << "X value: "
14       << x << endl;
15     cout << "Y value: "
16       << y << endl;
17   }
18   void move(double dx, double dy){
19     x += dx;
20     y += dy;
21   }
22 };
23 int main(){
24   point p1;
25   p1.input();
26   p1.print();
27 }

Encapsulation(IV)

3 struct complexCls{
4   double re,img;
5   void setRe(double r)
6   {re=r;}
7   void setImg(double i)
8   {img=i;}
9   double getRe(void)
10   {return re;}
11   double getImg(void)
12   {return img;}
13 };
14 void f1(){
15   complexCls c1;
16   c1.re=12;
17   c1.setRe(12);
18   c1.setImg(4);
19   cout << c1.getRe() << endl;
20 }
21 int main(){f1();}
3 struct complexCls{
4   double re,img;
5   void setRe(double r)
6   {re=r;}
7   void setImg(double i)
8   {img=i;}
9   double getRe(void)
10   {return re;}
11   double getImg(void)
12   {return img;}
13 
14   void set(double r=0, double i=0)
15   {re = r; img = i;}
16 };
17 void f1(void){
18   complexCls c1;
19   c1.set(12,14);
20   cout << c1.getRe() << endl;
21 }
22 int main(){f1();}

Attribute Default Value(I)

3 struct complexCls{
4   double re, img; // No Default
5   void set(double r = 0, double i = 0)
6   { re = r; img = i;}
7   void print(){
8     cout << '(' << re << ','
9       << img << ')' << endl;
10   }
11   void input(){
12     cout << "Enter re "; cin >> re;
13     cout << "Enter img "; cin >> img;
14   }
15 };
16 void f1(complexCls c1){
17   c1.re=12;
18   c1.print();
19   c1.set(12,14);
20   c1.print();
21 }
22 int main(){
23   complexCls c1;
24   f1(c1);
25 }
3 struct complexCls{
4   double re = 0, img = 0; // Default
5   void set(double r = 0, double i = 0)
6   { re = r; img = i;}
7   void print(){
8     cout << '(' << re << ','
9       << img << ')' << endl;
10   }
11   void input(){
12     cout << "Enter re "; cin >> re;
13     cout << "Enter img "; cin >> img;
14   }
15 };
16 void f1(complexCls c1){
17   c1.re=12;
18   c1.print();
19   c1.set(12,14);
20   c1.print();
21 }
22 int main(){
23   complexCls c1;
24   f1(c1);
25 }

Attribute Default Value(II)

3 struct date{ 
4   int year, month, day; 
5   void input(){
6     cout << "Year: "; 
7     cin >> year;
8     cout << "Month: "; 
9     cin >> month;
10     cout << "Day: "; 
11     cin >> day;
12   }
13   void print(){
14     cout << year << '/' << month 
15       << '/' << day << endl;
16   }
17 };
18 int main(){
19   date d1;
20   d1.input();
21   d1.print();
22   date d2;
23   d2.input();
24   d2.print();
25 }
3 struct date{ 
4   int year=1384, month=10, day=28; 
5   void input(void){
6     cout << "Year: "; 
7     cin >> year;
8     cout << "Month: "; 
9     cin >> month;
10     cout << "Day: "; 
11     cin >> day;
12   }
13   void print(void){
14     cout << year << '/' << month 
15       << '/' << day << endl;
16   }
17 };
18 int main(){
19   date d1;
20   d1.input();
21   d1.print();
22   date d2;
23   d2.input();
24   d2.print();
25 }

Attribute Default Value(III)

2 #include <cmath>
3 using namespace std;
4 struct point{
5   double x, y;
6   void input(void) {
7     cout << "Enter X: ";
8     cin >> x;
9     cout << "Enter Y: ";
10     cin >> y;
11   }
12   void print(void) {
13     cout << '(' << x << ','
14       << y << ')' << endl;
15   }
16   void move(double dx=1, double dy=0){
17     x += dx;
18     y += dy;
19   }
20   double magnitude(void){ // length
21     return sqrt(pow(x, 2)+pow(y, 2));
22   }
23 };
24 int main(){
25   point p1;
26   p1.print();
27   p1.input();
28   p1.print();
29   p1.move(3);
30   cout << p1.magnitude() << endl;
31 }
2 #include <cmath>
3 using namespace std;
4 struct point{
5   double x=0, y=0;
6   void input(void) {
7     cout << "Enter X: ";
8     cin >> x;
9     cout << "Enter Y: ";
10     cin >> y;
11   }
12   void print(void) {
13     cout << '(' << x << ','
14       << y << ')' << endl;
15   }
16   void move(double dx=1, double dy=0){
17     x += dx;
18     y += dy;
19   }
20   double magnitude(void){ // length
21     return sqrt(pow(x, 2)+pow(y, 2));
22   }
23 };
24 int main(){
25   point p1;
26   p1.print();
27   p1.input();
28   p1.print();
29   p1.move(3);
30   cout << p1.magnitude() << endl;
31 }

MyArray Struct(I)

1 #include <iostream>
2 using namespace std;
3 struct myArray{
4   double a[100];
5   int n;
6   void set(double ma[], int k){
7     n = k;
8     for(int i = 0; i < n; i++)
9       a[i] = ma[i];
10   }
11   void print(void){
12     cout << " n = " << n << endl;
13     for(int i = 0; i < n; i++)
14       cout << "a[" << i << "] = " 
15         << a[i] << endl;
16   }
17 };
18 int main(){
19   double x[] = {10, 12, 34, 54};
20   myArray d;
21   d.set(x, 4);
22   d.print();
23 }
1 #include <iostream>
2 using namespace std;
3 struct myArray{
4   double a[100];
5   int n;
6   void set(double ma[], int k){
7     n = k;
8     for(int i=0; i < n; i++)
9       a[i] = ma[i];
10   }
11   void print(void){
12     cout << " n = " << n << endl;
13     for(int i = 0; i < n; i++)
14       cout << "a[" << i << "] = " 
15         << a[i] << endl;
16   }
17 };
18 int main(){
19   double x[]{10, 12, 34, 54};
20   myArray d;
21   d.set(x, 4);
22   d.print();
23 }

MyArray Struct(II)

1 #include <iostream>
2 using namespace std;
3 struct myArray{
4   double a[100];
5   int n = 0;
6   void set(double ma[], int k){
7     n = k;
8     for(int i = 0; i < n; i++)
9       a[i] = ma[i];
10   }
11   void print(void){
12     cout << " n = " << n << endl;
13     for(int i = 0; i < n; i++)
14       cout << "a[" << i << "] = "
15         << a[i] << endl;
16   }
17 };
18 int main(){
19   double x[]{10, 12, 34, 54};
20   myArray d;
21   d.set(x, sizeof(x) / sizeof(double));
22   d.print();
23 }
1 #include <iostream> //MyArray/100422
2 using namespace std;
3 struct myArray{
4   double a[100];
5   int n = 0;
6   void set(double ma[], int k){
7     n = k;
8     for(int i = 0; i < n; i++)
9       a[i] = ma[i];
10   }
11   void print(void){
12     cout << " n = " << n << endl;
13     for(int i = 0; i < n; i++)
14       cout << "a[" << i << "] = "
15         << a[i] << endl;
16   }
17 };
18 int main(){
19   double x[]{10, 12, 34, 54};
20   myArray d;
21   d.set(x, sizeof(x) / sizeof(x[0]));
22   d.print();
23 }

MyArray Struct(III) - Error

1 #include <iostream>
2 using namespace std;
3 struct myArray{
4   double a[100];
5   int n = 0;
6   void set(double ma[], int k){
7     n = k;
8     for(int i = 0; i < n; i++)
9       a[i] = ma[i];
10   }
11   void print(void){
12     cout << " n = " << n << endl;
13     for(int i = 0; i < n; i++)
14       cout << "a[" << i << "] = "
15         << a[i] << endl;
16   }
17 };
18 int main(){
19   double x[]{10, 12, 34, 54};
20   myArray d;
21   d.set(x, sizeof(x) / sizeof(double));
22   d.print();
23 }
1 #include <iostream> //MyArray/100423.22
2 using namespace std;
3 struct myArray{
4   double a[100];
5   int n = 0;
6   void set(double ma[], int k){
7     if(k >= 0 && k <= 100){
8       n = k;
9       for(int i = 0; i < n; i++)
10         a[i] = ma[i];
11     }
12     else
13       cout << "Number of Elements is wrong" 
14         << k << endl;
15   }
16   void print(void){
17     cout << " n = " << n << endl;
18     for(int i = 0; i < n; i++)
19       cout << "a[" << i << "] = "
20         << a[i] << endl;
21   }
22 };
23 int main(){
24   double x[]{10, 12, 34, 54};
25   myArray d;
26   d.set(x, sizeof(x) / sizeof(x[0]));
27   d.print();
28 }

Error in Action

1 #include <iostream>
2 using namespace std;
3 struct myArray{//100423.33.cpp
4   int n = 0;
5   double a[100];
6   void set(double ma[], int k){
7     n = k;
8     for(int i = 0; i < n; i++)
9       a[i] = ma[i];
10   }
11   void print(void){
12     cout << " n = " << n << endl;
13     for(int i = 0; i < n; i++)
14       cout << "a[" << i << "] = "
15         << a[i] << endl;
16   }
17 };
18 int main(){
19   double x[2000]{10, 12, 34, 54};
20   myArray d;
21   d.set(x, 2000);
22   d.print();
23   myArray ax[30000];
24   for(int i=0 ; i<30000; i++){
25     ax[i].set(x, 2000);
26     ax[i].print();
27   }
28 }
29 
30 /* ./a.out
31 Segmentation fault (core dumped)
32 */

Order of properties and methods is irrelevant in struct

1 #include <iostream> //MyArray/100423.22
2 using namespace std;
3 struct myArray{
4   double a[100];
5   int n = 0;
6   void set(double ma[], int k){
7     if(k >= 0 && k <= 100){
8       n = k;
9       for(int i = 0; i < n; i++)
10         a[i] = ma[i];
11     }
12     else
13       cout << "Number of Elements is wrong" 
14         << k << endl;
15   }
16   void print(void){
17     cout << " n = " << n << endl;
18     for(int i = 0; i < n; i++)
19       cout << "a[" << i << "] = "
20         << a[i] << endl;
21   }
22 };
23 int main(){
24   double x[]{10, 12, 34, 54};
25   myArray d;
26   d.set(x, sizeof(x) / sizeof(x[0]));
27   d.print();
28 }
1 #include <iostream> //MyArray/100423.44
2 using namespace std;
3 struct myArray{
4   void set(double ma[], int k){
5     if(k >= 0 && k < 100)
6       for(n = k--; k >= 0; k--)
7         a[k] = ma[k];
8     else
9       cout << "Number of Elements is wrong" 
10         << k << endl;
11   }
12   void print(void){
13     cout << " n = " << n << endl;
14     for(int i = 0; i < n; i++)
15       cout << "a[" << i << "] = "
16         << a[i] << endl;
17   }
18 
19   double a[100];
20   int n = 0;
21 };
22 int main(){
23   double x[]{10, 12, 34, 54};
24   myArray d;
25   d.set(x, sizeof(x) / sizeof(x[0]));
26   d.print();
27 }

MyArray Struct(IV) Const(I)

1 #include <iostream> //MyArray/100423.22
2 using namespace std;
3 struct myArray{
4   double a[100];
5   int n = 0;
6   void set(double ma[], int k){
7     if(k >= 0 && k <= 100){
8       n = k;
9       for(int i = 0; i < n; i++)
10         a[i] = ma[i];
11     }
12     else
13       cout << "Number of Elements is wrong" 
14         << k << endl;
15   }
16   void print(void){
17     cout << " n = " << n << endl;
18     for(int i = 0; i < n; i++)
19       cout << "a[" << i << "] = "
20         << a[i] << endl;
21   }
22 };
23 int main(){
24   double x[]{10, 12, 34, 54};
25   myArray d;
26   d.set(x, sizeof(x) / sizeof(x[0]));
27   d.print();
28 }
1 #include <iostream> //MyArray/100424
2 using namespace std;
3 const int MAX_NUMBER_OF_CELLS = 100;
4 struct myArray{
5   double a[MAX_NUMBER_OF_CELLS];
6   int n = 0;
7   void set(double ma[], int k){
8     if(k >= 0 && k < MAX_NUMBER_OF_CELLS){
9       n = k;
10       for(int i = 0; i < n; i++)
11         a[i] = ma[i];
12     }
13     else
14       cout << "Number of Elements is wrong" 
15         << k << endl;    
16   }
17   void print(void){
18     cout << " n = " << n << endl;
19     for(int i = 0; i < n; i++)
20       cout << "a[" << i << "] = "
21         << a[i] << endl;
22   }
23 };
24 int main(){
25   double x[]{10, 12, 34, 54};
26   myArray d;
27   d.set(x, sizeof(x) / sizeof(x[0]));
28   d.print();
29 }

MyArray Struct(V) Const(II)

1 #include <iostream> //MyArray/100424
2 using namespace std;
3 const int MAX_NUMBER_OF_CELLS = 100;
4 struct myArray{
5   double a[MAX_NUMBER_OF_CELLS];
6   int n = 0;
7   void set(double ma[], int k){
8     if(k >= 0 && k < MAX_NUMBER_OF_CELLS){
9       n = k;
10       for(int i = 0; i < n; i++)
11         a[i] = ma[i];
12     }
13     else
14       cout << "Number of Elements is wrong" 
15         << k << endl;    
16   }
17   void print(void){
18     cout << " n = " << n << endl;
19     for(int i = 0; i < n; i++)
20       cout << "a[" << i << "] = "
21         << a[i] << endl;
22   }
23 };
24 int main(){
25   double x[]{10, 12, 34, 54};
26   myArray d;
27   d.set(x, sizeof(x) / sizeof(x[0]));
28   d.print();
29 }
1 #include <iostream> //MyArray/100426.22
2 using namespace std;
3 const int MAX_NUMBER_OF_CELLS = 100;
4 struct myArray{
5   double a[MAX_NUMBER_OF_CELLS];
6   int n = 0;
7   void set(const double ma[], const int k){
8     if(k >= 0 && k < MAX_NUMBER_OF_CELLS){
9       n = k;
10       for(int i = 0; i < n; i++)
11         a[i] = ma[i];
12     }
13     else
14       cout << "Number of Elements is wrong" 
15         << k << endl;    
16   }
17   void print(void){
18     cout << " n = " << n << endl;
19     for(int i = 0; i < n; i++)
20       cout << "a[" << i << "] = "
21         << a[i] << endl;
22   }
23 };
24 int main(){
25   double x[]{10, 12, 34, 54};
26   myArray d;
27   d.set(x, sizeof(x) / sizeof(x[0]));
28   d.print();
29 }

---

END

1