C++ Slides - I: Objects and Classes: Structure in C and C++, Class Specification, Objects
C++ Slides - I: Objects and Classes: Structure in C and C++, Class Specification, Objects
C++ Slides - I: Objects and Classes: Structure in C and C++, Class Specification, Objects
Example
cout<<“Hello and welcome”;
cin>>variable; //variable could be int, float, char array (without space)
cout basics
•‘\n’ is for new line, or you can use endl
cout << endl << “message”;
•‘\t’ is for tab
•‘\a’ is an alarm sound
•‘\r’ is carriage return to go to the beginning of the current
line
Input char array with blanks
#include<iostream>
using namespace std;
int main(){
char str[20];
cin.getline(str,sizeof(str));
cout<<str<<endl;
}
Header files and namespace
Header file and more
•#include <iostream> // input-output stream for cin/cout
•using namespace std;
int main() {
cout << ns1::value() << '\n'; //5 will be displayed
cout << ns2::value() << '\n'; // -5 will be displayed
}
What will be the output?
#include <iostream>
using namespace std;
namespace ns1 { int value() {return 5;}}
namespace ns2 { int x=10; int value() {return 4;}}
int main() {
cout << ns1::value() << endl;
cout << ns2::value() << endl;
cout<< ns2::x<<endl;
}
Without namespace could cause :: pollution
#include<iostream>
int main(){
std::cout << “Hello there” << std::endl;
return 0;
}
using namespace std;
• Thus using namespace std; means cin/cout will be performed
through standard console screen.
Header file and namespace
•#include<iostream> includes all necessary files required of
CIN and COUT opertions.
•using namespace std; allows us to reduce :: pollution for
simpler programs
Classes in C++
•Class: A class in C++ is the building block of object oriented
programming
•It is User Defined Datatype (UDT) which has data & functions
•Object is an instance of a Class i.e. variable of UDT.
Structure in C++
#include <iostream>
using namespace std;
struct Person{int age;};
int main(){
Person p1; // No need to write struct Person p1 in C++.
cout << "Enter age: ";cin >> p1.age;
cout <<"Age: " << p1.age << endl;
return 0;
}
Structures in C++ vs in C
1. Functions can be defined inside structure in C++
2. Using struct keyword not required in C++
3. C++ structures can have static members
4. C++ allows data hiding by using access modifiers
#include <iostream>
using namespace std;
struct Person{
int age; //variable
int setAge(int a){age = a;} //function
int display() {cout <<"Age: " << age << endl;} //function
};
int main(){
Person p1;
p1.setAge(20); p1.display();
return 0;
}
Need more knowledge for 3 & 4
1. Functions can be defined inside structure in C++
2. Using struct keyword not required in C++
3. C++ structures can have static members
4. C++ allows data hiding by using access modifiers
•Classes in C++ are similar to struct for syntax
•struct – everything is public by default
•class – everything is private by default.
What is the output of the following program?
#include <iostream>
using namespace std;
class Person{
int age;
int setAge(int a){age = a;}
int display() {cout <<"Age: " << age << endl;}
};
int main(){
Person p1;
p1.setAge(20); p1.display();
return 0;
}
Answer - Output – Compilation Error
class A{
int a; char c;
};
int main(){
A a[3]; // each of a[i] will have an int and a char
}
Passing and returning an object
class A{
int i;
public: A(){i=10;}
void show(){cout<<"i = "<<i<<endl;}
A makedouble(A obj){A temp; temp.i = 2*obj.i; return temp;}
};
int main(){
A a1,a2; a1.show(); a2 = a1.makedouble(a1); a2.show();
}
Inline functions
inline int cube(int s){ return s*s*s; }
int main() {
cout << "The cube of 3 is: " << cube(3) << endl;
}
Inline function properties
•Reduces function-call overhead
•Asks the compiler to copy code into program instead of using
function call
•Compiler can ignore inline
•Should be used for small, often used functions
‘const’ member function
int main(){
const int i = 10;
const int j = i + 10; // works fine
i++; // this leads to Compile time error
}
Const class variable
class Test{
const int i;
public:
Test(int x):i(x) {} //initialized using constructor
void show(){cout<<"i="<<i<<endl;}
};
int main(){Test t(190);t.show(); }
Const function
•The idea of const functions is not to allow them to modify
the object on which they are called.
•It is recommended to make as many functions const as
possible so that accidental changes to objects are avoided.
Const class member function
class A{
public: int x;
void func() const{
x = 0; // [Error] can’t modify object variable
}
};
int main(){}
Extra concepts
Const function and object
const function should be a member function
#include<iostream>
using namespace std;
int i = 99;
void fun() const{}
int main(){}